- java.lang.Object
-
- io.netty5.handler.codec.compression.ZlibCompressor
-
- All Implemented Interfaces:
Compressor
,AutoCloseable
public final class ZlibCompressor extends Object implements Compressor
Compresses aBuffer
using the deflate algorithm.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
# Close the compressor.Buffer
compress(Buffer uncompressed, BufferAllocator allocator)
This method will read from the inputBuffer
and compress into a newBuffer
that will be allocated (if needed) from theBufferAllocator
.Buffer
finish(BufferAllocator allocator)
By calling this method we signal that the compression stream is marked as finish.boolean
isClosed()
Returntrue
if the decompressor was closed,false
otherwise.boolean
isFinished()
Returns true if the compressor was finished or closed.static Supplier<ZlibCompressor>
newFactory()
Creates a zlib compressor factory with the default compression level (6
) and the default wrapper (ZlibWrapper.ZLIB
).static Supplier<ZlibCompressor>
newFactory(byte[] dictionary)
Creates a zlib compressor factory with the default compression level (6
) and the specified preset dictionary.static Supplier<ZlibCompressor>
newFactory(int compressionLevel)
Creates a zlib compressor factory with the specifiedcompressionLevel
and the default wrapper (ZlibWrapper.ZLIB
).static Supplier<ZlibCompressor>
newFactory(int compressionLevel, byte[] dictionary)
Creates a zlib compressor factory with the specifiedcompressionLevel
and the specified preset dictionary.static Supplier<ZlibCompressor>
newFactory(ZlibWrapper wrapper)
Creates a zlib compressor factory with the default compression level (6
) and the specified wrapper.static Supplier<ZlibCompressor>
newFactory(ZlibWrapper wrapper, int compressionLevel)
Creates a zlib compressor factory with the specifiedcompressionLevel
and the specified wrapper.
-
-
-
Method Detail
-
newFactory
public static Supplier<ZlibCompressor> newFactory()
Creates a zlib compressor factory with the default compression level (6
) and the default wrapper (ZlibWrapper.ZLIB
).- Returns:
- the factory.
- Throws:
CompressionException
- if failed to initialize zlib
-
newFactory
public static Supplier<ZlibCompressor> newFactory(int compressionLevel)
Creates a zlib compressor factory with the specifiedcompressionLevel
and the default wrapper (ZlibWrapper.ZLIB
).- Parameters:
compressionLevel
-1
yields the fastest compression and9
yields the best compression.0
means no compression. The default compression level is6
.- Throws:
CompressionException
- if failed to initialize zlib
-
newFactory
public static Supplier<ZlibCompressor> newFactory(ZlibWrapper wrapper)
Creates a zlib compressor factory with the default compression level (6
) and the specified wrapper.- Returns:
- the factory.
- Throws:
CompressionException
- if failed to initialize zlib
-
newFactory
public static Supplier<ZlibCompressor> newFactory(ZlibWrapper wrapper, int compressionLevel)
Creates a zlib compressor factory with the specifiedcompressionLevel
and the specified wrapper.- Parameters:
compressionLevel
-1
yields the fastest compression and9
yields the best compression.0
means no compression. The default compression level is6
.- Returns:
- the factory.
- Throws:
CompressionException
- if failed to initialize zlib
-
newFactory
public static Supplier<ZlibCompressor> newFactory(byte[] dictionary)
Creates a zlib compressor factory with the default compression level (6
) and the specified preset dictionary. The wrapper is alwaysZlibWrapper.ZLIB
because it is the only format that supports the preset dictionary.- Parameters:
dictionary
- the preset dictionary- Returns:
- the factory.
- Throws:
CompressionException
- if failed to initialize zlib
-
newFactory
public static Supplier<ZlibCompressor> newFactory(int compressionLevel, byte[] dictionary)
Creates a zlib compressor factory with the specifiedcompressionLevel
and the specified preset dictionary. The wrapper is alwaysZlibWrapper.ZLIB
because it is the only format that supports the preset dictionary.- Parameters:
compressionLevel
-1
yields the fastest compression and9
yields the best compression.0
means no compression. The default compression level is6
.dictionary
- the preset dictionary- Returns:
- the factory.
- Throws:
CompressionException
- if failed to initialize zlib
-
compress
public Buffer compress(Buffer uncompressed, BufferAllocator allocator) throws CompressionException
Description copied from interface:Compressor
This method will read from the inputBuffer
and compress into a newBuffer
that will be allocated (if needed) from theBufferAllocator
. This method is expected to consume all data from the input but not take ownership. The caller is responsible to release the input buffer after this method returns.- Specified by:
compress
in interfaceCompressor
- Parameters:
uncompressed
- theBuffer
that contains the data to be compressed.allocator
- theBufferAllocator
that is used to allocate a new buffer (if needed) to write the compressed bytes too.- Returns:
- the
Buffer
that contains the compressed data. The caller of this method takes ownership of the buffer. The return value will never benull
. - Throws:
CompressionException
- thrown if an compression error was encountered or the compressor was closed already.
-
finish
public Buffer finish(BufferAllocator allocator)
Description copied from interface:Compressor
By calling this method we signal that the compression stream is marked as finish. The returnedBuffer
might contain a "trailer" which marks the end of the stream.- Specified by:
finish
in interfaceCompressor
- Returns:
- the
Buffer
which represent the end of the compression stream, which might be empty if the compressor don't need a trailer to signal the end. The caller of this method takes ownership of the buffer. The return value will never benull
.
-
isFinished
public boolean isFinished()
Description copied from interface:Compressor
Returns true if the compressor was finished or closed. This might happen because someone explicit calledCompressor.finish(BufferAllocator)
/Compressor.close()
or the compressor implementation did decide to close itself due a compression error which can't be recovered. AfterCompressor.isFinished()
returnstrue
theCompressor.compress(Buffer, BufferAllocator)
method will just return an empty buffer without consuming anything from its input buffer.- Specified by:
isFinished
in interfaceCompressor
- Returns:
true
if the compressor was marked as finished,false
otherwise.
-
isClosed
public boolean isClosed()
Description copied from interface:Compressor
Returntrue
if the decompressor was closed,false
otherwise.- Specified by:
isClosed
in interfaceCompressor
- Returns:
true
if the decompressor was closed,false
otherwise.
-
close
public void close()
Description copied from interface:Compressor
# Close the compressor. After this method was calledCompressor.isFinished()
will returntrue
as well and it is not allowed to callCompressor.compress(Buffer, BufferAllocator)
orCompressor.finish(BufferAllocator)
anymore -- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCompressor
-
-