Top Description Fields Constructors Methods
org.apache.commons.compress.archivers.tar

public Class TarArchiveInputStream

extends ArchiveInputStream<TarArchiveEntry>
Class Inheritance
Imports
java.io.ByteArrayOutputStream, .FileInputStream, .IOException, .InputStream, java.util.ArrayList, .Arrays, .HashMap, .List, .Map, org.apache.commons.compress.archivers.ArchiveEntry, .ArchiveInputStream, org.apache.commons.compress.archivers.zip.ZipEncoding, .ZipEncodingHelper, org.apache.commons.compress.utils.ArchiveUtils, .BoundedInputStream, .IOUtils

The TarInputStream reads a UNIX tar archive as an InputStream. methods are provided to position at each successive entry in the archive, and the read each entry as a normal input stream using read().

Field Summary

Modifier and TypeField and Description
private boolean
atEof

True if stream is at EOF.

private final int
blockSize

The size of a block.

private TarArchiveEntry
currEntry

The meta-data about the current entry.

private int
currentSparseInputStreamIndex

The index of current input stream being read when reading sparse entries.

private long
entryOffset

How far into the entry the stream is at.

private long
entrySize

Size of the current .

private Map<String, String>
globalPaxHeaders

The global PAX header.

private final List<TarArchiveStructSparse>
globalSparseHeaders

The global sparse headers, this is only used in PAX Format 0.X.

private final boolean
private final byte[]
recordBuffer

The buffer to store the TAR header.

private final int
recordSize

The size the TAR header.

private static final int
private final byte[]
private List<InputStream>
sparseInputStreams

Input streams for reading sparse entries.

private final ZipEncoding
zipEncoding

The encoding of the file.

Constructor Summary

AccessConstructor and Description
public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final boolean
when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to TarArchiveEntry#UNKNOWN. When set to false such illegal fields cause an exception instead.
lenient
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final int
the block size to use
blockSize
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final int
the block size to use
blockSize
,
final int
the record size to use
recordSize
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final int
the block size to use
blockSize
,
final int
the record size to use
recordSize
,
final String
name of the encoding to use for file names
encoding
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final int
the block size to use
blockSize
,
final int
the record size to use
recordSize
,
final String
name of the encoding to use for file names
encoding
,
final boolean
when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to TarArchiveEntry#UNKNOWN. When set to false such illegal fields cause an exception instead.
lenient
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final int
the block size to use
blockSize
,
final String
name of the encoding to use for file names
encoding
)

Constructs a new instance.

public
TarArchiveInputStream(final InputStream
the input stream to use
inputStream
,
final String
name of the encoding to use for file names
encoding
)

Constructs a new instance.

Method Summary

Modifier and TypeMethod and Description
private void
public int

Returns:

The number of available bytes for the current entry.
available
()

Overrides java.io.FilterInputStream.available.

Gets the available data that can be read from the current entry in the archive.
private void
buildSparseInputStreams()

Build the input streams consisting of all-zero input streams and non-zero input streams.

public boolean

Returns:

The implementation will return true if the ArchiveEntry is an instance of TarArchiveEntry
canReadEntryData
(final ArchiveEntry
the entry to test
ae
)

Overrides org.apache.commons.compress.archivers.ArchiveInputStream.canReadEntryData.

Whether this class is able to read the given entry.
public void
close()

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Closes this stream.
private void
consumeRemainderOfLastBlock()

This method is invoked once the end of the archive is hit, it tries to consume the remaining bytes under the assumption that the tool creating this archive has padded the last block.

private long

Returns:

number of bytes actually skipped
getActuallySkipped
(final long
available bytes returned by inputStream.available()
available
,
final long
skipped bytes returned by inputStream.skip()
skipped
,
final long
bytes expected to skip
expected
)

For FileInputStream, the skip always return the number you input, so we need the available bytes to determine how many bytes are actually skipped

public TarArchiveEntry

Returns:

The current Archive Entry
getCurrentEntry
()

Gets the current TAR Archive Entry that this input stream is processing

protected byte[]

Returns:

The next entry in the archive as long name data, or null.
getLongNameData
()

Gets the next entry in this tar archive as long name data.

