Fix incorrect bytesBefore/indexOf() in ReplayingDecoderBuffer

Motivation:

bytesBefore(length, ...), bytesBefore(index, length, ...), and
indexOf(fromIndex, toIndex,...) in ReplayingDecoderBuffer are buggy.
They trigger 'REPLAY even when they don't need to.

Modification:

Implement the buggy methods properly so that REPLAYs are not triggered
unnecessarily.

Result:

Correct behvaior
This commit is contained in:
Trustin Lee 2014-06-26 18:53:50 +09:00
parent 4d2b78ca3c
commit d2e385acc2

View File

@ -307,11 +307,15 @@ final class ReplayingDecoderBuffer extends ByteBuf {
@Override
public int indexOf(int fromIndex, int toIndex, byte value) {
int endIndex = buffer.indexOf(fromIndex, toIndex, value);
if (endIndex < 0) {
if (fromIndex == toIndex) {
return -1;
}
if (Math.max(fromIndex, toIndex) > buffer.writerIndex()) {
throw REPLAY;
}
return endIndex;
return buffer.indexOf(fromIndex, toIndex, value);
}
@Override
@ -325,21 +329,27 @@ final class ReplayingDecoderBuffer extends ByteBuf {
@Override
public int bytesBefore(int length, byte value) {
checkReadableBytes(length);
int bytes = buffer.bytesBefore(length, value);
if (bytes < 0) {
throw REPLAY;
}
return bytes;
final int readerIndex = buffer.readerIndex();
return bytesBefore(readerIndex, buffer.writerIndex() - readerIndex, value);
}
@Override
public int bytesBefore(int index, int length, byte value) {
int bytes = buffer.bytesBefore(index, length, value);
if (bytes < 0) {
final int writerIndex = buffer.writerIndex();
if (index >= writerIndex) {
throw REPLAY;
}
return bytes;
if (index <= writerIndex - length) {
return buffer.bytesBefore(index, length, value);
}
int res = buffer.bytesBefore(index, writerIndex - index, value);
if (res < 0) {
throw REPLAY;
} else {
return res;
}
}
@Override
@ -359,7 +369,7 @@ final class ReplayingDecoderBuffer extends ByteBuf {
throw REPLAY;
}
if (index + length <= writerIndex) {
if (index <= writerIndex - length) {
return buffer.forEachByte(index, length, processor);
}