com.ibm.jzos

Class CompressionFactory

  • java.lang.Object
    • com.ibm.jzos.CompressionFactory


  • public class CompressionFactory
    extends java.lang.Object
    An abstract compression factory that can be used with different compression algorithms such as GZIP compression, INFLATE/DEFLATE, and z/Architecture CMPSC compression.

    A CompressionFactory may be used to construct one of the following:
    • An OutputStream which can be used to compress a stream of data
    • An InputStream which can be used to expand a stream of compressed data
    • A BufferCompressor which can be used to compress or expand discrete buffers of data

    A Java system property may be used to configure a default CompressionFactory with one of the following compression algorithms:
    • z/Architecture CMPSC instruction (see ZCompressor)
    • GZIP (see GZIPInputStream and GZIPOutputStream)
    • DEFLATE (see Deflater, Inflater, DeflaterOutputStream and InflaterInputStream)

    The syntax of this property is: -Dcom.ibm.jzos.compression.type=<algorithm>[:parm1[:parm2]]
    Examples:
    • -Dcom.ibm.jzos.compression.type=zcmpsc:12://HLQ.MYDICT.DATA
      z/Architecture CMPSC compression using, in this example, a CMPSC dictionary with 12-bit symbols in a z/OS data set.
      In this file/data set, the compression dictionary must be followed by the expansion dictionary.
      Both parameters for zcmpsc are required.
    • -Dcom.ibm.jzos.compression.type=gzip
      GZIP compression (the default if not specified). No additional parameters are required.
    • -Dcom.ibm.jzos.compression.type=deflate:9
      Deflate/Inflate with compression level 0-9, with 0 meaning no compression and 9 meaning maximum compression.
      The compression level is an optional parameter. If not specified, default compression level will be used.

    Note: This property is case sensitive.
    In addition, changes to this property after creating the default factory are not applied to the (cached) default factory. See releaseDefault().

    Usage examples:

    1. Use a compressing OutputStream with the default CompressionFactory
      
       CompressionFactory factory = CompressionFactory.getDefault();
       FileOutputStream fout = new FileOutputStream("compressed.dat");
       OutputStream compOut = factory.getCompressingOutputStream(fout);
       compOut.write(buf);
       ...
       compOut.close();
       factory.release(); // release any cached resources
           
    2. Obtain a BufferCompressor from the default CompressionFactory and use it to compress and expand a buffer:
      
       CompressionFactory factory = CompressionFactory.getDefault();
       BufferCompressor bc = factory.getBufferCompressor();
       int nCompressed = bc.compressBuffer(cbuf, 0, inbuf,  0, inbuf.length);
       byte[] buf2 = new byte[inbuf.length];
       int nExpanded = bc.expandBuffer(buf2, 0, cbuf, 0, nCompressed);
       assert nExpanded = inbuf.length;
       assert Arrays.equals(inbuf, buf2);
       factory.release(); // release any cached resources
           
    3. CompressionFactories may be constructed explicitly for a specific algorithm and parameters:
      
       CompressionFactory factory = CompressionFactory.getFactory(
               CompressionFactory.Algorithm.zcmpsc, "12:/data/zcmpsc.dicts");
       ...
       CompressionFactory factory = CompressionFactory.getFactory(
               CompressionFactory.Algorithm.deflate, "9");
       ...
       CompressionFactory factory = CompressionFactory.getFactory(
               CompressionFactory.Algorithm.gzip, null);
       ...
           
    Since:
    2.4.4
    See Also:
    ZCompressor, ZCompressorInputStream, ZCompressorOutputStream, Deflater, Inflater, DeflaterOutputStream, InflaterInputStream, GZIPInputStream, GZIPOutputStream
    • Field Detail

      • PROPERTY_COMPRESSION_TYPE

        public static final java.lang.String PROPERTY_COMPRESSION_TYPE
        This property may be used to set the default algorithm and its parameters, separated by ':'
        See Also:
        Constant Field Values
      • DEFAULT_COMPRESSION_TYPE

        public static final java.lang.String DEFAULT_COMPRESSION_TYPE
        The default algorithm is gzip if System property PROPERTY_COMPRESSION_TYPE is not set
    • Method Detail

      • releaseDefault

        public static void releaseDefault()
        Release the current default factory instance. This allows for changes to the PROPERTY_COMPRESSION_TYPE System property to be reflected in the creation of a new default cached factory.
      • getFactory

        public static CompressionFactory getFactory(CompressionFactory.Algorithm algorithm,
                                                    java.lang.String parms)
        Construct and return a new CompressionFactory for a given CompressionFactory.Algorithm and optional parms.
        Parameters:
        algorithm - the type of compression algorithm
        parms - zero or more algorithm specific parameters separated by ':', may be null
        Returns:
        a CompressionFactory
      • getAlgorithm

        public CompressionFactory.Algorithm getAlgorithm()
        Answer the compression algorithm used by this CompressionFactory instance.
        Returns:
        the compression algorithm
      • getAlgorithmParms

        public java.lang.String[] getAlgorithmParms()
        Answer the algorithm parameters used by this CompressionFactory instance.
        Returns:
        a String array with zero or more algorithm parameters
      • getCompressingOutputStream

        public java.io.OutputStream getCompressingOutputStream(java.io.OutputStream out)
                                                        throws java.io.IOException
        Construct and return an OutputStream that can be used to compress data to another OutputStream. The CompressionFactory.Algorithm and parameters configured for this CompressionFactory will determine which OutputStream implementation will be used. Streams are not thread safe.
        Parameters:
        out - the OutputStream where compressed data will be written
        Returns:
        OutputStream to which data may be written to be compressed
        Throws:
        java.io.IOException
      • getCompressingOutputStream

        public java.io.OutputStream getCompressingOutputStream(java.io.OutputStream out,
                                                               int bufsize)
                                                        throws java.io.IOException
        Construct and return an OutputStream that can be used to compress data to another OutputStream. The CompressionFactory.Algorithm and parameters configured for this CompressionFactory will determine which OutputStream implementation will be used. Streams are not thread safe.
        Parameters:
        out - the OutputStream where compressed data will be written
        bufsize - the buffer size used (if any) by the OutputStream
        Returns:
        OutputStream to which data may be written to be compressed
        Throws:
        java.io.IOException
      • getExpandingInputStream

        public java.io.InputStream getExpandingInputStream(java.io.InputStream in)
                                                    throws java.io.IOException
        Construct and return an InputStream that can be used to expand compressed data from another InputStream. The CompressionFactory.Algorithm and parameters configured for this CompressionFactory will determine which InputStream implementation will be used. Streams are not thread safe.
        Parameters:
        in - the InputStream where compressed data will be read
        Returns:
        InputStream from which expanded data may be read
        Throws:
        java.io.IOException
      • getExpandingInputStream

        public java.io.InputStream getExpandingInputStream(java.io.InputStream in,
                                                           int bufsize)
                                                    throws java.io.IOException
        Construct and return an InputStream that can be used to expand compressed data from another InputStream. The CompressionFactory.Algorithm and parameters configured for this CompressionFactory will determine which InputStream implementation will be used. Streams are not thread safe.
        Parameters:
        in - the InputStream where compressed data will be read
        bufsize - bufsize the buffer size (if any) used by the InputStream
        Returns:
        InputStream from which expanded data may be read
        Throws:
        java.io.IOException - if an I/O error has occurred
      • getBufferCompressor

        public BufferCompressor getBufferCompressor()
                                             throws java.io.IOException
        Construct and return a BufferCompressor that may be used to compress or expand a discrete block of data.
        Throws:
        java.io.IOException - in the case that constructing the configured algorithm's compressor encounters an IOException, e.g. when reading dictionaries for a z/Architecture CMPSC compressor.
        See Also:
        BufferCompressor
      • release

        public void release()
        Release any resources that are cached by this CompressionFactory.
        The default (cached) factory may choose to do nothing, but it is recommended that the programmer always call this method when finished using resources that were created by this factory instance.
� Copyright IBM Corporation 2005, 2022.