38 |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
* @author <a href="mailto:dog@gnu.org">Chris Burdess</a> |
39 |
* @version $Revision$ $Date$ |
* @version $Revision$ $Date$ |
40 |
*/ |
*/ |
41 |
public class LineInputStream |
public class LineInputStream extends FilterInputStream |
|
extends FilterInputStream |
|
42 |
{ |
{ |
43 |
|
|
44 |
/* |
/* |
45 |
* Line buffer. |
* Line buffer. |
46 |
*/ |
*/ |
47 |
private ByteArrayOutputStream buf; |
private ByteArrayOutputStream buf; |
48 |
|
|
49 |
/* |
/* |
50 |
* Encoding to use when translating bytes to characters. |
* Encoding to use when translating bytes to characters. |
51 |
*/ |
*/ |
52 |
private String encoding; |
private String encoding; |
53 |
|
|
54 |
/* |
/* |
55 |
* End-of-stream flag. |
* End-of-stream flag. |
56 |
*/ |
*/ |
57 |
private boolean eof; |
private boolean eof; |
58 |
|
|
59 |
/** |
/** |
60 |
* Constructor using the US-ASCII character encoding. |
* Constructor using the US-ASCII character encoding. |
62 |
*/ |
*/ |
63 |
public LineInputStream(InputStream in) |
public LineInputStream(InputStream in) |
64 |
{ |
{ |
65 |
this(in, "US-ASCII"); |
this(in, "US-ASCII"); |
66 |
} |
} |
67 |
|
|
68 |
/** |
/** |
69 |
* Constructor. |
* Constructor. |
70 |
* @param in the underlying input stream |
* @param in the underlying input stream |
72 |
*/ |
*/ |
73 |
public LineInputStream(InputStream in, String encoding) |
public LineInputStream(InputStream in, String encoding) |
74 |
{ |
{ |
75 |
super(in); |
super(in); |
76 |
buf = new ByteArrayOutputStream(); |
buf = new ByteArrayOutputStream(); |
77 |
this.encoding = encoding; |
this.encoding = encoding; |
78 |
eof = false; |
eof = false; |
79 |
} |
} |
80 |
|
|
81 |
/** |
/** |
82 |
* Read a line of input. |
* Read a line of input. |
83 |
*/ |
*/ |
84 |
public String readLine() |
public String readLine() throws IOException |
|
throws IOException |
|
85 |
{ |
{ |
86 |
if (eof) |
if (eof) |
87 |
return null; |
return null; |
88 |
do |
do |
89 |
{ |
{ |
90 |
int c = in.read(); |
int c = in.read(); |
91 |
switch (c) |
switch (c) |
92 |
{ |
{ |
93 |
case -1: |
case -1: |
94 |
eof = true; |
eof = true; |
95 |
case 10: // LF |
case 10: // LF |
96 |
String ret = buf.toString(encoding); |
String ret = buf.toString(encoding); |
97 |
buf.reset(); |
buf.reset(); |
98 |
return ret; |
return ret; |
99 |
default: |
default:buf.write(c); |
100 |
buf.write(c); |
} |
101 |
} |
} |
102 |
} while (true); |
while (true); |
103 |
} |
} |
104 |
|
|
105 |
} |
} |