1 |
/* DataInputStream.java -- FilteredInputStream that implements DataInput |
/* DataInputStream.java -- FilteredInputStream that implements DataInput |
2 |
Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation |
Copyright (C) 1998, 1999, 2000, 2001, 2003, 2005 Free Software Foundation |
3 |
|
|
4 |
This file is part of GNU Classpath. |
This file is part of GNU Classpath. |
5 |
|
|
56 |
*/ |
*/ |
57 |
public class DataInputStream extends FilterInputStream implements DataInput |
public class DataInputStream extends FilterInputStream implements DataInput |
58 |
{ |
{ |
|
// readLine() hack to ensure that an '\r' not followed by an '\n' is |
|
|
// handled correctly. If set, readLine() will ignore the first char it sees |
|
|
// if that char is a '\n' |
|
|
boolean ignoreInitialNewline = false; |
|
|
|
|
59 |
// Byte buffer, used to make primitive read calls more efficient. |
// Byte buffer, used to make primitive read calls more efficient. |
60 |
byte[] buf = new byte [8]; |
byte[] buf = new byte [8]; |
61 |
|
|
347 |
* |
* |
348 |
* @deprecated |
* @deprecated |
349 |
*/ |
*/ |
350 |
public final String readLine () throws IOException |
public final String readLine() throws IOException |
351 |
{ |
{ |
352 |
StringBuffer strb = new StringBuffer (); |
StringBuffer strb = new StringBuffer(); |
353 |
|
|
354 |
readloop: while (true) |
while (true) |
355 |
{ |
{ |
356 |
int c = 0; |
int c = in.read(); |
357 |
char ch = ' '; |
if (c == -1) // got an EOF |
358 |
boolean getnext = true; |
return strb.length() > 0 ? strb.toString() : null; |
359 |
while (getnext) |
if (c == '\r') |
|
{ |
|
|
getnext = false; |
|
|
c = in.read(); |
|
|
if (c < 0) // got an EOF |
|
|
return strb.length () > 0 ? strb.toString () : null; |
|
|
ch = (char) c; |
|
|
if ((ch &= 0xFF) == '\n') |
|
|
// hack to correctly handle '\r\n' sequences |
|
|
if (ignoreInitialNewline) |
|
|
{ |
|
|
ignoreInitialNewline = false; |
|
|
getnext = true; |
|
|
} |
|
|
else |
|
|
break readloop; |
|
|
} |
|
|
|
|
|
if (ch == '\r') |
|
360 |
{ |
{ |
361 |
// FIXME: The following code tries to adjust the stream back one |
int next_c = in.read(); |
362 |
// character if the next char read is '\n'. As a last resort, |
if (next_c != '\n' && next_c != -1) |
363 |
// it tries to mark the position before reading but the bottom |
{ |
364 |
// line is that it is possible that this method will not properly |
if (! (in instanceof PushbackInputStream)) |
365 |
// deal with a '\r' '\n' combination thus not fulfilling the |
in = new PushbackInputStream(in); |
366 |
// DataInput contract for readLine. It's not a particularly |
((PushbackInputStream) in).unread(next_c); |
367 |
// safe approach threadwise since it is unsynchronized and |
} |
368 |
// since it might mark an input stream behind the users back. |
break; |
|
// Along the same vein it could try the same thing for |
|
|
// ByteArrayInputStream and PushbackInputStream, but that is |
|
|
// probably overkill since this is deprecated & BufferedInputStream |
|
|
// is the most likely type of input stream. |
|
|
// |
|
|
// The alternative is to somehow push back the next byte if it |
|
|
// isn't a '\n' or to have the reading methods of this class |
|
|
// keep track of whether the last byte read was '\r' by readLine |
|
|
// and then skip the very next byte if it is '\n'. Either way, |
|
|
// this would increase the complexity of the non-deprecated methods |
|
|
// and since it is undesirable to make non-deprecated methods |
|
|
// less efficient, the following seems like the most reasonable |
|
|
// approach. |
|
|
int next_c = 0; |
|
|
char next_ch = ' '; |
|
|
if (in instanceof BufferedInputStream) |
|
|
{ |
|
|
next_c = in.read(); |
|
|
next_ch = (char) (next_c & 0xFF); |
|
|
if ((next_ch != '\n') && (next_c >= 0)) |
|
|
{ |
|
|
BufferedInputStream bin = (BufferedInputStream) in; |
|
|
if (bin.pos > 0) |
|
|
bin.pos--; |
|
|
} |
|
|
} |
|
|
else if (markSupported()) |
|
|
{ |
|
|
next_c = in.read(); |
|
|
next_ch = (char) (next_c & 0xFF); |
|
|
if ((next_ch != '\n') && (next_c >= 0)) |
|
|
{ |
|
|
mark(1); |
|
|
if ((in.read() & 0xFF) != '\n') |
|
|
reset(); |
|
|
} |
|
|
} |
|
|
// In order to catch cases where 'in' isn't a BufferedInputStream |
|
|
// and doesn't support mark() (such as reading from a Socket), set |
|
|
// a flag that instructs readLine() to ignore the first character |
|
|
// it sees _if_ that character is a '\n'. |
|
|
else ignoreInitialNewline = true; |
|
|
break; |
|
369 |
} |
} |
370 |
strb.append(ch); |
if (c == '\n') |
371 |
|
break; |
372 |
|
strb.append((char) c); |
373 |
} |
} |
374 |
|
|
375 |
return strb.length() > 0 ? strb.toString() : ""; |
return strb.length() > 0 ? strb.toString() : ""; |