[#964] ByteBuf.readLine() must return null when no more data is available in ByteBuf
This commit is contained in:
parent
a91887cda7
commit
7b6cbdbb1e
@ -188,6 +188,9 @@ public class ByteBufInputStream extends InputStream implements DataInput {
|
|||||||
lineBuf.setLength(0);
|
lineBuf.setLength(0);
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int b = read();
|
int b = read();
|
||||||
|
if (b == -1 && lineBuf.length() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
if (b < 0 || b == '\n') {
|
if (b < 0 || b == '\n') {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -18,6 +18,7 @@ package io.netty.buffer;
|
|||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
|
||||||
import java.io.EOFException;
|
import java.io.EOFException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
@ -170,13 +171,34 @@ public class ChannelBufferStreamTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEmptyReadLine() throws Exception {
|
public void testReadLine() throws Exception {
|
||||||
ByteBuf buf = Unpooled.buffer(0);
|
Charset utf8=Charset.forName("UTF-8");
|
||||||
|
ByteBuf buf = Unpooled.buffer();
|
||||||
ByteBufInputStream in = new ByteBufInputStream(buf);
|
ByteBufInputStream in = new ByteBufInputStream(buf);
|
||||||
|
|
||||||
String s = in.readLine();
|
String s = in.readLine();
|
||||||
assertEquals(0, s.length());
|
assertNull(s);
|
||||||
|
|
||||||
|
int charCount=5;//total chars in the string below without new line characters
|
||||||
|
byte[] abc = "a\nb\r\nc\nd\ne".getBytes(utf8);
|
||||||
|
buf.writeBytes(abc);
|
||||||
|
in.mark(charCount);
|
||||||
|
assertEquals("a",in.readLine());
|
||||||
|
assertEquals("b",in.readLine());
|
||||||
|
assertEquals("c",in.readLine());
|
||||||
|
assertEquals("d",in.readLine());
|
||||||
|
assertEquals("e",in.readLine());
|
||||||
|
assertNull(in.readLine());
|
||||||
|
|
||||||
|
in.reset();
|
||||||
|
int count=0;
|
||||||
|
while(in.readLine() != null){
|
||||||
|
++count;
|
||||||
|
if(count > charCount){
|
||||||
|
fail("readLine() should have returned null");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertEquals(charCount,count);
|
||||||
in.close();
|
in.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user