diff --git a/buffer/src/main/java/io/netty/buffer/ChannelBufferInputStream.java b/buffer/src/main/java/io/netty/buffer/ChannelBufferInputStream.java index 63c14f1109..0fd6d52d97 100644 --- a/buffer/src/main/java/io/netty/buffer/ChannelBufferInputStream.java +++ b/buffer/src/main/java/io/netty/buffer/ChannelBufferInputStream.java @@ -195,7 +195,8 @@ public class ChannelBufferInputStream extends InputStream implements DataInput { lineBuf.append((char) b); } - while (lineBuf.charAt(lineBuf.length() - 1) == '\r') { + while ( lineBuf.length() > 0 && + lineBuf.charAt(lineBuf.length() - 1) == '\r') { lineBuf.setLength(lineBuf.length() - 1); } diff --git a/buffer/src/test/java/io/netty/buffer/ChannelBufferStreamTest.java b/buffer/src/test/java/io/netty/buffer/ChannelBufferStreamTest.java index 5ddd657ec3..2474c630e4 100644 --- a/buffer/src/test/java/io/netty/buffer/ChannelBufferStreamTest.java +++ b/buffer/src/test/java/io/netty/buffer/ChannelBufferStreamTest.java @@ -168,4 +168,15 @@ public class ChannelBufferStreamTest { assertEquals(buf.readerIndex(), in.readBytes()); } + + @Test + public void testEmptyReadLine() throws Exception { + ChannelBuffer buf = ChannelBuffers.buffer(0); + ChannelBufferInputStream in = new ChannelBufferInputStream(buf); + + String s = in.readLine(); + assertEquals(0, s.length()); + + in.close(); + } }