[#1875] Correctly check the readerIndex when try to read a byte from AbstractByteBuf

This commit is contained in:
Norman Maurer 2013-09-30 14:47:49 +02:00
parent b4fa8af079
commit 6d09e57be7
2 changed files with 8 additions and 0 deletions

View File

@ -559,6 +559,7 @@ public abstract class AbstractByteBuf extends ByteBuf {
@Override @Override
public byte readByte() { public byte readByte() {
checkReadableBytes(1);
int i = readerIndex; int i = readerIndex;
byte b = getByte(i); byte b = getByte(i);
readerIndex = i + 1; readerIndex = i + 1;

View File

@ -1935,6 +1935,13 @@ public abstract class AbstractByteBufTest {
assertNull(cause.get()); assertNull(cause.get());
} }
@Test(expected = IndexOutOfBoundsException.class)
public void readByteThrowsIndexOutOfBoundsException() {
final ByteBuf buffer = freeLater(newBuffer(8));
buffer.writeByte(0);
assertEquals((byte) 0 , buffer.readByte());
buffer.readByte();
}
static final class TestGatheringByteChannel implements GatheringByteChannel { static final class TestGatheringByteChannel implements GatheringByteChannel {
private final ByteArrayOutputStream out = new ByteArrayOutputStream(); private final ByteArrayOutputStream out = new ByteArrayOutputStream();
private final WritableByteChannel channel = Channels.newChannel(out); private final WritableByteChannel channel = Channels.newChannel(out);