35
35
36
36
final class TelnetInputStream extends BufferedInputStream implements Runnable
37
37
{
38
+ /** End of file has been reached */
39
+ private static final int EOF = -1 ;
40
+
41
+ /** Read would block */
42
+ private static final int WOULD_BLOCK = -2 ;
43
+
44
+ // TODO should these be private enums?
38
45
static final int _STATE_DATA = 0 , _STATE_IAC = 1 , _STATE_WILL = 2 ,
39
46
_STATE_WONT = 3 , _STATE_DO = 4 , _STATE_DONT = 5 ,
40
47
_STATE_SB = 6 , _STATE_SE = 7 , _STATE_CR = 8 , _STATE_IAC_SB = 9 ;
@@ -106,20 +113,29 @@ void _start()
106
113
// TelnetOutputStream writing through the telnet client at same time
107
114
// as a processDo/Will/etc. command invoked from TelnetInputStream
108
115
// tries to write.
116
+ /**
117
+ * Get the next byte of data.
118
+ * IAC commands are processed internally and do not return data.
119
+ *
120
+ * @param mayBlock true if method is allowed to block
121
+ * @return the next byte of data,
122
+ * or -1 (EOF) if end of stread reached,
123
+ * or -2 (WOULD_BLOCK) if mayBlock is false and there is no data available
124
+ */
109
125
private int __read (boolean mayBlock ) throws IOException
110
126
{
111
127
int ch ;
112
128
113
129
while (true )
114
130
{
115
131
116
- // If there is no more data AND we were told not to block, just return -2 . (More efficient than exception.)
132
+ // If there is no more data AND we were told not to block, just return WOULD_BLOCK (-2) . (More efficient than exception.)
117
133
if (!mayBlock && super .available () == 0 )
118
- return - 2 ;
134
+ return WOULD_BLOCK ;
119
135
120
136
// Otherwise, exit only when we reach end of stream.
121
137
if ((ch = super .read ()) < 0 )
122
- return - 1 ;
138
+ return EOF ;
123
139
124
140
ch = (ch & 0xff );
125
141
@@ -341,9 +357,9 @@ public int read() throws IOException
341
357
342
358
if (__bytesAvailable == 0 )
343
359
{
344
- // Return -1 if at end of file
360
+ // Return EOF if at end of file
345
361
if (__hasReachedEOF )
346
- return - 1 ;
362
+ return EOF ;
347
363
348
364
// Otherwise, we have to wait for queue to get something
349
365
if (__threaded )
@@ -371,9 +387,9 @@ public int read() throws IOException
371
387
{
372
388
try
373
389
{
374
- if ((ch = __read (mayBlock )) < 0 )
375
- if (ch != - 2 )
376
- return (ch );
390
+ if ((ch = __read (mayBlock )) < 0 ) // EOF or WOULD_BLOCK
391
+ if (ch != WOULD_BLOCK )
392
+ return (ch ); // must be EOF
377
393
}
378
394
catch (InterruptedIOException e )
379
395
{
@@ -389,21 +405,21 @@ public int read() throws IOException
389
405
{
390
406
}
391
407
}
392
- return (- 1 ) ;
408
+ return EOF ;
393
409
}
394
410
395
411
396
412
try
397
413
{
398
- if (ch != - 2 )
414
+ if (ch != WOULD_BLOCK )
399
415
{
400
416
__processChar (ch );
401
417
}
402
418
}
403
419
catch (InterruptedException e )
404
420
{
405
421
if (__isClosed )
406
- return (- 1 ) ;
422
+ return EOF ;
407
423
}
408
424
409
425
// Reads should not block on subsequent iterations. Potentially, this could happen if the
@@ -488,16 +504,16 @@ public int read(byte buffer[], int offset, int length) throws IOException
488
504
length = __bytesAvailable ;
489
505
}
490
506
491
- if ((ch = read ()) == - 1 )
492
- return - 1 ;
507
+ if ((ch = read ()) == EOF )
508
+ return EOF ;
493
509
494
510
off = offset ;
495
511
496
512
do
497
513
{
498
514
buffer [offset ++] = (byte )ch ;
499
515
}
500
- while (--length > 0 && (ch = read ()) != - 1 );
516
+ while (--length > 0 && (ch = read ()) != EOF );
501
517
502
518
//__client._spyRead(buffer, off, offset - off);
503
519
return (offset - off );
0 commit comments