public TarArchiveEntry

Returns:

the next entry, or null if there are no more entries
getNextEntry
()

Implements abstract org.apache.commons.compress.archivers.ArchiveInputStream.getNextEntry.

Returns the next Archive Entry in this Stream.
public TarArchiveEntry

Returns:

The next TarEntry in the archive, or null.
getNextTarEntry
()

Deprecated Use getNextEntry().
Gets the next entry in this tar archive.
private byte[]

Returns:

The next header in the archive, or null.
getRecord
()

Gets the next record in this tar archive.

public int

Returns:

The TarBuffer record size.
getRecordSize
()

Gets the record size being used by this stream's buffer.

protected final boolean
private boolean
protected boolean

Returns:

true if the record data is an End of Archive
isEOFRecord
(final byte[]
The record data to check.
record
)

Determine if an archive record indicate End of Archive.

public synchronized void
mark(final int
The limit to mark.
markLimit
)

Overrides java.io.FilterInputStream.mark.

Since we do not support marking just yet, we do nothing.
public boolean

Returns:

False.
markSupported
()

Overrides java.io.FilterInputStream.markSupported.

Since we do not support marking just yet, we return false.
public static boolean

Returns:

true, if this stream is a tar archive stream, false otherwise
matches
(final byte[]
the bytes to check
signature
,
final int
the number of bytes to check
length
)

Checks if the signature matches what is expected for a tar file.

private void
paxHeaders()

For PAX Format 0.0, the sparse headers(GNU.sparse.offset and GNU.sparse.numbytes) may appear multi times, and they look like: GNU.sparse.size=size GNU.sparse.numblocks=numblocks repeat numblocks times GNU.sparse.offset=offset GNU.sparse.numbytes=numbytes end repeat For PAX Format 0.1, the sparse headers are stored in a single variable : GNU.sparse.map GNU.sparse.map Map of non-null data chunks.

public int

Returns:

The number of bytes read, or -1 at EOF.
read
(final byte[]
The buffer into which to place bytes read.
buf
,
final int
The offset at which to place bytes read.
offset
,
int
The number of bytes to read.
numToRead
)

Overrides java.io.FilterInputStream.read.

Reads bytes from the current tar archive entry.
private void
private void
readOldGNUSparse()

Adds the sparse chunks from the current entry to the sparse chunks, including any additional sparse entries following the current entry.

protected byte[]

Returns:

The record data or null if EOF has been hit.
readRecord
()

Read a record from the input stream and return the data.

private int

Returns:

The number of bytes read, or -1 at EOF.
readSparse
(final byte[]
The buffer into which to place bytes read.
buf
,
final int
The offset at which to place bytes read.
offset
,
final int
The number of bytes to read.
numToRead
)

For sparse tar entries, there are many "holes"(consisting of all 0) in the file.

public synchronized void
reset()

Overrides java.io.FilterInputStream.reset.

Since we do not support marking just yet, we do nothing.
protected final void
setAtEOF(final boolean b)

protected final void
public long

Returns:

the actual number of bytes skipped.
skip
(final long
the number of bytes to be skipped.
n
)

Overrides java.io.FilterInputStream.skip.

Skips over and discards n bytes of data from this input stream.
private void
skipRecordPadding()

The last record block should be written at the full size, so skip any additional space used to fill a record after an entry.

private long

Returns:

actual bytes of data skipped
skipSparse
(final long
bytes of data to skip
n
)

Skip n bytes from current input stream, if the current input stream doesn't have enough data to skip, jump to the next input stream and skip the rest bytes, keep doing this until total n bytes are skipped or the input streams are all skipped

private void
tryToConsumeSecondEOFRecord()

Tries to read the next record rewinding the stream if it is not an EOF record.

Inherited from org.apache.commons.compress.archivers.ArchiveInputStream:
countcountgetBytesReadgetCharsetgetCountpushedBackBytesread

Field Detail

atEofback to summary
private boolean atEof

True if stream is at EOF.

blockSizeback to summary
private final int blockSize

The size of a block.

currEntryback to summary
private TarArchiveEntry currEntry

The meta-data about the current entry.

