Index: classpath/gnu/java/net/LineInputStream.java =================================================================== RCS file: /cvs/gcc/gcc/libjava/classpath/gnu/java/net/LineInputStream.java,v retrieving revision 1.1.1.1 diff -c -p -r1.1.1.1 LineInputStream.java *** classpath/gnu/java/net/LineInputStream.java 16 Jul 2005 00:33:56 -0000 1.1.1.1 --- classpath/gnu/java/net/LineInputStream.java 12 Sep 2005 18:30:39 -0000 *************** *** 1,5 **** /* LineInputStream.java -- ! Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc. This file is part of GNU Classpath. --- 1,5 ---- /* LineInputStream.java -- ! Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc. This file is part of GNU Classpath. *************** public class LineInputStream *** 67,77 **** private boolean eof; /** - * Whether we can use block reads. - */ - private final boolean blockReads; - - /** * Constructor using the US-ASCII character encoding. * @param in the underlying input stream */ --- 67,72 ---- *************** public class LineInputStream *** 91,99 **** buf = new ByteArrayOutputStream(); this.encoding = encoding; eof = false; - blockReads = in.markSupported(); } /** * Read a line of input. */ --- 86,107 ---- buf = new ByteArrayOutputStream(); this.encoding = encoding; eof = false; } + + private String bufToString() throws IOException + { + if ("US-ASCII".equals(encoding)) + { + return buf.toString(0); + } + else + { + return buf.toString(encoding); + } + } + + /** * Read a line of input. */ *************** public class LineInputStream *** 106,198 **** } do { ! if (blockReads) ! { ! // Use mark and reset to read chunks of bytes ! final int MIN_LENGTH = 1024; ! int len, pos; ! ! len = in.available(); ! len = (len < MIN_LENGTH) ? MIN_LENGTH : len; ! byte[] b = new byte[len]; ! in.mark(len); ! // Read into buffer b ! len = in.read(b, 0, len); ! // Handle EOF ! if (len == -1) ! { ! eof = true; ! if (buf.size() == 0) ! { ! return null; ! } ! else ! { ! // We don't care about resetting buf ! return buf.toString(encoding); ! } ! } ! // Get index of LF in b ! pos = indexOf(b, len, (byte) 0x0a); ! if (pos != -1) ! { ! // Write pos bytes to buf ! buf.write(b, 0, pos); ! // Reset stream, and read pos + 1 bytes ! in.reset(); ! pos += 1; ! while (pos > 0) ! { ! len = in.read(b, 0, pos); ! pos = (len == -1) ? -1 : pos - len; ! } ! // Return line ! String ret = buf.toString(encoding); ! buf.reset(); ! return ret; ! } ! else ! { ! // Append everything to buf and fall through to re-read. ! buf.write(b, 0, len); ! } ! } ! else { ! // We must use character reads in order not to read too much ! // from the underlying stream. ! int c = in.read(); ! switch (c) { ! case -1: ! eof = true; ! if (buf.size() == 0) ! { ! return null; ! } ! // Fall through and return contents of buffer. ! case 0x0a: // LF ! String ret = buf.toString(encoding); ! buf.reset(); ! return ret; ! default: ! buf.write(c); } } } while (true); } - - private int indexOf(byte[] b, int len, byte c) - { - for (int pos = 0; pos < len; pos++) - { - if (b[pos] == c) - { - return pos; - } - } - return -1; - } } --- 114,140 ---- } do { ! // We must use character reads in order not to read too much ! // from the underlying stream. ! int c = in.read(); ! switch (c) { ! case -1: ! eof = true; ! if (buf.size() == 0) { ! return null; } + // Fall through and return contents of buffer. + case 0x0a: // LF + String ret = bufToString(); + buf.reset(); + return ret; + default: + buf.write(c); } } while (true); } }