Port the changes in SpdyHeaderBlockZlibEncoder to master & 4.0

Somehow it slipped away.
This commit is contained in:
Trustin Lee 2014-01-13 23:00:41 +09:00
parent cead74f84d
commit b8de47484a

View File

@ -24,13 +24,12 @@ import static io.netty.handler.codec.spdy.SpdyCodecUtil.*;
class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder { class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
private final byte[] out = new byte[8192];
private final Deflater compressor; private final Deflater compressor;
private boolean finished; private boolean finished;
SpdyHeaderBlockZlibEncoder(SpdyVersion version, int compressionLevel) { SpdyHeaderBlockZlibEncoder(SpdyVersion spdyVersion, int compressionLevel) {
super(version); super(spdyVersion);
if (compressionLevel < 0 || compressionLevel > 9) { if (compressionLevel < 0 || compressionLevel > 9) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
"compressionLevel: " + compressionLevel + " (expected: 0-9)"); "compressionLevel: " + compressionLevel + " (expected: 0-9)");
@ -39,20 +38,36 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
compressor.setDictionary(SPDY_DICT); compressor.setDictionary(SPDY_DICT);
} }
private void setInput(ByteBuf decompressed) { private int setInput(ByteBuf decompressed) {
byte[] in = new byte[decompressed.readableBytes()]; int len = decompressed.readableBytes();
decompressed.readBytes(in);
compressor.setInput(in); if (decompressed.hasArray()) {
compressor.setInput(decompressed.array(), decompressed.arrayOffset() + decompressed.readerIndex(), len);
} else {
byte[] in = new byte[len];
decompressed.getBytes(decompressed.readerIndex(), in);
compressor.setInput(in, 0, in.length);
}
return len;
} }
private void encode(ByteBuf compressed) { private void encode(ByteBuf compressed) {
int numBytes = out.length; while (compressInto(compressed)) {
while (numBytes == out.length) { // Although unlikely, it's possible that the compressed size is larger than the decompressed size
numBytes = compressor.deflate(out, 0, out.length, Deflater.SYNC_FLUSH); compressed.ensureWritable(compressed.capacity() << 1);
compressed.writeBytes(out, 0, numBytes);
} }
} }
private boolean compressInto(ByteBuf compressed) {
byte[] out = compressed.array();
int off = compressed.arrayOffset() + compressed.writerIndex();
int toWrite = compressed.writableBytes();
int numBytes = compressor.deflate(out, off, toWrite, Deflater.SYNC_FLUSH);
compressed.writerIndex(compressed.writerIndex() + numBytes);
return numBytes == toWrite;
}
@Override @Override
public ByteBuf encode(SpdyHeadersFrame frame) throws Exception { public ByteBuf encode(SpdyHeadersFrame frame) throws Exception {
if (frame == null) { if (frame == null) {
@ -68,9 +83,11 @@ class SpdyHeaderBlockZlibEncoder extends SpdyHeaderBlockRawEncoder {
return Unpooled.EMPTY_BUFFER; return Unpooled.EMPTY_BUFFER;
} }
ByteBuf compressed = decompressed.alloc().buffer(); ByteBuf compressed = decompressed.alloc().heapBuffer(decompressed.readableBytes());
setInput(decompressed); int len = setInput(decompressed);
encode(compressed); encode(compressed);
decompressed.skipBytes(len);
return compressed; return compressed;
} }