Skip to content

Commit 444d7f1

Browse files
committed
Document "magic" numbers -1 and -2
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/net/branches/NET_2_0@963079 13f79535-47bb-0310-9956-ffa450edef68
1 parent dec6c1a commit 444d7f1

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/main/java/org/apache/commons/net/telnet/TelnetInputStream.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@
3535

3636
final class TelnetInputStream extends BufferedInputStream implements Runnable
3737
{
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?
3845
static final int _STATE_DATA = 0, _STATE_IAC = 1, _STATE_WILL = 2,
3946
_STATE_WONT = 3, _STATE_DO = 4, _STATE_DONT = 5,
4047
_STATE_SB = 6, _STATE_SE = 7, _STATE_CR = 8, _STATE_IAC_SB = 9;
@@ -106,20 +113,29 @@ void _start()
106113
// TelnetOutputStream writing through the telnet client at same time
107114
// as a processDo/Will/etc. command invoked from TelnetInputStream
108115
// 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+
*/
109125
private int __read(boolean mayBlock) throws IOException
110126
{
111127
int ch;
112128

113129
while (true)
114130
{
115131

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.)
117133
if(!mayBlock && super.available() == 0)
118-
return -2;
134+
return WOULD_BLOCK;
119135

120136
// Otherwise, exit only when we reach end of stream.
121137
if ((ch = super.read()) < 0)
122-
return -1;
138+
return EOF;
123139

124140
ch = (ch & 0xff);
125141

@@ -341,9 +357,9 @@ public int read() throws IOException
341357

342358
if (__bytesAvailable == 0)
343359
{
344-
// Return -1 if at end of file
360+
// Return EOF if at end of file
345361
if (__hasReachedEOF)
346-
return -1;
362+
return EOF;
347363

348364
// Otherwise, we have to wait for queue to get something
349365
if(__threaded)
@@ -371,9 +387,9 @@ public int read() throws IOException
371387
{
372388
try
373389
{
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
377393
}
378394
catch (InterruptedIOException e)
379395
{
@@ -389,21 +405,21 @@ public int read() throws IOException
389405
{
390406
}
391407
}
392-
return (-1);
408+
return EOF;
393409
}
394410

395411

396412
try
397413
{
398-
if(ch != -2)
414+
if(ch != WOULD_BLOCK)
399415
{
400416
__processChar(ch);
401417
}
402418
}
403419
catch (InterruptedException e)
404420
{
405421
if (__isClosed)
406-
return (-1);
422+
return EOF;
407423
}
408424

409425
// 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
488504
length = __bytesAvailable;
489505
}
490506

491-
if ((ch = read()) == -1)
492-
return -1;
507+
if ((ch = read()) == EOF)
508+
return EOF;
493509

494510
off = offset;
495511

496512
do
497513
{
498514
buffer[offset++] = (byte)ch;
499515
}
500-
while (--length > 0 && (ch = read()) != -1);
516+
while (--length > 0 && (ch = read()) != EOF);
501517

502518
//__client._spyRead(buffer, off, offset - off);
503519
return (offset - off);

0 commit comments

Comments
 (0)