currentSparseInputStreamIndexback to summary
private int currentSparseInputStreamIndex

The index of current input stream being read when reading sparse entries.

entryOffsetback to summary
private long entryOffset

How far into the entry the stream is at.

entrySizeback to summary
private long entrySize

Size of the current .

globalPaxHeadersback to summary
private Map<String, String> globalPaxHeaders

The global PAX header.

globalSparseHeadersback to summary
private final List<TarArchiveStructSparse> globalSparseHeaders

The global sparse headers, this is only used in PAX Format 0.X.

lenientback to summary
private final boolean lenient
recordBufferback to summary
private final byte[] recordBuffer

The buffer to store the TAR header.

recordSizeback to summary
private final int recordSize

The size the TAR header.

SMALL_BUFFER_SIZEback to summary
private static final int SMALL_BUFFER_SIZE
smallBufback to summary
private final byte[] smallBuf
sparseInputStreamsback to summary
private List<InputStream> sparseInputStreams

Input streams for reading sparse entries.

zipEncodingback to summary
private final ZipEncoding zipEncoding

The encoding of the file.

Constructor Detail

TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final boolean lenient)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

lenient:boolean

when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to TarArchiveEntry#UNKNOWN. When set to false such illegal fields cause an exception instead.

Since
1.19
TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final int blockSize)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

blockSize:int

the block size to use

TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final int recordSize)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

blockSize:int

the block size to use

recordSize:int

the record size to use

TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final int recordSize, final String encoding)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

blockSize:int

the block size to use

recordSize:int

the record size to use

encoding:String

name of the encoding to use for file names

Since
1.4
TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final int recordSize, final String encoding, final boolean lenient)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

blockSize:int

the block size to use

recordSize:int

the record size to use

encoding:String

name of the encoding to use for file names

lenient:boolean

when set to true illegal values for group/userid, mode, device numbers and timestamp will be ignored and the fields set to TarArchiveEntry#UNKNOWN. When set to false such illegal fields cause an exception instead.

Since
1.19
TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final int blockSize, final String encoding)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

blockSize:int

the block size to use

encoding:String

name of the encoding to use for file names

Since
1.4
TarArchiveInputStreamback to summary
public TarArchiveInputStream(final InputStream inputStream, final String encoding)

Constructs a new instance.

Parameters
inputStream:InputStream

the input stream to use

encoding:String

name of the encoding to use for file names

Since
1.4

Method Detail

applyPaxHeadersToCurrentEntryback to summary
private void applyPaxHeadersToCurrentEntry(final Map<String, String> headers, final List<TarArchiveStructSparse> sparseHeaders) throws IOException
availableback to summary
public int available() throws IOException

Overrides java.io.FilterInputStream.available.

Gets the available data that can be read from the current entry in the archive. This does not indicate how much data is left in the entire archive, only in the current entry. This value is determined from the entry's size header field and the amount of data already read from the current entry. Integer.MAX_VALUE is returned in case more than Integer.MAX_VALUE bytes are left in the current entry in the archive.

Returns:int

The number of available bytes for the current entry.

Annotations
@Override
Exceptions
IOException:
for signature
buildSparseInputStreamsback to summary
private void buildSparseInputStreams() throws IOException

Build the input streams consisting of all-zero input streams and non-zero input streams. When reading from the non-zero input streams, the data is actually read from the original input stream. The size of each input stream is introduced by the sparse headers. NOTE : Some all-zero input streams and non-zero input streams have the size of 0. We DO NOT store the 0 size input streams because they are meaningless.

canReadEntryDataback to summary
public boolean canReadEntryData(final ArchiveEntry ae)

Overrides org.apache.commons.compress.archivers.ArchiveInputStream.canReadEntryData.

Whether this class is able to read the given entry.

Parameters
ae:ArchiveEntry

Doc from org.apache.commons.compress.archivers.ArchiveInputStream.canReadEntryData.

the entry to test

Returns:boolean

The implementation will return true if the ArchiveEntry is an instance of TarArchiveEntry

Annotations
@Override
closeback to summary
public void close() throws IOException

Overrides java.io.FilterInputStream.close.

Implements java.io.Closeable.close.

