netty-1597: Rewrite ByteBufInputStream.readLine() to avoid IndexOutOfBoundsException and to behave more correctly for lines ending in '\r'.
This commit is contained in:
parent
6c9c151d66
commit
674f4bce51
|
@ -186,21 +186,25 @@ public class ByteBufInputStream extends InputStream implements DataInput {
|
|||
@Override
|
||||
public String readLine() throws IOException {
|
||||
lineBuf.setLength(0);
|
||||
for (;;) {
|
||||
int b = read();
|
||||
if (b == -1 && lineBuf.length() == 0) {
|
||||
return null;
|
||||
}
|
||||
if (b < 0 || b == '\n') {
|
||||
break;
|
||||
|
||||
loop: while (true) {
|
||||
if (!buffer.isReadable()) {
|
||||
return (lineBuf.length() > 0) ? lineBuf.toString() : null;
|
||||
}
|
||||
|
||||
lineBuf.append((char) b);
|
||||
}
|
||||
int c = buffer.readUnsignedByte();
|
||||
switch (c) {
|
||||
case '\n':
|
||||
break loop;
|
||||
|
||||
if (lineBuf.length() > 0) {
|
||||
while (lineBuf.charAt(lineBuf.length() - 1) == '\r') {
|
||||
lineBuf.setLength(lineBuf.length() - 1);
|
||||
case '\r':
|
||||
if (buffer.isReadable() && buffer.getUnsignedByte(buffer.readerIndex()) == '\n') {
|
||||
buffer.skipBytes(1);
|
||||
}
|
||||
break loop;
|
||||
|
||||
default:
|
||||
lineBuf.append((char) c);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -134,6 +134,7 @@ public class ByteBufStreamTest {
|
|||
|
||||
assertEquals("Hello, World!", in.readUTF());
|
||||
assertEquals("The first line", in.readLine());
|
||||
assertEquals("", in.readLine());
|
||||
|
||||
assertEquals(4, in.read(tmp));
|
||||
assertEquals(1, tmp[0]);
|
||||
|
|
Loading…
Reference in New Issue
Block a user