Remove support for JZlib (#11058)
Motivation: This library is obsolete; hasn't been updated since 2013. Modification: Remove jzlib dependency, integration code and tests. Result: - No more jzlib support. - Less code. - The JdkZlib* code can now be simplified because it no longer share anything with jzlib.
This commit is contained in:
parent
2ce03e0a08
commit
bfea65ef52
@ -63,11 +63,6 @@
|
||||
<artifactId>jboss-marshalling</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jcraft</groupId>
|
||||
<artifactId>jzlib</artifactId>
|
||||
<optional>true</optional>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.ning</groupId>
|
||||
<artifactId>compress-lzf</artifactId>
|
||||
|
@ -1,218 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
import com.jcraft.jzlib.Inflater;
|
||||
import com.jcraft.jzlib.JZlib;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
public class JZlibDecoder extends ZlibDecoder {
|
||||
|
||||
private final Inflater z = new Inflater();
|
||||
private byte[] dictionary;
|
||||
private volatile boolean finished;
|
||||
|
||||
/**
|
||||
* Creates a new instance with the default wrapper ({@link ZlibWrapper#ZLIB}).
|
||||
*
|
||||
* @throws DecompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibDecoder() {
|
||||
this(ZlibWrapper.ZLIB, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the default wrapper ({@link ZlibWrapper#ZLIB})
|
||||
* and specified maximum buffer allocation.
|
||||
*
|
||||
* @param maxAllocation
|
||||
* Maximum size of the decompression buffer. Must be >= 0.
|
||||
* If zero, maximum size is decided by the {@link ByteBufAllocator}.
|
||||
*
|
||||
* @throws DecompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibDecoder(int maxAllocation) {
|
||||
this(ZlibWrapper.ZLIB, maxAllocation);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified wrapper.
|
||||
*
|
||||
* @throws DecompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibDecoder(ZlibWrapper wrapper) {
|
||||
this(wrapper, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified wrapper and maximum buffer allocation.
|
||||
*
|
||||
* @param maxAllocation
|
||||
* Maximum size of the decompression buffer. Must be >= 0.
|
||||
* If zero, maximum size is decided by the {@link ByteBufAllocator}.
|
||||
*
|
||||
* @throws DecompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibDecoder(ZlibWrapper wrapper, int maxAllocation) {
|
||||
super(maxAllocation);
|
||||
|
||||
requireNonNull(wrapper, "wrapper");
|
||||
|
||||
int resultCode = z.init(ZlibUtil.convertWrapperType(wrapper));
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "initialization failure", resultCode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified preset dictionary. The wrapper
|
||||
* is always {@link ZlibWrapper#ZLIB} because it is the only format that
|
||||
* supports the preset dictionary.
|
||||
*
|
||||
* @throws DecompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibDecoder(byte[] dictionary) {
|
||||
this(dictionary, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new instance with the specified preset dictionary and maximum buffer allocation.
|
||||
* The wrapper is always {@link ZlibWrapper#ZLIB} because it is the only format that
|
||||
* supports the preset dictionary.
|
||||
*
|
||||
* @param maxAllocation
|
||||
* Maximum size of the decompression buffer. Must be >= 0.
|
||||
* If zero, maximum size is decided by the {@link ByteBufAllocator}.
|
||||
*
|
||||
* @throws DecompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibDecoder(byte[] dictionary, int maxAllocation) {
|
||||
super(maxAllocation);
|
||||
this.dictionary = requireNonNull(dictionary, "dictionary");
|
||||
int resultCode;
|
||||
resultCode = z.inflateInit(JZlib.W_ZLIB);
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "initialization failure", resultCode);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns {@code true} if and only if the end of the compressed stream
|
||||
* has been reached.
|
||||
*/
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decode(ChannelHandlerContext ctx, ByteBuf in) throws Exception {
|
||||
if (finished) {
|
||||
// Skip data received after finished.
|
||||
in.skipBytes(in.readableBytes());
|
||||
return;
|
||||
}
|
||||
|
||||
final int inputLength = in.readableBytes();
|
||||
if (inputLength == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Configure input.
|
||||
z.avail_in = inputLength;
|
||||
if (in.hasArray()) {
|
||||
z.next_in = in.array();
|
||||
z.next_in_index = in.arrayOffset() + in.readerIndex();
|
||||
} else {
|
||||
byte[] array = new byte[inputLength];
|
||||
in.getBytes(in.readerIndex(), array);
|
||||
z.next_in = array;
|
||||
z.next_in_index = 0;
|
||||
}
|
||||
final int oldNextInIndex = z.next_in_index;
|
||||
|
||||
// Configure output.
|
||||
ByteBuf decompressed = prepareDecompressBuffer(ctx, null, inputLength << 1);
|
||||
|
||||
try {
|
||||
loop: for (;;) {
|
||||
decompressed = prepareDecompressBuffer(ctx, decompressed, z.avail_in << 1);
|
||||
z.avail_out = decompressed.writableBytes();
|
||||
z.next_out = decompressed.array();
|
||||
z.next_out_index = decompressed.arrayOffset() + decompressed.writerIndex();
|
||||
int oldNextOutIndex = z.next_out_index;
|
||||
|
||||
// Decompress 'in' into 'out'
|
||||
int resultCode = z.inflate(JZlib.Z_SYNC_FLUSH);
|
||||
int outputLength = z.next_out_index - oldNextOutIndex;
|
||||
if (outputLength > 0) {
|
||||
decompressed.writerIndex(decompressed.writerIndex() + outputLength);
|
||||
}
|
||||
|
||||
switch (resultCode) {
|
||||
case JZlib.Z_NEED_DICT:
|
||||
if (dictionary == null) {
|
||||
ZlibUtil.fail(z, "decompression failure", resultCode);
|
||||
} else {
|
||||
resultCode = z.inflateSetDictionary(dictionary, dictionary.length);
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case JZlib.Z_STREAM_END:
|
||||
finished = true; // Do not decode anymore.
|
||||
z.inflateEnd();
|
||||
break loop;
|
||||
case JZlib.Z_OK:
|
||||
break;
|
||||
case JZlib.Z_BUF_ERROR:
|
||||
if (z.avail_in <= 0) {
|
||||
break loop;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ZlibUtil.fail(z, "decompression failure", resultCode);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
in.skipBytes(z.next_in_index - oldNextInIndex);
|
||||
if (decompressed.isReadable()) {
|
||||
ctx.fireChannelRead(decompressed);
|
||||
} else {
|
||||
decompressed.release();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
// Deference the external references explicitly to tell the VM that
|
||||
// the allocated byte arrays are temporary so that the call stack
|
||||
// can be utilized.
|
||||
// I'm not sure if the modern VMs do this optimization though.
|
||||
z.next_in = null;
|
||||
z.next_out = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void decompressionBufferExhausted(ByteBuf buffer) {
|
||||
finished = true;
|
||||
}
|
||||
}
|
@ -1,399 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
import static java.util.Objects.requireNonNull;
|
||||
|
||||
import com.jcraft.jzlib.Deflater;
|
||||
import com.jcraft.jzlib.JZlib;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.channel.ChannelFutureListener;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.channel.ChannelPromise;
|
||||
import io.netty.channel.ChannelPromiseNotifier;
|
||||
import io.netty.util.concurrent.EventExecutor;
|
||||
import io.netty.util.internal.EmptyArrays;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* Compresses a {@link ByteBuf} using the deflate algorithm.
|
||||
*/
|
||||
public class JZlibEncoder extends ZlibEncoder {
|
||||
|
||||
private final int wrapperOverhead;
|
||||
private final Deflater z = new Deflater();
|
||||
private volatile boolean finished;
|
||||
private volatile ChannelHandlerContext ctx;
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the default compression level ({@code 6}),
|
||||
* default window bits ({@code 15}), default memory level ({@code 8}),
|
||||
* and the default wrapper ({@link ZlibWrapper#ZLIB}).
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder() {
|
||||
this(6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the specified {@code compressionLevel},
|
||||
* default window bits ({@code 15}), default memory level ({@code 8}),
|
||||
* and the default wrapper ({@link ZlibWrapper#ZLIB}).
|
||||
*
|
||||
* @param compressionLevel
|
||||
* {@code 1} yields the fastest compression and {@code 9} yields the
|
||||
* best compression. {@code 0} means no compression. The default
|
||||
* compression level is {@code 6}.
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(int compressionLevel) {
|
||||
this(ZlibWrapper.ZLIB, compressionLevel);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the default compression level ({@code 6}),
|
||||
* default window bits ({@code 15}), default memory level ({@code 8}),
|
||||
* and the specified wrapper.
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(ZlibWrapper wrapper) {
|
||||
this(wrapper, 6);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the specified {@code compressionLevel},
|
||||
* default window bits ({@code 15}), default memory level ({@code 8}),
|
||||
* and the specified wrapper.
|
||||
*
|
||||
* @param compressionLevel
|
||||
* {@code 1} yields the fastest compression and {@code 9} yields the
|
||||
* best compression. {@code 0} means no compression. The default
|
||||
* compression level is {@code 6}.
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
|
||||
this(wrapper, compressionLevel, 15, 8);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the specified {@code compressionLevel},
|
||||
* the specified {@code windowBits}, the specified {@code memLevel}, and
|
||||
* the specified wrapper.
|
||||
*
|
||||
* @param compressionLevel
|
||||
* {@code 1} yields the fastest compression and {@code 9} yields the
|
||||
* best compression. {@code 0} means no compression. The default
|
||||
* compression level is {@code 6}.
|
||||
* @param windowBits
|
||||
* The base two logarithm of the size of the history buffer. The
|
||||
* value should be in the range {@code 9} to {@code 15} inclusive.
|
||||
* Larger values result in better compression at the expense of
|
||||
* memory usage. The default value is {@code 15}.
|
||||
* @param memLevel
|
||||
* How much memory should be allocated for the internal compression
|
||||
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
|
||||
* memory. Larger values result in better and faster compression
|
||||
* at the expense of memory usage. The default value is {@code 8}
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
|
||||
|
||||
if (compressionLevel < 0 || compressionLevel > 9) {
|
||||
throw new IllegalArgumentException(
|
||||
"compressionLevel: " + compressionLevel +
|
||||
" (expected: 0-9)");
|
||||
}
|
||||
if (windowBits < 9 || windowBits > 15) {
|
||||
throw new IllegalArgumentException(
|
||||
"windowBits: " + windowBits + " (expected: 9-15)");
|
||||
}
|
||||
if (memLevel < 1 || memLevel > 9) {
|
||||
throw new IllegalArgumentException(
|
||||
"memLevel: " + memLevel + " (expected: 1-9)");
|
||||
}
|
||||
requireNonNull(wrapper, "wrapper");
|
||||
if (wrapper == ZlibWrapper.ZLIB_OR_NONE) {
|
||||
throw new IllegalArgumentException(
|
||||
"wrapper '" + ZlibWrapper.ZLIB_OR_NONE + "' is not " +
|
||||
"allowed for compression.");
|
||||
}
|
||||
|
||||
int resultCode = z.init(
|
||||
compressionLevel, windowBits, memLevel,
|
||||
ZlibUtil.convertWrapperType(wrapper));
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "initialization failure", resultCode);
|
||||
}
|
||||
|
||||
wrapperOverhead = ZlibUtil.wrapperOverhead(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the default compression level ({@code 6}),
|
||||
* default window bits ({@code 15}), default memory level ({@code 8}),
|
||||
* and the specified preset dictionary. The wrapper is always
|
||||
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
|
||||
* the preset dictionary.
|
||||
*
|
||||
* @param dictionary the preset dictionary
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(byte[] dictionary) {
|
||||
this(6, dictionary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the specified {@code compressionLevel},
|
||||
* default window bits ({@code 15}), default memory level ({@code 8}),
|
||||
* and the specified preset dictionary. The wrapper is always
|
||||
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
|
||||
* the preset dictionary.
|
||||
*
|
||||
* @param compressionLevel
|
||||
* {@code 1} yields the fastest compression and {@code 9} yields the
|
||||
* best compression. {@code 0} means no compression. The default
|
||||
* compression level is {@code 6}.
|
||||
* @param dictionary the preset dictionary
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(int compressionLevel, byte[] dictionary) {
|
||||
this(compressionLevel, 15, 8, dictionary);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new zlib encoder with the specified {@code compressionLevel},
|
||||
* the specified {@code windowBits}, the specified {@code memLevel},
|
||||
* and the specified preset dictionary. The wrapper is always
|
||||
* {@link ZlibWrapper#ZLIB} because it is the only format that supports
|
||||
* the preset dictionary.
|
||||
*
|
||||
* @param compressionLevel
|
||||
* {@code 1} yields the fastest compression and {@code 9} yields the
|
||||
* best compression. {@code 0} means no compression. The default
|
||||
* compression level is {@code 6}.
|
||||
* @param windowBits
|
||||
* The base two logarithm of the size of the history buffer. The
|
||||
* value should be in the range {@code 9} to {@code 15} inclusive.
|
||||
* Larger values result in better compression at the expense of
|
||||
* memory usage. The default value is {@code 15}.
|
||||
* @param memLevel
|
||||
* How much memory should be allocated for the internal compression
|
||||
* state. {@code 1} uses minimum memory and {@code 9} uses maximum
|
||||
* memory. Larger values result in better and faster compression
|
||||
* at the expense of memory usage. The default value is {@code 8}
|
||||
* @param dictionary the preset dictionary
|
||||
*
|
||||
* @throws CompressionException if failed to initialize zlib
|
||||
*/
|
||||
public JZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
|
||||
if (compressionLevel < 0 || compressionLevel > 9) {
|
||||
throw new IllegalArgumentException("compressionLevel: " + compressionLevel + " (expected: 0-9)");
|
||||
}
|
||||
if (windowBits < 9 || windowBits > 15) {
|
||||
throw new IllegalArgumentException(
|
||||
"windowBits: " + windowBits + " (expected: 9-15)");
|
||||
}
|
||||
if (memLevel < 1 || memLevel > 9) {
|
||||
throw new IllegalArgumentException(
|
||||
"memLevel: " + memLevel + " (expected: 1-9)");
|
||||
}
|
||||
requireNonNull(dictionary, "dictionary");
|
||||
int resultCode;
|
||||
resultCode = z.deflateInit(
|
||||
compressionLevel, windowBits, memLevel,
|
||||
JZlib.W_ZLIB); // Default: ZLIB format
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "initialization failure", resultCode);
|
||||
} else {
|
||||
resultCode = z.deflateSetDictionary(dictionary, dictionary.length);
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "failed to set the dictionary", resultCode);
|
||||
}
|
||||
}
|
||||
|
||||
wrapperOverhead = ZlibUtil.wrapperOverhead(ZlibWrapper.ZLIB);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelFuture close() {
|
||||
return close(ctx().channel().newPromise());
|
||||
}
|
||||
|
||||
@Override
|
||||
public ChannelFuture close(final ChannelPromise promise) {
|
||||
ChannelHandlerContext ctx = ctx();
|
||||
EventExecutor executor = ctx.executor();
|
||||
if (executor.inEventLoop()) {
|
||||
return finishEncode(ctx, promise);
|
||||
} else {
|
||||
final ChannelPromise p = ctx.newPromise();
|
||||
executor.execute(() -> {
|
||||
ChannelFuture f = finishEncode(ctx(), p);
|
||||
f.addListener(new ChannelPromiseNotifier(promise));
|
||||
});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
|
||||
private ChannelHandlerContext ctx() {
|
||||
ChannelHandlerContext ctx = this.ctx;
|
||||
if (ctx == null) {
|
||||
throw new IllegalStateException("not added to a pipeline");
|
||||
}
|
||||
return ctx;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isClosed() {
|
||||
return finished;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void encode(ChannelHandlerContext ctx, ByteBuf in, ByteBuf out) throws Exception {
|
||||
if (finished) {
|
||||
out.writeBytes(in);
|
||||
return;
|
||||
}
|
||||
|
||||
int inputLength = in.readableBytes();
|
||||
if (inputLength == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
// Configure input.
|
||||
boolean inHasArray = in.hasArray();
|
||||
z.avail_in = inputLength;
|
||||
if (inHasArray) {
|
||||
z.next_in = in.array();
|
||||
z.next_in_index = in.arrayOffset() + in.readerIndex();
|
||||
} else {
|
||||
byte[] array = new byte[inputLength];
|
||||
in.getBytes(in.readerIndex(), array);
|
||||
z.next_in = array;
|
||||
z.next_in_index = 0;
|
||||
}
|
||||
int oldNextInIndex = z.next_in_index;
|
||||
|
||||
// Configure output.
|
||||
int maxOutputLength = (int) Math.ceil(inputLength * 1.001) + 12 + wrapperOverhead;
|
||||
out.ensureWritable(maxOutputLength);
|
||||
z.avail_out = maxOutputLength;
|
||||
z.next_out = out.array();
|
||||
z.next_out_index = out.arrayOffset() + out.writerIndex();
|
||||
int oldNextOutIndex = z.next_out_index;
|
||||
|
||||
// Note that Z_PARTIAL_FLUSH has been deprecated.
|
||||
int resultCode;
|
||||
try {
|
||||
resultCode = z.deflate(JZlib.Z_SYNC_FLUSH);
|
||||
} finally {
|
||||
in.skipBytes(z.next_in_index - oldNextInIndex);
|
||||
}
|
||||
|
||||
if (resultCode != JZlib.Z_OK) {
|
||||
ZlibUtil.fail(z, "compression failure", resultCode);
|
||||
}
|
||||
|
||||
int outputLength = z.next_out_index - oldNextOutIndex;
|
||||
if (outputLength > 0) {
|
||||
out.writerIndex(out.writerIndex() + outputLength);
|
||||
}
|
||||
} finally {
|
||||
// Deference the external references explicitly to tell the VM that
|
||||
// the allocated byte arrays are temporary so that the call stack
|
||||
// can be utilized.
|
||||
// I'm not sure if the modern VMs do this optimization though.
|
||||
z.next_in = null;
|
||||
z.next_out = null;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close(
|
||||
final ChannelHandlerContext ctx,
|
||||
final ChannelPromise promise) {
|
||||
ChannelFuture f = finishEncode(ctx, ctx.newPromise());
|
||||
f.addListener((ChannelFutureListener) f1 -> ctx.close(promise));
|
||||
|
||||
if (!f.isDone()) {
|
||||
// Ensure the channel is closed even if the write operation completes in time.
|
||||
ctx.executor().schedule(() -> {
|
||||
ctx.close(promise);
|
||||
}, 10, TimeUnit.SECONDS); // FIXME: Magic number
|
||||
}
|
||||
}
|
||||
|
||||
private ChannelFuture finishEncode(ChannelHandlerContext ctx, ChannelPromise promise) {
|
||||
if (finished) {
|
||||
promise.setSuccess();
|
||||
return promise;
|
||||
}
|
||||
finished = true;
|
||||
|
||||
ByteBuf footer;
|
||||
try {
|
||||
// Configure input.
|
||||
z.next_in = EmptyArrays.EMPTY_BYTES;
|
||||
z.next_in_index = 0;
|
||||
z.avail_in = 0;
|
||||
|
||||
// Configure output.
|
||||
byte[] out = new byte[32]; // room for ADLER32 + ZLIB / CRC32 + GZIP header
|
||||
z.next_out = out;
|
||||
z.next_out_index = 0;
|
||||
z.avail_out = out.length;
|
||||
|
||||
// Write the ADLER32 checksum (stream footer).
|
||||
int resultCode = z.deflate(JZlib.Z_FINISH);
|
||||
if (resultCode != JZlib.Z_OK && resultCode != JZlib.Z_STREAM_END) {
|
||||
promise.setFailure(ZlibUtil.deflaterException(z, "compression failure", resultCode));
|
||||
return promise;
|
||||
} else if (z.next_out_index != 0) { // lgtm[java/constant-comparison]
|
||||
// Suppressed a warning above to be on the safe side
|
||||
// even if z.next_out_index seems to be always 0 here
|
||||
footer = Unpooled.wrappedBuffer(out, 0, z.next_out_index);
|
||||
} else {
|
||||
footer = Unpooled.EMPTY_BUFFER;
|
||||
}
|
||||
} finally {
|
||||
z.deflateEnd();
|
||||
|
||||
// Deference the external references explicitly to tell the VM that
|
||||
// the allocated byte arrays are temporary so that the call stack
|
||||
// can be utilized.
|
||||
// I'm not sure if the modern VMs do this optimization though.
|
||||
z.next_in = null;
|
||||
z.next_out = null;
|
||||
}
|
||||
return ctx.writeAndFlush(footer, promise);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
||||
this.ctx = ctx;
|
||||
}
|
||||
}
|
@ -15,32 +15,11 @@
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
import io.netty.util.internal.SystemPropertyUtil;
|
||||
import io.netty.util.internal.logging.InternalLogger;
|
||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
|
||||
/**
|
||||
* Creates a new {@link ZlibEncoder} and a new {@link ZlibDecoder}.
|
||||
*/
|
||||
public final class ZlibCodecFactory {
|
||||
private static final InternalLogger logger = InternalLoggerFactory.getInstance(ZlibCodecFactory.class);
|
||||
|
||||
private static final int DEFAULT_JDK_WINDOW_SIZE = 15;
|
||||
private static final int DEFAULT_JDK_MEM_LEVEL = 8;
|
||||
|
||||
private static final boolean noJdkZlibDecoder;
|
||||
private static final boolean noJdkZlibEncoder;
|
||||
private static final boolean supportsWindowSizeAndMemLevel;
|
||||
|
||||
static {
|
||||
noJdkZlibDecoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibDecoder", false);
|
||||
logger.debug("-Dio.netty.noJdkZlibDecoder: {}", noJdkZlibDecoder);
|
||||
|
||||
noJdkZlibEncoder = SystemPropertyUtil.getBoolean("io.netty.noJdkZlibEncoder", false);
|
||||
logger.debug("-Dio.netty.noJdkZlibEncoder: {}", noJdkZlibEncoder);
|
||||
|
||||
supportsWindowSizeAndMemLevel = true;
|
||||
}
|
||||
private static final boolean supportsWindowSizeAndMemLevel = true;
|
||||
|
||||
/**
|
||||
* Returns {@code true} if specify a custom window size and mem level is supported.
|
||||
@ -50,85 +29,43 @@ public final class ZlibCodecFactory {
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(int compressionLevel) {
|
||||
if (noJdkZlibEncoder) {
|
||||
return new JZlibEncoder(compressionLevel);
|
||||
} else {
|
||||
return new JdkZlibEncoder(compressionLevel);
|
||||
}
|
||||
return new JdkZlibEncoder(compressionLevel);
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper) {
|
||||
if (noJdkZlibEncoder) {
|
||||
return new JZlibEncoder(wrapper);
|
||||
} else {
|
||||
return new JdkZlibEncoder(wrapper);
|
||||
}
|
||||
return new JdkZlibEncoder(wrapper);
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel) {
|
||||
if (noJdkZlibEncoder) {
|
||||
return new JZlibEncoder(wrapper, compressionLevel);
|
||||
} else {
|
||||
return new JdkZlibEncoder(wrapper, compressionLevel);
|
||||
}
|
||||
return new JdkZlibEncoder(wrapper, compressionLevel);
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(ZlibWrapper wrapper, int compressionLevel, int windowBits, int memLevel) {
|
||||
if (noJdkZlibEncoder ||
|
||||
windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
|
||||
return new JZlibEncoder(wrapper, compressionLevel, windowBits, memLevel);
|
||||
} else {
|
||||
return new JdkZlibEncoder(wrapper, compressionLevel);
|
||||
}
|
||||
return new JdkZlibEncoder(wrapper, compressionLevel);
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(byte[] dictionary) {
|
||||
if (noJdkZlibEncoder) {
|
||||
return new JZlibEncoder(dictionary);
|
||||
} else {
|
||||
return new JdkZlibEncoder(dictionary);
|
||||
}
|
||||
return new JdkZlibEncoder(dictionary);
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(int compressionLevel, byte[] dictionary) {
|
||||
if (noJdkZlibEncoder) {
|
||||
return new JZlibEncoder(compressionLevel, dictionary);
|
||||
} else {
|
||||
return new JdkZlibEncoder(compressionLevel, dictionary);
|
||||
}
|
||||
return new JdkZlibEncoder(compressionLevel, dictionary);
|
||||
}
|
||||
|
||||
public static ZlibEncoder newZlibEncoder(int compressionLevel, int windowBits, int memLevel, byte[] dictionary) {
|
||||
if (noJdkZlibEncoder ||
|
||||
windowBits != DEFAULT_JDK_WINDOW_SIZE || memLevel != DEFAULT_JDK_MEM_LEVEL) {
|
||||
return new JZlibEncoder(compressionLevel, windowBits, memLevel, dictionary);
|
||||
} else {
|
||||
return new JdkZlibEncoder(compressionLevel, dictionary);
|
||||
}
|
||||
return new JdkZlibEncoder(compressionLevel, dictionary);
|
||||
}
|
||||
|
||||
public static ZlibDecoder newZlibDecoder() {
|
||||
if (noJdkZlibDecoder) {
|
||||
return new JZlibDecoder();
|
||||
} else {
|
||||
return new JdkZlibDecoder(true);
|
||||
}
|
||||
return new JdkZlibDecoder(true);
|
||||
}
|
||||
|
||||
public static ZlibDecoder newZlibDecoder(ZlibWrapper wrapper) {
|
||||
if (noJdkZlibDecoder) {
|
||||
return new JZlibDecoder(wrapper);
|
||||
} else {
|
||||
return new JdkZlibDecoder(wrapper, true);
|
||||
}
|
||||
return new JdkZlibDecoder(wrapper, true);
|
||||
}
|
||||
|
||||
public static ZlibDecoder newZlibDecoder(byte[] dictionary) {
|
||||
if (noJdkZlibDecoder) {
|
||||
return new JZlibDecoder(dictionary);
|
||||
} else {
|
||||
return new JdkZlibDecoder(dictionary);
|
||||
}
|
||||
return new JdkZlibDecoder(dictionary);
|
||||
}
|
||||
|
||||
private ZlibCodecFactory() {
|
||||
|
@ -1,85 +0,0 @@
|
||||
/*
|
||||
* Copyright 2012 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
import com.jcraft.jzlib.Deflater;
|
||||
import com.jcraft.jzlib.Inflater;
|
||||
import com.jcraft.jzlib.JZlib;
|
||||
|
||||
/**
|
||||
* Utility methods used by {@link JZlibEncoder} and {@link JZlibDecoder}.
|
||||
*/
|
||||
final class ZlibUtil {
|
||||
|
||||
static void fail(Inflater z, String message, int resultCode) {
|
||||
throw inflaterException(z, message, resultCode);
|
||||
}
|
||||
|
||||
static void fail(Deflater z, String message, int resultCode) {
|
||||
throw deflaterException(z, message, resultCode);
|
||||
}
|
||||
|
||||
static DecompressionException inflaterException(Inflater z, String message, int resultCode) {
|
||||
return new DecompressionException(message + " (" + resultCode + ')' + (z.msg != null? ": " + z.msg : ""));
|
||||
}
|
||||
|
||||
static CompressionException deflaterException(Deflater z, String message, int resultCode) {
|
||||
return new CompressionException(message + " (" + resultCode + ')' + (z.msg != null? ": " + z.msg : ""));
|
||||
}
|
||||
|
||||
static JZlib.WrapperType convertWrapperType(ZlibWrapper wrapper) {
|
||||
JZlib.WrapperType convertedWrapperType;
|
||||
switch (wrapper) {
|
||||
case NONE:
|
||||
convertedWrapperType = JZlib.W_NONE;
|
||||
break;
|
||||
case ZLIB:
|
||||
convertedWrapperType = JZlib.W_ZLIB;
|
||||
break;
|
||||
case GZIP:
|
||||
convertedWrapperType = JZlib.W_GZIP;
|
||||
break;
|
||||
case ZLIB_OR_NONE:
|
||||
convertedWrapperType = JZlib.W_ANY;
|
||||
break;
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
return convertedWrapperType;
|
||||
}
|
||||
|
||||
static int wrapperOverhead(ZlibWrapper wrapper) {
|
||||
int overhead;
|
||||
switch (wrapper) {
|
||||
case NONE:
|
||||
overhead = 0;
|
||||
break;
|
||||
case ZLIB:
|
||||
case ZLIB_OR_NONE:
|
||||
overhead = 2;
|
||||
break;
|
||||
case GZIP:
|
||||
overhead = 10;
|
||||
break;
|
||||
default:
|
||||
throw new Error();
|
||||
}
|
||||
return overhead;
|
||||
}
|
||||
|
||||
private ZlibUtil() {
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
public class JZlibTest extends ZlibTest {
|
||||
|
||||
@Override
|
||||
protected ZlibEncoder createEncoder(ZlibWrapper wrapper) {
|
||||
return new JZlibEncoder(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ZlibDecoder createDecoder(ZlibWrapper wrapper, int maxAllocation) {
|
||||
return new JZlibDecoder(wrapper, maxAllocation);
|
||||
}
|
||||
}
|
@ -1,29 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
public class ZlibCrossTest1 extends ZlibTest {
|
||||
|
||||
@Override
|
||||
protected ZlibEncoder createEncoder(ZlibWrapper wrapper) {
|
||||
return new JdkZlibEncoder(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ZlibDecoder createDecoder(ZlibWrapper wrapper, int maxAllocation) {
|
||||
return new JZlibDecoder(wrapper, maxAllocation);
|
||||
}
|
||||
}
|
@ -1,37 +0,0 @@
|
||||
/*
|
||||
* Copyright 2013 The Netty Project
|
||||
*
|
||||
* The Netty Project licenses this file to you under the Apache License,
|
||||
* version 2.0 (the "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at:
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
||||
* License for the specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
package io.netty.handler.codec.compression;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class ZlibCrossTest2 extends ZlibTest {
|
||||
|
||||
@Override
|
||||
protected ZlibEncoder createEncoder(ZlibWrapper wrapper) {
|
||||
return new JZlibEncoder(wrapper);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ZlibDecoder createDecoder(ZlibWrapper wrapper, int maxAllocation) {
|
||||
return new JdkZlibDecoder(wrapper, maxAllocation);
|
||||
}
|
||||
|
||||
@Test(expected = DecompressionException.class)
|
||||
@Override
|
||||
public void testZLIB_OR_NONE3() throws Exception {
|
||||
super.testZLIB_OR_NONE3();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user