Package opennlp.tools.util.archive
Class TarStream
java.lang.Object
opennlp.tools.util.archive.TarStream
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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intprivate static final intThe two zero blocks that end an archive.private booleanprivate longprivate final byte[]private final InputStreamprivate static final Stringprivate static final Stringprivate static final intprivate static final Stringprivate static final intprivate final longprivate Stringprivate static final intprivate StringThe name an extension header supplied for the entry that follows it, else null.private longThe size a pax header supplied for the entry that follows it, else-1.private static final intprivate static final intprivate longprivate longprivate static final intprivate static final intprivate static final Stringprivate charprivate static final charprivate static final charprivate static final charprivate static final intprivate static final charprivate static final charprivate static final charprivate static final charprivate static final String -
Constructor Summary
ConstructorsConstructorDescriptionTarStream(InputStream in) Initializes the reader.TarStream(InputStream in, long maxEntries) Initializes a reader with an archive-entry limit. -
Method Summary
Modifier and TypeMethodDescriptionprivate voidApplies one pax record.private static StringdecodeUtf8(byte[] bytes, int offset, int length, String subject) Decodes archive text without replacing malformed input, which would silently change an archive path.Opens the current entry's content.private StringReads a NUL-terminated text field of the current header.private static booleanhasPosixUstarMagic(byte[] block) Checks for the POSIX ustar magic specifically, which is"ustar"followed by a NUL.private static booleanhasValidChecksum(byte[] block) Verifies a header block against the checksum stored in it.static booleanChecks whether the given stream is positioned at an archive without entries, leaving its position unchanged.private booleanbooleanisFile()Returnstrueif the current entry is a regular file.private static booleanisHeader(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.private static booleanisOctalDigit(byte b) name()Returns the current entry's name as stored in the archive; notnullafter a successfulnext().booleannext()Advances to the next entry.private longpadding(long entrySize) private longReads 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.private longParses the size field of the current header, tolerating NUL and blank padding around the octal digits.private booleanFills the header buffer with the next 512-byte block.private byte[]Reads the payload of the extension header just read, leaving the stream positioned on the header that follows it.private StringreadName()Reads the current header's entry name.private voidreadRecords(byte[] payload, boolean global) Reads a pax extended header payload, which is a sequence of"<length> <keyword>=<value>\n"records.private static voidRejects a stream that cannot be inspected without consuming it.longsize()Returns the current entry's size in bytes.private voidskip(long bytes) Consumes and discards the given number of bytes from the underlying stream.static booleanChecks whether the given stream is positioned at a tar entry header, leaving its position unchanged.private static StringDrops everything from the first NUL onward, which is how a GNU long-name header terminates the name it contains.
-
Field Details
-
BLOCK
private static final int BLOCK- See Also:
-
END_OF_ARCHIVE_LENGTH
private static final int END_OF_ARCHIVE_LENGTHThe two zero blocks that end an archive.- See Also:
-
NAME_LENGTH
private static final int NAME_LENGTH- See Also:
-
CHECKSUM_OFFSET
private static final int CHECKSUM_OFFSET- See Also:
-
CHECKSUM_LENGTH
private static final int CHECKSUM_LENGTH- See Also:
-
SIZE_OFFSET
private static final int SIZE_OFFSET- See Also:
-
SIZE_LENGTH
private static final int SIZE_LENGTH- See Also:
-
TYPE_OFFSET
private static final int TYPE_OFFSET- See Also:
-
MAGIC_OFFSET
private static final int MAGIC_OFFSET- See Also:
-
PREFIX_OFFSET
private static final int PREFIX_OFFSET- See Also:
-
PREFIX_LENGTH
private static final int PREFIX_LENGTH- See Also:
-
USTAR_MAGIC
- See Also:
-
TYPE_REGULAR_FILE
private static final char TYPE_REGULAR_FILE- See Also:
-
TYPE_REGULAR_FILE_CLASSIC
private static final char TYPE_REGULAR_FILE_CLASSIC- See Also:
-
TYPE_GNU_LONG_NAME
private static final char TYPE_GNU_LONG_NAME- See Also:
-
TYPE_GNU_LONG_LINK
private static final char TYPE_GNU_LONG_LINK- See Also:
-
TYPE_GNU_SPARSE
private static final char TYPE_GNU_SPARSE- See Also:
-
TYPE_PAX_EXTENDED
private static final char TYPE_PAX_EXTENDED- See Also:
-
TYPE_PAX_GLOBAL
private static final char TYPE_PAX_GLOBAL- See Also:
-
MAX_EXTENSION_BYTES
private static final int MAX_EXTENSION_BYTES- See Also:
-
MALFORMED_RECORD
- See Also:
-
KEYWORD_PATH
- See Also:
-
KEYWORD_SIZE
- See Also:
-
SPARSE_PREFIX
- See Also:
-
BASE_256_MARKER
private static final int BASE_256_MARKER- See Also:
-
BASE_256_NEGATIVE
private static final int BASE_256_NEGATIVE- See Also:
-
BASE_256_FIRST_BYTE_BITS
private static final int BASE_256_FIRST_BYTE_BITS- See Also:
-
in
-
maxEntries
private final long maxEntries -
header
private final byte[] header -
name
-
size
private long size -
type
private char type -
remaining
private long remaining -
entries
private long entries -
ended
private boolean ended -
pendingPath
The name an extension header supplied for the entry that follows it, else null. -
pendingSize
private long pendingSizeThe size a pax header supplied for the entry that follows it, else-1.
-
-
Constructor Details
-
TarStream
Initializes the reader.- Parameters:
in- The tar content. Notnull. Not closed by this class.- Throws:
IllegalArgumentException- Thrown ifinisnull.
-
TarStream
Initializes a reader with an archive-entry limit. Extension headers count toward the limit.- Parameters:
in- The tar content. Notnull. Not closed by this class.maxEntries- The maximum number of archive headers to read. Must be positive.- Throws:
IllegalArgumentException- Thrown ifinisnullormaxEntriesis not positive.
-
-
Method Details
-
startsWithHeader
Checks whether the given stream is positioned at a tar entry header, leaving its position unchanged.- Parameters:
in- The stream to inspect. Notnulland must supportmarkandreset.- Returns:
trueif the next 512 bytes read as a tar header,falseif they do not or if fewer than 512 bytes are available.- Throws:
IOException- Thrown if reading from or repositioning the stream fails.IllegalArgumentException- Thrown ifinisnullor does not support mark and reset.
-
isEmptyArchive
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, whichstartsWithHeader(InputStream)does not recognize.- Parameters:
in- The stream to inspect. Notnulland must supportmarkandreset.- Returns:
trueif the next 1024 bytes are all zero,falseif they are not or if fewer than 1024 bytes are available.- Throws:
IOException- Thrown if reading from or repositioning the stream fails.IllegalArgumentException- Thrown ifinisnullor does not support mark and reset.
-
requireMarkable
Rejects a stream that cannot be inspected without consuming it.- Parameters:
in- The stream to inspect.- Throws:
IllegalArgumentException- Thrown ifinisnullor does not support mark and reset.
-
next
Advances to the next entry.- Returns:
trueif an entry is available,falseat the end of the archive.- Throws:
IOException- Thrown if the archive is truncated or a header is malformed.
-
readExtensionPayload
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 thanMAX_EXTENSION_BYTESor the archive ends inside it.
-
readRecords
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
Applies one pax record. Onlypathandsizechange 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 ifsizeis not a number.
-
trimNul
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
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
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
Returns the current entry's name as stored in the archive; notnullafter a successfulnext().- Returns:
- the current entry's name as stored in the archive; not
nullafter a successfulnext()
-
size
public long size()Returns the current entry's size in bytes.- Returns:
- the current entry's size in bytes
-
isFile
public boolean isFile()Returnstrueif the current entry is a regular file.- Returns:
trueif the current entry is a regular file
-
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
0asInputStream.read(byte[], int, int)requires. Notnull. 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:
trueif 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:
trueif 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 storesatimeat 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:
trueif the block contains the POSIX ustar magic.
-
isOctalDigit
private static boolean isOctalDigit(byte b) - Parameters:
b- The byte to classify.- Returns:
trueif the byte is one of the digits0to7.
-
readBlock
Fills the header buffer with the next 512-byte block.- Returns:
truewhen a full block was read,falseat 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:
trueif the current header buffer is one of the all-zero blocks that terminate a tar archive.
-
parseSize
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 byparseBase256Size().- 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 along.
-
parseBase256Size
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 along.
-
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
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.
-