Closes this stream. Calls the TarBuffer's close() method.

Annotations
@Override
Exceptions
IOException:
on error
consumeRemainderOfLastBlockback to summary
private void consumeRemainderOfLastBlock() throws IOException

This method is invoked once the end of the archive is hit, it tries to consume the remaining bytes under the assumption that the tool creating this archive has padded the last block.

getActuallySkippedback to summary
private long getActuallySkipped(final long available, final long skipped, final long expected) throws IOException

For FileInputStream, the skip always return the number you input, so we need the available bytes to determine how many bytes are actually skipped

Parameters
available:long

available bytes returned by inputStream.available()

skipped:long

skipped bytes returned by inputStream.skip()

expected:long

bytes expected to skip

Returns:long

number of bytes actually skipped

Exceptions
IOException:
if a truncated tar archive is detected
getCurrentEntryback to summary
public TarArchiveEntry getCurrentEntry()

Gets the current TAR Archive Entry that this input stream is processing

Returns:TarArchiveEntry

The current Archive Entry

getLongNameDataback to summary
protected byte[] getLongNameData() throws IOException

Gets the next entry in this tar archive as long name data.

Returns:byte[]

The next entry in the archive as long name data, or null.

Exceptions
IOException:
on error
getNextEntryback to summary
public TarArchiveEntry getNextEntry() throws IOException

Implements abstract org.apache.commons.compress.archivers.ArchiveInputStream.getNextEntry.

Returns the next Archive Entry in this Stream.

Returns:TarArchiveEntry

the next entry, or null if there are no more entries

Annotations
@Override
Exceptions
IOException:
if the next entry could not be read
getNextTarEntryback to summary
public TarArchiveEntry getNextTarEntry() throws IOException

Deprecated

Use getNextEntry().

Gets the next entry in this tar archive. This will skip over any remaining data in the current entry, if there is one, and place the input stream at the header of the next entry, and read the header and instantiate a new TarEntry from the header bytes and return that entry. If there are no more entries in the archive, null will be returned to indicate that the end of the archive has been reached.

Returns:TarArchiveEntry

The next TarEntry in the archive, or null.

Annotations
@Deprecated
Exceptions
IOException:
on error
getRecordback to summary
private byte[] getRecord() throws IOException

Gets the next record in this tar archive. This will skip over any remaining data in the current entry, if there is one, and place the input stream at the header of the next entry.

If there are no more entries in the archive, null will be returned to indicate that the end of the archive has been reached. At the same time the hasHitEOF marker will be set to true.

Returns:byte[]

The next header in the archive, or null.

Exceptions
IOException:
on error
getRecordSizeback to summary
public int getRecordSize()

Gets the record size being used by this stream's buffer.

Returns:int

The TarBuffer record size.

isAtEOFback to summary
protected final boolean isAtEOF()
isDirectoryback to summary
private boolean isDirectory()
isEOFRecordback to summary
protected boolean isEOFRecord(final byte[] record)

Determine if an archive record indicate End of Archive. End of archive is indicated by a record that consists entirely of null bytes.

Parameters
record:byte[]

The record data to check.

Returns:boolean

true if the record data is an End of Archive

markback to summary
public synchronized void mark(final int markLimit)

Overrides java.io.FilterInputStream.mark.

Since we do not support marking just yet, we do nothing.

Parameters
markLimit:int

The limit to mark.

Annotations
@Override
markSupportedback to summary
public boolean markSupported()

Overrides java.io.FilterInputStream.markSupported.

Since we do not support marking just yet, we return false.

Returns:boolean

False.

Annotations
@Override
matchesback to summary
public static boolean matches(final byte[] signature, final int length)

Checks if the signature matches what is expected for a tar file.

Parameters
signature:byte[]

the bytes to check

length:int

the number of bytes to check

Returns:boolean

true, if this stream is a tar archive stream, false otherwise

paxHeadersback to summary
private void paxHeaders() throws IOException

