Class TarStream

java.lang.Object
opennlp.tools.util.archive.TarStream

@Internal public final class TarStream extends Object
A forward-only reader for classic v7, POSIX ustar, GNU, and pax tar streams. next() advances to the following entry and entryStream() exposes only the current entry's bytes.

The reader validates header checksums, supports ustar name prefixes, GNU long names, pax path and size records, and GNU base-256 sizes. Sparse entries and global pax records that change paths or sizes are rejected because this reader cannot reproduce their content or global semantics.

Since:
3.0.0
  • Constructor Details

    • TarStream

      public TarStream(InputStream in)
      Initializes the reader.
      Parameters:
      in - The tar content. Not null. Not closed by this class.
      Throws:
      IllegalArgumentException - Thrown if in is null.
    • TarStream

      public TarStream(InputStream in, long maxEntries)
      Initializes a reader with an archive-entry limit. Extension headers count toward the limit.
      Parameters:
      in - The tar content. Not null. Not closed by this class.
      maxEntries - The maximum number of archive headers to read. Must be positive.
      Throws:
      IllegalArgumentException - Thrown if in is null or maxEntries is not positive.
  • Method Details

    • startsWithHeader

      public static boolean startsWithHeader(InputStream in) throws IOException
      Checks whether the given stream is positioned at a tar entry header, leaving its position unchanged.
      Parameters:
      in - The stream to inspect. Not null and must support mark and reset.
      Returns:
      true if the next 512 bytes read as a tar header, false if they do not or if fewer than 512 bytes are available.
      Throws:
      IOException - Thrown if reading from or repositioning the stream fails.
      IllegalArgumentException - Thrown if in is null or does not support mark and reset.
    • next

      public boolean next() throws IOException
      Advances to the next entry.
      Returns:
      true if an entry is available, false at the end of the archive.
      Throws:
      IOException - Thrown if the archive is truncated or a header is malformed.
    • name

      public String name()
      Returns:
      The current entry's name as stored in the archive. Not null after a successful next().
    • size

      public long size()
      Returns:
      The current entry's size in bytes.
    • isFile

      public boolean isFile()
      Returns:
      true if the current entry is a regular file.
    • entryStream

      public InputStream entryStream()
      Opens the current entry's content.
      Returns:
      A stream over exactly this entry's bytes; reading past the end returns end of stream, and a zero-length read returns 0 as InputStream.read(byte[], int, int) requires. Not null. Closing it is not required.