My favorites | Sign in
Project Logo
       
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/* This code is part of Freenet. It is distributed under the GNU General
* Public License, version 2 (or at your option any later version). See
* http://www.gnu.org/ for further details of the GPL. */
package freenet.support.io;

import freenet.support.HexUtil;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* A FilterInputStream which provides readLine().
*/
public class LineReadingInputStream extends FilterInputStream implements LineReader {

public LineReadingInputStream(InputStream in) {
super(in);
}

/**
* Read a \n or \r\n terminated line of UTF-8 or ISO-8859-1.
* @param maxLength The maximum length of a line. If a line is longer than this, we throw IOException rather
* than keeping on reading it forever.
* @param bufferSize The initial size of the read buffer.
* @param utf If true, read as UTF-8, if false, read as ISO-8859-1.
*/
public String readLine(int maxLength, int bufferSize, boolean utf) throws IOException {
if(maxLength < 1)
return null;
if(maxLength <= bufferSize)
bufferSize = maxLength + 1; // Buffer too big, shrink it (add 1 for the optional \r)

if(!markSupported())
return readLineWithoutMarking(maxLength, bufferSize, utf);

byte[] buf = new byte[Math.max(Math.min(128, maxLength), Math.min(1024, bufferSize))];
int ctr = 0;
mark(maxLength + 2); // in case we have both a \r and a \n
while(true) {
int x = read(buf, ctr, buf.length - ctr);
if(x == -1) {
if(ctr == 0)
return null;
return new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1");
}
// REDFLAG this is definitely safe with the above charsets, it may not be safe with some wierd ones.
int end = ctr + x;
for(; ctr < end; ctr++) {
if(buf[ctr] == '\n') {
String toReturn = "";
if(ctr != 0) {
boolean removeCR = (buf[ctr - 1] == '\r');
toReturn = new String(buf, 0, (removeCR ? ctr - 1 : ctr), utf ? "UTF-8" : "ISO-8859-1");
}
reset();
skip(ctr + 1);
return toReturn;
}
if(ctr >= maxLength)
throw new TooLongException("We reached maxLength="+maxLength+ " parsing\n "+HexUtil.bytesToHex(buf, 0, ctr) + "\n" + new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1"));
}
if((buf.length < maxLength) && (buf.length - ctr < bufferSize)) {
byte[] newBuf = new byte[Math.min(buf.length * 2, maxLength)];
System.arraycopy(buf, 0, newBuf, 0, ctr);
buf = newBuf;
}
}
}

public String readLineWithoutMarking(int maxLength, int bufferSize, boolean utf) throws IOException {
if(maxLength < bufferSize)
bufferSize = maxLength + 1; // Buffer too big, shrink it (add 1 for the optional \r)
byte[] buf = new byte[Math.max(Math.min(128, maxLength), Math.min(1024, bufferSize))];
int ctr = 0;
while(true) {
int x = read();
if(x == -1) {
if(ctr == 0)
return null;
return new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1");
}
// REDFLAG this is definitely safe with the above charsets, it may not be safe with some wierd ones.
if(x == '\n') {
if(ctr == 0)
return "";
if(buf[ctr - 1] == '\r')
ctr--;
return new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1");
}
if(ctr >= maxLength)
throw new TooLongException("We reached maxLength="+maxLength+ " parsing\n "+HexUtil.bytesToHex(buf, 0, ctr) + "\n" + new String(buf, 0, ctr, utf ? "UTF-8" : "ISO-8859-1"));
if(ctr >= buf.length) {
byte[] newBuf = new byte[Math.min(buf.length * 2, maxLength)];
System.arraycopy(buf, 0, newBuf, 0, buf.length);
buf = newBuf;
}
buf[ctr++] = (byte) x;
}
}
}
Show details Hide details

Change log

r25844 by nextgens on Feb 26, 2009   Diff
maybe fix LineReadingInputStream:
1) get rid of the potential
ArrayIndexOutOfBounds toad has spotted
2) optimize the trivial case where
maxLength < 1
3) paranoia
Go to: 
Project members, sign in to write a code review

Older revisions

r25398 by nextgens on Jan 30, 2009   Diff
doh2!
r25397 by nextgens on Jan 30, 2009   Diff
doh!
r22098 by nextgens on Aug 22, 2008   Diff
LineReadingInputStream: toad's right:
maxLength is in bytes not in chars
All revisions of this file

File info

Size: 3619 bytes, 100 lines
Hosted by Google Code