For PAX Format 0.0, the sparse headers(GNU.sparse.offset and GNU.sparse.numbytes) may appear multi times, and they look like: GNU.sparse.size=size GNU.sparse.numblocks=numblocks repeat numblocks times GNU.sparse.offset=offset GNU.sparse.numbytes=numbytes end repeat For PAX Format 0.1, the sparse headers are stored in a single variable : GNU.sparse.map GNU.sparse.map Map of non-null data chunks. It is a string consisting of comma-separated values "offset,size[,offset-1,size-1...]" For PAX Format 1.X: The sparse map itself is stored in the file data block, preceding the actual file data. It consists of a series of decimal numbers delimited by newlines. The map is padded with nulls to the nearest block boundary. The first number gives the number of entries in the map. Following are map entries, each one consisting of two numbers giving the offset and size of the data block it describes.

readback to summary
public int read(final byte[] buf, final int offset, int numToRead) throws IOException

Overrides java.io.FilterInputStream.read.

Reads bytes from the current tar archive entry. This method is aware of the boundaries of the current entry in the archive and will deal with them as if they were this stream's start and EOF.

Parameters
buf:byte[]

The buffer into which to place bytes read.

offset:int

The offset at which to place bytes read.

numToRead:int

The number of bytes to read.

Returns:int

The number of bytes read, or -1 at EOF.

Annotations
@Override
Exceptions
IOException:
on error
readGlobalPaxHeadersback to summary
private void readGlobalPaxHeaders() throws IOException
readOldGNUSparseback to summary
private void readOldGNUSparse() throws IOException

Adds the sparse chunks from the current entry to the sparse chunks, including any additional sparse entries following the current entry.

Exceptions
IOException:
on error
readRecordback to summary
protected byte[] readRecord() throws IOException

Read a record from the input stream and return the data.

Returns:byte[]

The record data or null if EOF has been hit.

Exceptions
IOException:
on error
readSparseback to summary
private int readSparse(final byte[] buf, final int offset, final int numToRead) throws IOException

For sparse tar entries, there are many "holes"(consisting of all 0) in the file. Only the non-zero data is stored in tar files, and they are stored separately. The structure of non-zero data is introduced by the sparse headers using the offset, where a block of non-zero data starts, and numbytes, the length of the non-zero data block. When reading sparse entries, the actual data is read out with "holes" and non-zero data combined together according to the sparse headers.

Parameters
buf:byte[]

The buffer into which to place bytes read.

offset:int

The offset at which to place bytes read.

numToRead:int

The number of bytes to read.

Returns:int

The number of bytes read, or -1 at EOF.

Exceptions
IOException:
on error
resetback to summary
public synchronized void reset()

Overrides java.io.FilterInputStream.reset.

Since we do not support marking just yet, we do nothing.

Annotations
@Override
setAtEOFback to summary
protected final void setAtEOF(final boolean b)
setCurrentEntryback to summary
protected final void setCurrentEntry(final TarArchiveEntry e)
skipback to summary
public long skip(final long n) throws IOException

Overrides java.io.FilterInputStream.skip.

Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file or end of entry before n bytes have been skipped; are only two possibilities. The actual number of bytes skipped is returned. If n is negative, no bytes are skipped.

Parameters
n:long

the number of bytes to be skipped.

Returns:long

the actual number of bytes skipped.

Annotations
@Override
Exceptions
IOException:
if a truncated tar archive is detected or some other I/O error occurs
skipRecordPaddingback to summary
private void skipRecordPadding() throws IOException

The last record block should be written at the full size, so skip any additional space used to fill a record after an entry.

Exceptions
IOException:
if a truncated tar archive is detected
skipSparseback to summary
private long skipSparse(final long n) throws IOException

Skip n bytes from current input stream, if the current input stream doesn't have enough data to skip, jump to the next input stream and skip the rest bytes, keep doing this until total n bytes are skipped or the input streams are all skipped

Parameters
n:long

bytes of data to skip

Returns:long

actual bytes of data skipped

tryToConsumeSecondEOFRecordback to summary
private void tryToConsumeSecondEOFRecord() throws IOException

Tries to read the next record rewinding the stream if it is not an EOF record.

This is meant to protect against cases where a tar implementation has written only one EOF record when two are expected. Actually this won't help since a non-conforming implementation likely won't fill full blocks consisting of - by default - ten records either so we probably have already read beyond the archive anyway.