java.lang.Object
xyz.apollosoftware.bibliothiki.compression.ArchiveInputStream
All Implemented Interfaces:
Closeable, AutoCloseable
Direct Known Subclasses:
TapeArchiveInputStream, ZipArchiveInputStream

public abstract class ArchiveInputStream extends Object implements Closeable
Process an archive one entry at a time.
  • Field Details

    • stream

      protected final InputStream stream
      The stream of compressed data.
  • Constructor Details

    • ArchiveInputStream

      public ArchiveInputStream(@NonNull InputStream stream)
      Construct an ArchiveInputStream from the given stream.

      The stream should not be exposed externally to the class hierarchy of the ArchiveInputStream.

      Parameters:
      stream - The stream containing archive data.
  • Method Details

    • getNextEntry

      public abstract @Nullable ArchiveEntry getNextEntry()
      Get the next entry in the archive.

      If there are no (more) entries in the archive, this method returns null, instead.

      Returns:
      The ArchiveEntry, if there is one.
      See Also:
    • writeCurrentEntryTo

      public abstract void writeCurrentEntryTo(@NonNull OutputStream stream) throws IOException
      Write the current archive entry to the given OutputStream.

      The getNextEntry() must have been called prior, or this method will throw a CompressionException.

      A common pattern for writing to files, therefore, would be to invoke getNextEntry() and then write the file data with this method.

      Some implementations may return a CompressionException for format or decompression failures. Implementations that do this will still return IOException when an exception is raised writing to the given output stream. In cases where it is not possible to disambiguate, an IOException is always thrown instead.

      Parameters:
      stream - The stream to write the file data to.
      Throws:
      IOException - If writing to the given OutputStream fails with an IOException.
      CompressionException - If decompressing or reading an entry to write fails.
      See Also:
    • closeCurrentEntry

      public abstract void closeCurrentEntry()
      Close the current entry.

      This may be a no-op for some implementations.

    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • detect

      public static @NonNull ArchiveInputStream detect(@NonNull InputStream stream)
      Automatically detects the archive format (and therefore the ArchiveInputStream implementation) to use for the given stream.
      Parameters:
      stream - The stream to detect the input stream for.
      Returns:
      The ArchiveInputStream implementation for the given stream format.
      Throws:
      CompressionException - If the archive format is not supported.