Package org.apache.commons.compress

# Class CompressFilterOutputStream<T extends OutputStream>

java.lang.Object
java.io.OutputStream
java.io.FilterOutputStream
org.apache.commons.compress.CompressFilterOutputStream<T>

Type Parameters:
  `T` - The underlying `OutputStream` type.

All Implemented Interfaces:
  `Closeable`, `Flushable`, `AutoCloseable`

Direct Known Subclasses:
  `ArchiveOutputStream`, `CompressorOutputStream`

public abstract class CompressFilterOutputStream<T extends OutputStream> extends FilterOutputStream
Abstracts classes that compress or archive an output stream.

Since:
  1.28.0

- ## Field Summary

  ### Fields inherited from class java.io.FilterOutputStream

  `out`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `CompressFilterOutputStream()`
  Constructs a new instance without a backing `OutputStream`.
  `CompressFilterOutputStream(T out)`
  Creates an output stream filter built on top of the specified underlying `OutputStream`.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `protected void`
  `checkOpen()`
  Check to make sure that this stream has not been closed.
  `void`
  `close()`
  `void`
  `finish()`
  Finishes the addition of entries to this stream, without closing it.
  `boolean`
  `isClosed()`
  Tests whether this instance was successfully closed.
  `protected boolean`
  `isFinished()`
  Tests whether this instance was successfully finished.
  `protected T`
  `out()`
  Gets the underlying output stream.
  `long`
  `write(File file)`
  Writes all bytes from a file this output stream.
  `long`
  `write(Path path)`
  Writes all bytes from a file to this output stream.
  `byte[]`
  `writeUsAscii(String data)`
  Writes and filters the ASCII bytes from the specified String to this output stream.
  `byte[]`
  `writeUsAsciiRaw(String data)`
  Writes the raw ASCII bytes from the specified String to this output stream.
  `byte[]`
  `writeUtf8(String data)`
  Writes and filters the UTF-8 bytes from the specified String to this output stream.

  ### Methods inherited from class java.io.FilterOutputStream

  `flush, write, write, write`

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

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

- ## Constructor Details

  - ### CompressFilterOutputStream

    public CompressFilterOutputStream()
    Constructs a new instance without a backing `OutputStream`.

    You must initialize `this.out` after construction.

  - ### CompressFilterOutputStream

    public CompressFilterOutputStream(T out)
    Creates an output stream filter built on top of the specified underlying `OutputStream`.
    Parameters:
      `out` - the underlying output stream to be assigned to the field `this.out` for later use, or `null` if this instance is to be created without an underlying stream.

- ## Method Details

  - ### checkOpen

    protected void checkOpen() throws IOException
    Check to make sure that this stream has not been closed.
    Throws:
      `IOException` - if the stream is already closed.

  - ### close

    public void close() throws IOException
    Specified by:
      `close` in interface `AutoCloseable`
    Specified by:
      `close` in interface `Closeable`
    Overrides:
      `close` in class `FilterOutputStream`
    Throws:
      `IOException`

  - ### finish

    public void finish() throws IOException
    Finishes the addition of entries to this stream, without closing it. Additional data can be written, if the format supports it.
    Throws:
      `IOException` - Maybe thrown by subclasses if the user forgets to close the entry.

  - ### isClosed

    public boolean isClosed()
    Tests whether this instance was successfully closed.
    Returns:
      whether this instance was successfully closed.
    Since:
      1.27.0

  - ### isFinished

    protected boolean isFinished()
    Tests whether this instance was successfully finished.
    Returns:
      whether this instance was successfully finished.
    Since:
      1.27.0

  - ### out

    protected T out()
    Gets the underlying output stream.
    Returns:
      the underlying output stream.

  - ### write

    public long write(File file) throws IOException
    Writes all bytes from a file this output stream.
    Parameters:
      `file` - the path to the source file.
    Returns:
      the number of bytes read or written.
    Throws:
      `IOException` - if an I/O error occurs when reading or writing.

  - ### write

    public long write(Path path) throws IOException
    Writes all bytes from a file to this output stream.
    Parameters:
      `path` - the path to the source file.
    Returns:
      the number of bytes read or written.
    Throws:
      `IOException` - if an I/O error occurs when reading or writing.

  - ### writeUsAscii

    public byte[] writeUsAscii(String data) throws IOException
    Writes and filters the ASCII bytes from the specified String to this output stream.
    Parameters:
      `data` - the data.
    Returns:
      the ASCII bytes.
    Throws:
      `IOException` - if an I/O error occurs.
    See Also:
      - `OutputStream.write(byte[])`

  - ### writeUsAsciiRaw

    public byte[] writeUsAsciiRaw(String data) throws IOException
    Writes the raw ASCII bytes from the specified String to this output stream.
    Parameters:
      `data` - the data.
    Returns:
      the ASCII bytes.
    Throws:
      `IOException` - if an I/O error occurs.
    See Also:
      - `OutputStream.write(byte[])`

  - ### writeUtf8

    public byte[] writeUtf8(String data) throws IOException
    Writes and filters the UTF-8 bytes from the specified String to this output stream.
    Parameters:
      `data` - the data.
    Returns:
      the ASCII bytes.
    Throws:
      `IOException` - if an I/O error occurs.
    See Also:
      - `OutputStream.write(byte[])`