2 // A stream with some useful stdio-like methods. Can be used either for
3 // inheriting those methods into your own input stream, or for adding them
4 // to some input stream.
5 import java.io.InputStream;
6 import java.io.IOException;
7 import java.io.EOFException;
9 public class LineInputStream
13 LineInputStream(InputStream i)
18 public int read() throws IOException
20 public int read(byte b[]) throws IOException
21 { return in.read(b); }
22 public int read(byte b[], int o, int l) throws IOException
23 { return in.read(b, o, l); }
24 public long skip(long n) throws IOException
25 { return in.skip(n); }
26 public int available() throws IOException
27 { return in.available(); }
28 public void close() throws IOException
30 public synchronized void mark(int readlimit)
31 { in.mark(readlimit); }
32 public synchronized void reset() throws IOException
34 public boolean markSupported()
35 { return in.markSupported(); }
38 // Read a line and return it (minus the \n)
39 String gets() throws IOException, EOFException
41 StringBuffer buf = new StringBuffer();
43 while((b = read()) != '\n') {
44 if (b == -1) throw new EOFException();
47 if (buf.length() != 0 && buf.charAt(buf.length()-1) == '\r')
48 buf.setLength(buf.length()-1); // lose \r
49 return buf.toString();
53 // Read a single word, surrounded by whitespace
54 String getw() throws IOException, EOFException
56 StringBuffer buf = new StringBuffer();
60 if ((b = read()) == -1) throw new EOFException();
61 } while(Character.isSpace((char)b));
65 if ((b = read()) == -1) throw new EOFException();
66 } while(!Character.isSpace((char)b));
67 return buf.toString();
71 // Fill the given array completely, even if read() only reads
72 // some max number of bytes at a time.
73 public int readdata(byte b[]) throws IOException, EOFException
77 p += read(b, p, b.length-p);