From a6d7105761a083230bb4d2aac7e58ca5fb07a196 Mon Sep 17 00:00:00 2001 From: Sun Ning Date: Thu, 24 May 2012 14:33:19 +0800 Subject: [PATCH] fix #360, add check for empty buffer; also add unit test for this scenario --- .../io/netty/buffer/ChannelBufferInputStream.java | 3 ++- .../java/io/netty/buffer/ChannelBufferStreamTest.java | 11 +++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) 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(); + } }