Fix public int read() throws IOException method exceeds the limit of length (#9306)
Motivation: buffer.isReadable() should not be used to limit the amount of data that can be read as the amount may be less then was is readable. Modification: - Use available() which takes the length into account - Add unit test Result: Fixes https://github.com/netty/netty/issues/9305
This commit is contained in:
parent
e0094b2f89
commit
c0f1e9bd21
@ -165,7 +165,8 @@ public class ByteBufInputStream extends InputStream implements DataInput {
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
if (!buffer.isReadable()) {
|
||||
int available = available();
|
||||
if (available == 0) {
|
||||
return -1;
|
||||
}
|
||||
return buffer.readByte() & 0xff;
|
||||
|
@ -212,4 +212,39 @@ public class ByteBufStreamTest {
|
||||
assertEquals(charCount, count);
|
||||
in.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRead() throws Exception {
|
||||
// case1
|
||||
ByteBuf buf = Unpooled.buffer(16);
|
||||
buf.writeBytes(new byte[]{1, 2, 3, 4, 5, 6});
|
||||
|
||||
ByteBufInputStream in = new ByteBufInputStream(buf, 3);
|
||||
|
||||
assertEquals(1, in.read());
|
||||
assertEquals(2, in.read());
|
||||
assertEquals(3, in.read());
|
||||
assertEquals(-1, in.read());
|
||||
assertEquals(-1, in.read());
|
||||
assertEquals(-1, in.read());
|
||||
|
||||
buf.release();
|
||||
in.close();
|
||||
|
||||
// case2
|
||||
ByteBuf buf2 = Unpooled.buffer(16);
|
||||
buf2.writeBytes(new byte[]{1, 2, 3, 4, 5, 6});
|
||||
|
||||
ByteBufInputStream in2 = new ByteBufInputStream(buf2, 4);
|
||||
|
||||
assertEquals(1, in2.read());
|
||||
assertEquals(2, in2.read());
|
||||
assertEquals(3, in2.read());
|
||||
assertEquals(4, in2.read());
|
||||
assertNotEquals(5, in2.read());
|
||||
assertEquals(-1, in2.read());
|
||||
|
||||
buf2.release();
|
||||
in2.close();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user