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 Summary

    Constructors
    Constructor
    Description
    Initializes the reader.
    TarStream(InputStream in, long maxEntries)
    Initializes a reader with an archive-entry limit.
  • Method Summary

    Modifier and Type
    Method
    Description
    Opens the current entry's content.
    static boolean
    Checks whether the given stream is positioned at an archive without entries, leaving its position unchanged.
    boolean
    Returns true if the current entry is a regular file.
    Returns the current entry's name as stored in the archive; not null after a successful next().
    boolean
    Advances to the next entry.
    long
    Returns the current entry's size in bytes.
    static boolean
    Checks whether the given stream is positioned at a tar entry header, leaving its position unchanged.

    Methods inherited from class Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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.
    • isEmptyArchive

      public static boolean isEmptyArchive(InputStream in) throws IOException
      Checks whether the given stream is positioned at an archive without entries, leaving its position unchanged. Such an archive consists of only the two zero blocks that end a tar archive, which startsWithHeader(InputStream) does not recognize.
      Parameters:
      in - The stream to inspect. Not null and must support mark and reset.
      Returns:
      true if the next 1024 bytes are all zero, false if they are not or if fewer than 1024 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().
      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.
      Returns:
      the current entry's size in bytes
    • isFile

      public boolean isFile()
      Returns true if the current entry is a regular file.
      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.