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
    Modifier and Type
    Field
    Description
    private final AtomicBoolean
    Whether this instance was successfully closed.
    private boolean
    Whether this instance was successfully finished.

    Fields inherited from class java.io.FilterOutputStream

    out
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new instance without a backing OutputStream.
    Creates an output stream filter built on top of the specified underlying OutputStream.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected void
    Check to make sure that this stream has not been closed.
    void
     
    void
    Finishes the addition of entries to this stream, without closing it.
    boolean
    Tests whether this instance was successfully closed.
    protected boolean
    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.
    private static byte[]
    write(OutputStream os, String data, Charset charset)
    Writes and filters the bytes from the specified String to this output stream using the given Charset.
    long
    write(Path path)
    Writes all bytes from a file to this output stream.
    byte[]
    Writes and filters the ASCII bytes from the specified String to this output stream.
    byte[]
    Writes the raw ASCII bytes from the specified String to this output stream.
    byte[]
    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
  • Field Details

    • closed

      private final AtomicBoolean closed
      Whether this instance was successfully closed.
    • finished

      private boolean finished
      Whether this instance was successfully finished.

      The state transition usually is open, to finished, to closed.

  • 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

    • write

      private static byte[] write(OutputStream os, String data, Charset charset) throws IOException
      Writes and filters the bytes from the specified String to this output stream using the given Charset.
      Parameters:
      os - the target output stream.
      data - the data.
      charset - The Charset to be used to encode the String
      Returns:
      the ASCII bytes.
      Throws:
      IOException - if an I/O error occurs.
      See Also:
    • 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:
    • 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:
    • 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: