# CompressorStreamFactory (Apache Commons Compress 1.13 API)

org.apache.commons.compress.compressors

## Class CompressorStreamFactory

- java.lang.Object
- - org.apache.commons.compress.compressors.CompressorStreamFactory

- All Implemented Interfaces:
    CompressorStreamProvider

      public class CompressorStreamFactory
      extends Object
      implements CompressorStreamProvider

  Factory to create Compressor[In|Out]putStreams from names. To add other implementations you should extend CompressorStreamFactory and override the appropriate methods (and call their implementation from super of course).

  Example (Compressing a file):

       final OutputStream out = new FileOutputStream(output);
       CompressorOutputStream cos = new CompressorStreamFactory()
               .createCompressorOutputStream(CompressorStreamFactory.BZIP2, out);
       IOUtils.copy(new FileInputStream(input), cos);
       cos.close();

  Example (Decompressing a file):

       final InputStream is = new FileInputStream(input);
       CompressorInputStream in = new CompressorStreamFactory().createCompressorInputStream(CompressorStreamFactory.BZIP2,
               is);
       IOUtils.copy(in, new FileOutputStream(output));
       in.close();

  This class is immutable
    provided that the deprecated method setDecompressConcatenated is not used.
  This class is thread-safe
    even if the deprecated method setDecompressConcatenated is used

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `static String` | `BZIP2` Constant (value "bzip2") used to identify the BZIP2 compression algorithm.
    `static String` | `DEFLATE` Constant (value "deflate") used to identify the Deflate compress method.
    `static String` | `GZIP` Constant (value "gz") used to identify the GZIP compression algorithm.
    `static String` | `LZMA` Constant (value "lzma") used to identify the LZMA compression method.
    `static String` | `PACK200` Constant (value "pack200") used to identify the PACK200 compression algorithm.
    `static String` | `SNAPPY_FRAMED` Constant (value "snappy-framed") used to identify the "framed" Snappy compression method.
    `static String` | `SNAPPY_RAW` Constant (value "snappy-raw") used to identify the "raw" Snappy compression method.
    `static String` | `XZ` Constant (value "xz") used to identify the XZ compression method.
    `static String` | `Z` Constant (value "z") used to identify the traditional Unix compress method.

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `CompressorStreamFactory()` Create an instance with the decompress Concatenated option set to false.
    `CompressorStreamFactory(boolean decompressUntilEOF)` Create an instance with the provided decompress Concatenated option.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `CompressorInputStream` | `createCompressorInputStream(InputStream in)` Create an compressor input stream from an input stream, autodetecting the compressor type from the first few bytes of the stream.
    `CompressorInputStream` | `createCompressorInputStream(String name, InputStream in)` Creates a compressor input stream from a compressor name and an input stream.
    `CompressorInputStream` | `createCompressorInputStream(String name, InputStream in, boolean actualDecompressConcatenated)` Creates a compressor input stream from a compressor name and an input stream.
    `CompressorOutputStream` | `createCompressorOutputStream(String name, OutputStream out)` Creates an compressor output stream from an compressor name and an output stream.
    `static SortedMap<String,CompressorStreamProvider>` | `findAvailableCompressorInputStreamProviders()` Constructs a new sorted map from input stream provider names to provider objects.
    `static SortedMap<String,CompressorStreamProvider>` | `findAvailableCompressorOutputStreamProviders()` Constructs a new sorted map from output stream provider names to provider objects.
    `static String` | `getBzip2()`
    `SortedMap<String,CompressorStreamProvider>` | `getCompressorInputStreamProviders()`
    `SortedMap<String,CompressorStreamProvider>` | `getCompressorOutputStreamProviders()`
    `Boolean` | `getDecompressUntilEOF()`
    `static String` | `getDeflate()`
    `static String` | `getGzip()`
    `Set<String>` | `getInputStreamCompressorNames()` Gets all the input stream compressor names for this provider
    `static String` | `getLzma()`
    `Set<String>` | `getOutputStreamCompressorNames()` Gets all the output stream compressor names for this provider
    `static String` | `getPack200()`
    `static CompressorStreamFactory` | `getSingleton()`
    `static String` | `getSnappyFramed()`
    `static String` | `getSnappyRaw()`
    `static String` | `getXz()`
    `static String` | `getZ()`
    `void` | `setDecompressConcatenated(boolean decompressConcatenated)` Deprecated. 1.10 use the `CompressorStreamFactory(boolean)` constructor instead

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### BZIP2

          public static final String BZIP2

      Constant (value "bzip2") used to identify the BZIP2 compression algorithm.
      Since:
        1.1
      See Also:
        Constant Field Values

    - #### GZIP

          public static final String GZIP

      Constant (value "gz") used to identify the GZIP compression algorithm. Not supported as an output stream type.
      Since:
        1.1
      See Also:
        Constant Field Values

    - #### PACK200

          public static final String PACK200

      Constant (value "pack200") used to identify the PACK200 compression algorithm.
      Since:
        1.3
      See Also:
        Constant Field Values

    - #### XZ

          public static final String XZ

      Constant (value "xz") used to identify the XZ compression method.
      Since:
        1.4
      See Also:
        Constant Field Values

    - #### LZMA

          public static final String LZMA

      Constant (value "lzma") used to identify the LZMA compression method. Not supported as an output stream type.
      Since:
        1.6
      See Also:
        Constant Field Values

    - #### SNAPPY_FRAMED

          public static final String SNAPPY_FRAMED

      Constant (value "snappy-framed") used to identify the "framed" Snappy compression method. Not supported as an output stream type.
      Since:
        1.7
      See Also:
        Constant Field Values

    - #### SNAPPY_RAW

          public static final String SNAPPY_RAW

      Constant (value "snappy-raw") used to identify the "raw" Snappy compression method. Not supported as an output stream type.
      Since:
        1.7
      See Also:
        Constant Field Values

    - #### Z

          public static final String Z

      Constant (value "z") used to identify the traditional Unix compress method. Not supported as an output stream type.
      Since:
        1.7
      See Also:
        Constant Field Values

    - #### DEFLATE

          public static final String DEFLATE

      Constant (value "deflate") used to identify the Deflate compress method.
      Since:
        1.9
      See Also:
        Constant Field Values

  - ### Constructor Detail

    - #### CompressorStreamFactory

          public CompressorStreamFactory()

      Create an instance with the decompress Concatenated option set to false.

    - #### CompressorStreamFactory

          public CompressorStreamFactory(boolean decompressUntilEOF)

      Create an instance with the provided decompress Concatenated option.
      Parameters:
        `decompressUntilEOF` - if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and xz formats only.
      Since:
        1.10

  - ### Method Detail

    - #### findAvailableCompressorInputStreamProviders

          public static SortedMap<String,CompressorStreamProvider> findAvailableCompressorInputStreamProviders()

      Constructs a new sorted map from input stream provider names to provider objects.

      The map returned by this method will have one entry for each provider for which support is available in the current Java virtual machine. If two or more supported provider have the same name then the resulting map will contain just one of them; which one it will contain is not specified.

      The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available providers, for example to allow user provider selection.

      This method may return different results at different times if new providers are dynamically made available to the current Java virtual machine.

      Returns:
        An immutable, map from names to provider objects
      Since:
        1.13

    - #### findAvailableCompressorOutputStreamProviders

          public static SortedMap<String,CompressorStreamProvider> findAvailableCompressorOutputStreamProviders()

      Constructs a new sorted map from output stream provider names to provider objects.

      The map returned by this method will have one entry for each provider for which support is available in the current Java virtual machine. If two or more supported provider have the same name then the resulting map will contain just one of them; which one it will contain is not specified.

      The invocation of this method, and the subsequent use of the resulting map, may cause time-consuming disk or network I/O operations to occur. This method is provided for applications that need to enumerate all of the available providers, for example to allow user provider selection.

      This method may return different results at different times if new providers are dynamically made available to the current Java virtual machine.

      Returns:
        An immutable, map from names to provider objects
      Since:
        1.13

    - #### getBzip2

          public static String getBzip2()

    - #### getDeflate

          public static String getDeflate()

    - #### getGzip

          public static String getGzip()

    - #### getLzma

          public static String getLzma()

    - #### getPack200

          public static String getPack200()

    - #### getSingleton

          public static CompressorStreamFactory getSingleton()

    - #### getSnappyFramed

          public static String getSnappyFramed()

    - #### getSnappyRaw

          public static String getSnappyRaw()

    - #### getXz

          public static String getXz()

    - #### getZ

          public static String getZ()

    - #### createCompressorInputStream

          public CompressorInputStream createCompressorInputStream(InputStream in)
                                                            throws CompressorException

      Create an compressor input stream from an input stream, autodetecting the compressor type from the first few bytes of the stream. The InputStream must support marks, like BufferedInputStream.
      Parameters:
        `in` - the input stream
      Returns:
        the compressor input stream
      Throws:
        `CompressorException` - if the compressor name is not known
        `IllegalArgumentException` - if the stream is null or does not support mark
      Since:
        1.1

    - #### createCompressorInputStream

          public CompressorInputStream createCompressorInputStream(String name,
                                                          InputStream in)
                                                            throws CompressorException

      Creates a compressor input stream from a compressor name and an input stream.
      Parameters:
        `name` - of the compressor, i.e. "gz", "bzip2", "xz", "lzma", "pack200", "snappy-raw", "snappy-framed", "z" or "deflate"
        `in` - the input stream
      Returns:
        compressor input stream
      Throws:
        `CompressorException` - if the compressor name is not known
        `IllegalArgumentException` - if the name or input stream is null

    - #### createCompressorInputStream

          public CompressorInputStream createCompressorInputStream(String name,
                                                          InputStream in,
                                                          boolean actualDecompressConcatenated)
                                                            throws CompressorException

      Description copied from interface: `CompressorStreamProvider`
      Creates a compressor input stream from a compressor name and an input stream.
      Specified by:
        `createCompressorInputStream` in interface `CompressorStreamProvider`
      Parameters:
        `name` - of the compressor, i.e. "gz", "bzip2", "xz", "lzma", "pack200", "snappy-raw", "snappy-framed", "z" or "deflate"
        `in` - the input stream
        `actualDecompressConcatenated` - if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream. This setting applies to the gzip, bzip2 and xz formats only.
      Returns:
        compressor input stream
      Throws:
        `CompressorException` - if the compressor name is not known

    - #### createCompressorOutputStream

          public CompressorOutputStream createCompressorOutputStream(String name,
                                                            OutputStream out)
                                                              throws CompressorException

      Creates an compressor output stream from an compressor name and an output stream.
      Specified by:
        `createCompressorOutputStream` in interface `CompressorStreamProvider`
      Parameters:
        `name` - the compressor name, i.e. "gz", "bzip2", "xz", "pack200" or "deflate"
        `out` - the output stream
      Returns:
        the compressor output stream
      Throws:
        `CompressorException` - if the archiver name is not known
        `IllegalArgumentException` - if the archiver name or stream is null

    - #### getCompressorInputStreamProviders

          public SortedMap<String,CompressorStreamProvider> getCompressorInputStreamProviders()

    - #### getCompressorOutputStreamProviders

          public SortedMap<String,CompressorStreamProvider> getCompressorOutputStreamProviders()

    - #### getDecompressUntilEOF

          public Boolean getDecompressUntilEOF()

    - #### getInputStreamCompressorNames

          public Set<String> getInputStreamCompressorNames()

      Description copied from interface: `CompressorStreamProvider`
      Gets all the input stream compressor names for this provider
      Specified by:
        `getInputStreamCompressorNames` in interface `CompressorStreamProvider`
      Returns:
        all the input compressor names for this provider

    - #### getOutputStreamCompressorNames

          public Set<String> getOutputStreamCompressorNames()

      Description copied from interface: `CompressorStreamProvider`
      Gets all the output stream compressor names for this provider
      Specified by:
        `getOutputStreamCompressorNames` in interface `CompressorStreamProvider`
      Returns:
        all the output compressor names for this provider

    - #### setDecompressConcatenated

          @Deprecated
          public void setDecompressConcatenated(boolean decompressConcatenated)

      Deprecated. 1.10 use the `CompressorStreamFactory(boolean)` constructor instead
      Whether to decompress the full input or only the first stream in formats supporting multiple concatenated input streams.

      This setting applies to the gzip, bzip2 and xz formats only.

      Parameters:
        `decompressConcatenated` - if true, decompress until the end of the input; if false, stop after the first stream and leave the input position to point to the next byte after the stream
      Throws:
        `IllegalStateException` - if the constructor `CompressorStreamFactory(boolean)` was used to create the factory
      Since:
        1.5

Copyright © 2016 The Apache Software Foundation. All rights reserved.