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 c4d94a52cd
commit bfd7e510ac

View File

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