Class UnsafeChunkDecoder


  • public class UnsafeChunkDecoder
    extends ChunkDecoder
    Highly optimized ChunkDecoder implementation that uses Sun JDK's Unsafe class (which may be included by other JDK's as well; IBM's apparently does).

    Credits for the idea go to Dain Sundstrom, who kindly suggested this use, and is all-around great source for optimization tips and tricks. Big thanks also to LZ4-java developers, whose stellar performance made me go back and see what more I can do to optimize this code!

    • Constructor Detail

      • UnsafeChunkDecoder

        public UnsafeChunkDecoder()
    • Method Detail

      • decodeChunk

        public final int decodeChunk​(InputStream is,
                                     byte[] inputBuffer,
                                     byte[] outputBuffer)
                              throws IOException
        Description copied from class: ChunkDecoder
        Main decode from a stream. Decompressed bytes are placed in the outputBuffer, inputBuffer is a "scratch-area".
        Specified by:
        decodeChunk in class ChunkDecoder
        Parameters:
        is - An input stream of LZF compressed bytes
        inputBuffer - A byte array used as a scratch area.
        outputBuffer - A byte array in which the result is returned
        Returns:
        The number of bytes placed in the outputBuffer.
        Throws:
        IOException
      • skipOrDecodeChunk

        public int skipOrDecodeChunk​(InputStream is,
                                     byte[] inputBuffer,
                                     byte[] outputBuffer,
                                     long maxToSkip)
                              throws IOException
        Specified by:
        skipOrDecodeChunk in class ChunkDecoder
        Returns:
        If positive number, number of bytes skipped; if -1, end-of-stream was reached; otherwise, amount of content decoded (using formula of returnValue = -(decodedAmount + 2))
        Throws:
        IOException