Package org.h2.tools

Class CompressTool


  • public class CompressTool
    extends java.lang.Object
    A tool to losslessly compress data, and expand the compressed data again.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private byte[] buffer  
      private static int MAX_BUFFER_SIZE  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CompressTool()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static int compress​(byte[] in, int len, Compressor compress, byte[] out)  
      byte[] compress​(byte[] in, java.lang.String algorithm)
      Compressed the data using the specified algorithm.
      byte[] expand​(byte[] in)
      Expands the compressed data.
      static void expand​(byte[] in, byte[] out, int outPos)
      INTERNAL
      private byte[] getBuffer​(int min)  
      private static int getCompressAlgorithm​(java.lang.String algorithm)
      INTERNAL
      private static Compressor getCompressor​(int algorithm)  
      private static Compressor getCompressor​(java.lang.String algorithm)  
      static CompressTool getInstance()
      Get a new instance.
      static int getVariableIntLength​(int x)
      Get a variable size integer length using Rice coding.
      static int readVariableInt​(byte[] buff, int pos)
      Read a variable size integer using Rice coding.
      static java.io.InputStream wrapInputStream​(java.io.InputStream in, java.lang.String compressionAlgorithm, java.lang.String entryName)
      INTERNAL
      static java.io.OutputStream wrapOutputStream​(java.io.OutputStream out, java.lang.String compressionAlgorithm, java.lang.String entryName)
      INTERNAL
      static int writeVariableInt​(byte[] buff, int pos, int x)
      Write a variable size integer using Rice coding.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • buffer

        private byte[] buffer
    • Constructor Detail

      • CompressTool

        private CompressTool()
    • Method Detail

      • getBuffer

        private byte[] getBuffer​(int min)
      • getInstance

        public static CompressTool getInstance()
        Get a new instance. Each instance uses a separate buffer, so multiple instances can be used concurrently. However each instance alone is not multithreading safe.
        Returns:
        a new instance
      • compress

        public byte[] compress​(byte[] in,
                               java.lang.String algorithm)
        Compressed the data using the specified algorithm. If no algorithm is supplied, LZF is used
        Parameters:
        in - the byte array with the original data
        algorithm - the algorithm (LZF, DEFLATE)
        Returns:
        the compressed data
      • compress

        private static int compress​(byte[] in,
                                    int len,
                                    Compressor compress,
                                    byte[] out)
      • expand

        public byte[] expand​(byte[] in)
        Expands the compressed data.
        Parameters:
        in - the byte array with the compressed data
        Returns:
        the uncompressed data
      • expand

        public static void expand​(byte[] in,
                                  byte[] out,
                                  int outPos)
        INTERNAL
        Parameters:
        in - compressed data
        out - uncompressed result
        outPos - the offset at the output array
      • readVariableInt

        public static int readVariableInt​(byte[] buff,
                                          int pos)
        Read a variable size integer using Rice coding.
        Parameters:
        buff - the buffer
        pos - the position
        Returns:
        the integer
      • writeVariableInt

        public static int writeVariableInt​(byte[] buff,
                                           int pos,
                                           int x)
        Write a variable size integer using Rice coding. Negative values need 5 bytes.
        Parameters:
        buff - the buffer
        pos - the position
        x - the value
        Returns:
        the number of bytes written (0-5)
      • getVariableIntLength

        public static int getVariableIntLength​(int x)
        Get a variable size integer length using Rice coding. Negative values need 5 bytes.
        Parameters:
        x - the value
        Returns:
        the number of bytes needed (0-5)
      • getCompressor

        private static Compressor getCompressor​(java.lang.String algorithm)
      • getCompressAlgorithm

        private static int getCompressAlgorithm​(java.lang.String algorithm)
        INTERNAL
        Parameters:
        algorithm - to translate into index
        Returns:
        index of the specified algorithm
      • getCompressor

        private static Compressor getCompressor​(int algorithm)
      • wrapOutputStream

        public static java.io.OutputStream wrapOutputStream​(java.io.OutputStream out,
                                                            java.lang.String compressionAlgorithm,
                                                            java.lang.String entryName)
        INTERNAL
        Parameters:
        out - stream
        compressionAlgorithm - to be used
        entryName - in a zip file
        Returns:
        compressed stream
      • wrapInputStream

        public static java.io.InputStream wrapInputStream​(java.io.InputStream in,
                                                          java.lang.String compressionAlgorithm,
                                                          java.lang.String entryName)
        INTERNAL
        Parameters:
        in - stream
        compressionAlgorithm - to be used
        entryName - in a zip file
        Returns:
        in stream or null if there is no such entry