Zlib decoder calls reduction and index fix

Motivation:
The JdkZlibDecoder and JZlibDecoder call isReadable and readableBytes in the same method. There is an opportunity to reduce the number of methods calls to just use readableBytes.  JdkZlibDecoder reads from a ByteBuf with an absolute index instead of using readerIndex()

Modifications:
- Use readableBytes where isReadable was used
- Correct absolute ByteBuf index to be relative to readerIndex()

Result:
Less method calls duplicating work and preventing an index out of bounds exception.
This commit is contained in:
Scott Mitchell 2015-01-26 10:11:23 -05:00 committed by Norman Maurer
parent bc97d7d7a1
commit a9577c0a4b
2 changed files with 10 additions and 8 deletions

View File

@ -90,13 +90,13 @@ public class JZlibDecoder extends ZlibDecoder {
return;
}
if (!in.isReadable()) {
final int inputLength = in.readableBytes();
if (inputLength == 0) {
return;
}
try {
// Configure input.
int inputLength = in.readableBytes();
z.avail_in = inputLength;
if (in.hasArray()) {
z.next_in = in.array();

View File

@ -125,17 +125,18 @@ public class JdkZlibDecoder extends ZlibDecoder {
return;
}
if (!in.isReadable()) {
int readableBytes = in.readableBytes();
if (readableBytes == 0) {
return;
}
if (decideZlibOrNone) {
// First two bytes are needed to decide if it's a ZLIB stream.
if (in.readableBytes() < 2) {
if (readableBytes < 2) {
return;
}
boolean nowrap = !looksLikeZlib(in.getShort(0));
boolean nowrap = !looksLikeZlib(in.getShort(in.readerIndex()));
inflater = new Inflater(nowrap);
decideZlibOrNone = false;
}
@ -154,13 +155,14 @@ public class JdkZlibDecoder extends ZlibDecoder {
}
}
}
// Some bytes may have been consumed, and so we must re-set the number of readable bytes.
readableBytes = in.readableBytes();
}
int readableBytes = in.readableBytes();
if (in.hasArray()) {
inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), in.readableBytes());
inflater.setInput(in.array(), in.arrayOffset() + in.readerIndex(), readableBytes);
} else {
byte[] array = new byte[in.readableBytes()];
byte[] array = new byte[readableBytes];
in.getBytes(in.readerIndex(), array);
inflater.setInput(array);
}