public class TarArchiveInputStream extends ArchiveInputStream
Modifier and Type | Class and Description |
---|---|
private static class |
TarArchiveInputStream.TarArchiveSparseZeroInputStream
This is an inputstream that always return 0,
this is used when reading the "holes" of a sparse file
|
Modifier and Type | Field and Description |
---|---|
private 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
|
(package private) java.lang.String |
encoding |
private long |
entryOffset
How far into the entry the stream is at
|
private long |
entrySize
Size of the current entry
|
private java.util.Map<java.lang.String,java.lang.String> |
globalPaxHeaders |
private java.util.List<TarArchiveStructSparse> |
globalSparseHeaders |
private boolean |
hasHitEOF
True if file has hit EOF
|
private java.io.InputStream |
inputStream
An input stream to read from
|
private boolean |
lenient |
private int |
recordSize
The size the TAR header
|
private static int |
SMALL_BUFFER_SIZE |
private byte[] |
smallBuf |
private java.util.List<java.io.InputStream> |
sparseInputStreams
Input streams for reading sparse entries
|
private ZipEncoding |
zipEncoding
The encoding of the file
|
Constructor and Description |
---|
TarArchiveInputStream(java.io.InputStream is)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
boolean lenient)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
int blockSize)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
int blockSize,
int recordSize)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
int blockSize,
int recordSize,
java.lang.String encoding)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
int blockSize,
int recordSize,
java.lang.String encoding,
boolean lenient)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
int blockSize,
java.lang.String encoding)
Constructor for TarInputStream.
|
TarArchiveInputStream(java.io.InputStream is,
java.lang.String encoding)
Constructor for TarInputStream.
|
Modifier and Type | Method and Description |
---|---|
private void |
applyPaxHeadersToCurrentEntry(java.util.Map<java.lang.String,java.lang.String> headers,
java.util.List<TarArchiveStructSparse> sparseHeaders) |
int |
available()
Get 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.
|
boolean |
canReadEntryData(ArchiveEntry ae)
Whether this class is able to read the given entry.
|
void |
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.
|
TarArchiveEntry |
getCurrentEntry()
Get the current TAR Archive Entry that this input stream is processing
|
protected byte[] |
getLongNameData()
Get the next entry in this tar archive as longname data.
|
ArchiveEntry |
getNextEntry()
Returns the next Archive Entry in this Stream.
|
TarArchiveEntry |
getNextTarEntry()
Get the next entry in this tar archive.
|
private byte[] |
getRecord()
Get the next record in this tar archive.
|
int |
getRecordSize()
Get the record size being used by this stream's buffer.
|
protected boolean |
isAtEOF() |
private boolean |
isDirectory() |
protected boolean |
isEOFRecord(byte[] record)
Determine if an archive record indicate End of Archive.
|
void |
mark(int markLimit)
Since we do not support marking just yet, we do nothing.
|
boolean |
markSupported()
Since we do not support marking just yet, we return false.
|
static boolean |
matches(byte[] signature,
int length)
Checks if the signature matches what is expected for a tar file.
|
private java.util.List<TarArchiveStructSparse> |
parsePAX01SparseHeaders(java.lang.String sparseMap)
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.
|
private java.util.List<TarArchiveStructSparse> |
parsePAX1XSparseHeaders()
For PAX Format 1.X:
The sparse map itself is stored in the file data block, preceding the actual file data.
|
(package private) java.util.Map<java.lang.String,java.lang.String> |
parsePaxHeaders(java.io.InputStream inputStream,
java.util.List<TarArchiveStructSparse> sparseHeaders)
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.
|
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.
|
int |
read(byte[] buf,
int offset,
int numToRead)
Reads bytes from the current tar archive entry.
|
private void |
readGlobalPaxHeaders() |
private long[] |
readLineOfNumberForPax1X(java.io.InputStream inputStream)
For 1.X PAX Format, the sparse headers are stored in the file data block, preceding the actual file data.
|
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[] |
readRecord()
Read a record from the input stream and return the data.
|
private int |
readSparse(byte[] buf,
int offset,
int numToRead)
For sparse tar entries, there are many "holes"(consisting of all 0) in the file.
|
void |
reset()
Since we do not support marking just yet, we do nothing.
|
protected void |
setAtEOF(boolean b) |
protected void |
setCurrentEntry(TarArchiveEntry e) |
long |
skip(long n)
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 |
skipSparse(long 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 a EOF record.
|
count, count, getBytesRead, getCount, pushedBackBytes, read
private static final int SMALL_BUFFER_SIZE
private final byte[] smallBuf
private final int recordSize
private final int blockSize
private boolean hasHitEOF
private long entrySize
private long entryOffset
private final java.io.InputStream inputStream
private java.util.List<java.io.InputStream> sparseInputStreams
private int currentSparseInputStreamIndex
private TarArchiveEntry currEntry
private final ZipEncoding zipEncoding
final java.lang.String encoding
private java.util.Map<java.lang.String,java.lang.String> globalPaxHeaders
private final java.util.List<TarArchiveStructSparse> globalSparseHeaders
private final boolean lenient
public TarArchiveInputStream(java.io.InputStream is)
is
- the input stream to usepublic TarArchiveInputStream(java.io.InputStream is, boolean lenient)
is
- the input stream to uselenient
- 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.public TarArchiveInputStream(java.io.InputStream is, java.lang.String encoding)
is
- the input stream to useencoding
- name of the encoding to use for file namespublic TarArchiveInputStream(java.io.InputStream is, int blockSize)
is
- the input stream to useblockSize
- the block size to usepublic TarArchiveInputStream(java.io.InputStream is, int blockSize, java.lang.String encoding)
is
- the input stream to useblockSize
- the block size to useencoding
- name of the encoding to use for file namespublic TarArchiveInputStream(java.io.InputStream is, int blockSize, int recordSize)
is
- the input stream to useblockSize
- the block size to userecordSize
- the record size to usepublic TarArchiveInputStream(java.io.InputStream is, int blockSize, int recordSize, java.lang.String encoding)
is
- the input stream to useblockSize
- the block size to userecordSize
- the record size to useencoding
- name of the encoding to use for file namespublic TarArchiveInputStream(java.io.InputStream is, int blockSize, int recordSize, java.lang.String encoding, boolean lenient)
is
- the input stream to useblockSize
- the block size to userecordSize
- the record size to useencoding
- name of the encoding to use for file nameslenient
- 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.public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.InputStream
java.io.IOException
- on errorpublic int getRecordSize()
public int available() throws java.io.IOException
available
in class java.io.InputStream
java.io.IOException
- for signaturepublic long skip(long n) throws java.io.IOException
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.skip
in class java.io.InputStream
n
- the number of bytes to be skipped.java.io.IOException
- if some other I/O error occurs.private long skipSparse(long n) throws java.io.IOException
n
- bytes of data to skipjava.io.IOException
public boolean markSupported()
markSupported
in class java.io.InputStream
public void mark(int markLimit)
mark
in class java.io.InputStream
markLimit
- The limit to mark.public void reset()
reset
in class java.io.InputStream
public TarArchiveEntry getNextTarEntry() throws java.io.IOException
java.io.IOException
- on errorprivate void skipRecordPadding() throws java.io.IOException
java.io.IOException
protected byte[] getLongNameData() throws java.io.IOException
java.io.IOException
- on errorprivate byte[] getRecord() throws java.io.IOException
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.
java.io.IOException
- on errorprotected boolean isEOFRecord(byte[] record)
record
- The record data to check.protected byte[] readRecord() throws java.io.IOException
java.io.IOException
- on errorprivate void readGlobalPaxHeaders() throws java.io.IOException
java.io.IOException
private void paxHeaders() throws java.io.IOException
java.io.IOException
private java.util.List<TarArchiveStructSparse> parsePAX01SparseHeaders(java.lang.String sparseMap) throws java.io.IOException
sparseMap
- the sparse map string consisting of comma-separated values "offset,size[,offset-1,size-1...]"java.io.IOException
private java.util.List<TarArchiveStructSparse> parsePAX1XSparseHeaders() throws java.io.IOException
java.io.IOException
private long[] readLineOfNumberForPax1X(java.io.InputStream inputStream) throws java.io.IOException
inputStream
- the input stream of the tar filejava.io.IOException
java.util.Map<java.lang.String,java.lang.String> parsePaxHeaders(java.io.InputStream inputStream, java.util.List<TarArchiveStructSparse> sparseHeaders) throws java.io.IOException
inputstream
- inputstream to read keys and valuessparseHeaders
- used in PAX Format 0.0 & 0.1, as it may appear multi times,
the sparse headers need to be stored in an array, not a mapjava.io.IOException
private void applyPaxHeadersToCurrentEntry(java.util.Map<java.lang.String,java.lang.String> headers, java.util.List<TarArchiveStructSparse> sparseHeaders)
private void readOldGNUSparse() throws java.io.IOException
java.io.IOException
- on errorprivate boolean isDirectory()
public ArchiveEntry getNextEntry() throws java.io.IOException
getNextEntry
in class ArchiveInputStream
null
if there are no more entriesjava.io.IOException
- if the next entry could not be readprivate void tryToConsumeSecondEOFRecord() throws java.io.IOException
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.
java.io.IOException
public int read(byte[] buf, int offset, int numToRead) throws java.io.IOException
read
in class java.io.InputStream
buf
- The buffer into which to place bytes read.offset
- The offset at which to place bytes read.numToRead
- The number of bytes to read.java.io.IOException
- on errorprivate int readSparse(byte[] buf, int offset, int numToRead) throws java.io.IOException
buf
- The buffer into which to place bytes read.offset
- The offset at which to place bytes read.numToRead
- The number of bytes to read.java.io.IOException
- on errorpublic boolean canReadEntryData(ArchiveEntry ae)
May return false if the current entry is a sparse file.
canReadEntryData
in class ArchiveInputStream
ae
- the entry to testpublic TarArchiveEntry getCurrentEntry()
protected final void setCurrentEntry(TarArchiveEntry e)
protected final boolean isAtEOF()
protected final void setAtEOF(boolean b)
private void consumeRemainderOfLastBlock() throws java.io.IOException
java.io.IOException
public static boolean matches(byte[] signature, int length)
signature
- the bytes to checklength
- the number of bytes to checkprivate void buildSparseInputStreams() throws java.io.IOException
java.io.IOException