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
  • Field Details

  • 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.
    • requireMarkable

      private static void requireMarkable(InputStream in)
      Rejects a stream that cannot be inspected without consuming it.
      Parameters:
      in - The stream to inspect.
      Throws:
      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.
    • readExtensionPayload

      private byte[] readExtensionPayload() throws IOException
      Reads the payload of the extension header just read, leaving the stream positioned on the header that follows it.
      Returns:
      The payload bytes. Not null.
      Throws:
      IOException - Thrown if the payload is larger than MAX_EXTENSION_BYTES or the archive ends inside it.
    • readRecords

      private void readRecords(byte[] payload, boolean global) throws IOException
      Reads a pax extended header payload, which is a sequence of "<length> <keyword>=<value>\n" records. Each length counts the complete record, including the length digits, blank, and newline. Records are parsed from raw bytes because the length is a byte count and a multibyte value would shift later records.
      Parameters:
      payload - The raw extended header payload.
      global - Whether this is a global header, which applies to all following entries.
      Throws:
      IOException - Thrown if a record is malformed, if a global header contains a keyword that would change the entries after it, or if the entry is sparse.
    • decodeUtf8

      private static String decodeUtf8(byte[] bytes, int offset, int length, String subject) throws IOException
      Decodes archive text without replacing malformed input, which would silently change an archive path.
      Parameters:
      bytes - The bytes containing the text.
      offset - The first byte to decode.
      length - The number of bytes to decode.
      subject - The field name to use in an error message.
      Returns:
      The decoded text. Not null.
      Throws:
      IOException - Thrown if the bytes are not valid UTF-8.
    • apply

      private void apply(String keyword, String value, boolean global) throws IOException
      Applies one pax record. Only path and size change what this reader reports, so other keywords are ignored, except sparse entries, which cannot be unpacked.
      Parameters:
      keyword - The record's keyword.
      value - The record's value.
      global - Whether the record came from a global header.
      Throws:
      IOException - Thrown if the entry is sparse, if a global header contains a keyword that would change the entries after it, or if size is not a number.
    • trimNul

      private static String trimNul(String value)
      Drops everything from the first NUL onward, which is how a GNU long-name header terminates the name it contains.
      Parameters:
      value - The decoded payload.
      Returns:
      The name without its terminator. Not null.
    • readName

      private String readName() throws IOException
      Reads the current header's entry name. On a POSIX ustar header a non-empty name prefix is joined to the name field with /, which is how a name longer than the 100-byte name field is stored when no extension header contains it.
      Returns:
      The entry name. Not null.
      Throws:
      IOException - Thrown if the stored name or prefix is not valid UTF-8.
    • field

      private String field(int offset, int length, String subject) throws IOException
      Reads a NUL-terminated text field of the current header.
      Parameters:
      offset - The field's offset in the header block.
      length - The field's length in bytes.
      subject - The field name to use in an error message.
      Returns:
      The field content up to its first NUL, decoded as UTF-8. Not null.
      Throws:
      IOException - Thrown if the field is not valid UTF-8.
    • 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.
    • isHeader

      private static boolean isHeader(byte[] block)
      Checks whether a full 512-byte block reads as a tar entry header, which it does when it starts with a name and its stored checksum matches the block. Both classic and ustar headers contain that checksum, so it identifies both without relying on the ustar magic, which arbitrary content can also contain.
      Parameters:
      block - The block to inspect. Must be 512 bytes long.
      Returns:
      true if the block reads as a tar header.
    • hasValidChecksum

      private static boolean hasValidChecksum(byte[] block)
      Verifies a header block against the checksum stored in it. The checksum is the sum of every header byte with the checksum field itself read as eight blanks. Historical writers used signed-byte sums, so both totals are accepted.
      Parameters:
      block - The header block to verify. Must be 512 bytes long.
      Returns:
      true if the stored checksum is well formed and matches the block.
    • hasPosixUstarMagic

      private static boolean hasPosixUstarMagic(byte[] block)
      Checks for the POSIX ustar magic specifically, which is "ustar" followed by a NUL. GNU writes "ustar" followed by two blanks and a NUL in the same field, and its headers must not be read as ustar: GNU stores atime at the offset ustar gives to the name prefix, so a GNU incremental archive would otherwise deliver every entry under a directory named after an octal timestamp.
      Parameters:
      block - The block to inspect. Must be 512 bytes long.
      Returns:
      true if the block contains the POSIX ustar magic.
    • isOctalDigit

      private static boolean isOctalDigit(byte b)
      Parameters:
      b - The byte to classify.
      Returns:
      true if the byte is one of the digits 0 to 7.
    • readBlock

      private boolean readBlock() throws IOException
      Fills the header buffer with the next 512-byte block.
      Returns:
      true when a full block was read, false at a clean end of the stream before any byte of the block.
      Throws:
      IOException - Thrown if the stream ends inside the block.
    • isEndBlock

      private boolean isEndBlock()
      Returns:
      true if the current header buffer is one of the all-zero blocks that terminate a tar archive.
    • parseSize

      private long parseSize() throws IOException
      Parses the size field of the current header, tolerating NUL and blank padding around the octal digits. A field with its leading bit set is in the base-256 encoding instead, and is read by parseBase256Size().
      Returns:
      The entry size in bytes.
      Throws:
      IOException - Thrown if the field contains a character that is not an octal digit, a blank, or NUL padding, or if the base-256 form is negative or too large for a long.
    • parseBase256Size

      private long parseBase256Size() throws IOException
      Reads a size field in the base-256 encoding, which GNU writes when a value does not fit the 11 octal digits the field otherwise contains, so entries of 8 GiB and above can state their length.

      The leading bit marks the encoding, the next bit is the sign, and the remaining bits of that byte followed by all later bytes form a big-endian two's complement number. Negative sizes are rejected.

      Returns:
      The entry size in bytes.
      Throws:
      IOException - Thrown if the encoded value is negative or does not fit a long.
    • padding

      private long padding(long entrySize)
      Parameters:
      entrySize - The size of an entry's content in bytes.
      Returns:
      The number of padding bytes that align the entry to the next 512-byte block boundary.
    • skip

      private void skip(long bytes) throws IOException
      Consumes and discards the given number of bytes from the underlying stream.
      Parameters:
      bytes - The number of bytes to discard.
      Throws:
      IOException - Thrown if the stream ends before all bytes were consumed.