- java.lang.Object
-
- io.netty5.handler.codec.compression.LzfCompressor
-
- All Implemented Interfaces:
Compressor,AutoCloseable
public final class LzfCompressor extends Object implements Compressor
Compresses aBufferusing the LZF format.See original LZF package and LZF format for full description.
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()# Close the compressor.Buffercompress(Buffer in, BufferAllocator allocator)This method will read from the inputBufferand compress into a newBufferthat will be allocated (if needed) from theBufferAllocator.Bufferfinish(BufferAllocator allocator)By calling this method we signal that the compression stream is marked as finish.booleanisClosed()Returntrueif the decompressor was closed,falseotherwise.booleanisFinished()Returns true if the compressor was finished or closed.static Supplier<LzfCompressor>newFactory()Creates a new LZF compressor factory with the most optimal available methods for underlying data access.static Supplier<LzfCompressor>newFactory(boolean safeInstance)Creates a new LZF compressor factory with specified encoding instance.static Supplier<LzfCompressor>newFactory(boolean safeInstance, int totalLength)Creates a new LZF compressor factory with specified encoding instance and compressThreshold.static Supplier<LzfCompressor>newFactory(boolean safeInstance, int totalLength, int compressThreshold)Creates a new LZF compressor factory with specified settings.static Supplier<LzfCompressor>newFactory(int totalLength)Creates a new LZF compressor factory with specified total length of encoded chunk.
-
-
-
Method Detail
-
newFactory
public static Supplier<LzfCompressor> newFactory()
Creates a new LZF compressor factory with the most optimal available methods for underlying data access. It will "unsafe" instance if one can be used on current JVM. It should be safe to call this constructor as implementations are dynamically loaded; however, on some non-standard platforms it may be necessary to usenewFactory(boolean)withtrueparam.- Returns:
- the factory.
-
newFactory
public static Supplier<LzfCompressor> newFactory(boolean safeInstance)
Creates a new LZF compressor factory with specified encoding instance.- Parameters:
safeInstance- Iftrueencoder will useChunkEncoderthat only uses standard JDK access methods, and should work on all Java platforms and JVMs. Otherwise encoder will try to use highly optimizedChunkEncoderimplementation that uses Sun JDK'sUnsafeclass (which may be included by other JDK's as well).- Returns:
- the factory.
-
newFactory
public static Supplier<LzfCompressor> newFactory(boolean safeInstance, int totalLength)
Creates a new LZF compressor factory with specified encoding instance and compressThreshold.- Parameters:
safeInstance- Iftrueencoder will useChunkEncoderthat only uses standard JDK access methods, and should work on all Java platforms and JVMs. Otherwise encoder will try to use highly optimizedChunkEncoderimplementation that uses Sun JDK'sUnsafeclass (which may be included by other JDK's as well).totalLength- Expected total length of content to compress; only matters for outgoing messages that is smaller than maximum chunk size (64k), to optimize encoding hash tables.- Returns:
- the factory.
-
newFactory
public static Supplier<LzfCompressor> newFactory(int totalLength)
Creates a new LZF compressor factory with specified total length of encoded chunk. You can configure it to encode your data flow more efficient if you know the average size of messages that you send.- Parameters:
totalLength- Expected total length of content to compress; only matters for outgoing messages that is smaller than maximum chunk size (64k), to optimize encoding hash tables.- Returns:
- the factory.
-
newFactory
public static Supplier<LzfCompressor> newFactory(boolean safeInstance, int totalLength, int compressThreshold)
Creates a new LZF compressor factory with specified settings.- Parameters:
safeInstance- Iftrueencoder will useChunkEncoderthat only uses standard JDK access methods, and should work on all Java platforms and JVMs. Otherwise encoder will try to use highly optimizedChunkEncoderimplementation that uses Sun JDK'sUnsafeclass (which may be included by other JDK's as well).totalLength- Expected total length of content to compress; only matters for outgoing messages that is smaller than maximum chunk size (64k), to optimize encoding hash tables.compressThreshold- Compress threshold for LZF format. When the amount of input data is less than compressThreshold, we will construct an uncompressed output according to the LZF format.- Returns:
- the factory.
-
compress
public Buffer compress(Buffer in, BufferAllocator allocator) throws CompressionException
Description copied from interface:CompressorThis method will read from the inputBufferand compress into a newBufferthat 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:
compressin interfaceCompressor- Parameters:
in- theBufferthat contains the data to be compressed.allocator- theBufferAllocatorthat is used to allocate a new buffer (if needed) to write the compressed bytes too.- Returns:
- the
Bufferthat 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:CompressorBy calling this method we signal that the compression stream is marked as finish. The returnedBuffermight contain a "trailer" which marks the end of the stream.- Specified by:
finishin interfaceCompressor- Returns:
- the
Bufferwhich 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:CompressorReturns 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()returnstruetheCompressor.compress(Buffer, BufferAllocator)method will just return an empty buffer without consuming anything from its input buffer.- Specified by:
isFinishedin interfaceCompressor- Returns:
trueif the compressor was marked as finished,falseotherwise.
-
isClosed
public boolean isClosed()
Description copied from interface:CompressorReturntrueif the decompressor was closed,falseotherwise.- Specified by:
isClosedin interfaceCompressor- Returns:
trueif the decompressor was closed,falseotherwise.
-
close
public void close()
Description copied from interface:Compressor# Close the compressor. After this method was calledCompressor.isFinished()will returntrueas well and it is not allowed to callCompressor.compress(Buffer, BufferAllocator)orCompressor.finish(BufferAllocator)anymore -- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCompressor
-
-