TrueZIP 6.8.3

de.schlichtherle.io.archive.tar
Class TarInputArchive

java.lang.Object
  extended by de.schlichtherle.io.archive.tar.TarInputArchive
All Implemented Interfaces:
InputArchive, org.apache.tools.tar.TarConstants

public class TarInputArchive
extends Object
implements InputArchive, org.apache.tools.tar.TarConstants

Presents a TarInputStream as a randomly accessible archive.

Warning: The constructor of this class extracts each entry in the archive to a temporary file. This may be very time and space consuming for large archives, but is the fastest implementation for subsequent random access, since there is no way the archive driver could predict the client application's behaviour.

Since:
TrueZIP 6.0
Author:
Christian Schlichtherle

Nested Class Summary
private static class TarInputArchive.TempFileException
          This needs to be a FileNotFoundException in order to signal that the TAR is simply not accessible and not necessarily a false positive.
 
Field Summary
private static int CHECKSUM_OFFSET
           
private  Map entries
          Maps entry names to tar entries [String -> TarEntry].
private  InputArchiveMetaData metaData
           
private static byte[] NULL_RECORD
           
private static String TEMP_FILE_PREFIX
          Prefix for temporary files created by the multiplexer.
 
Fields inherited from interface org.apache.tools.tar.TarConstants
CHKSUMLEN, DEVLEN, GIDLEN, GNAMELEN, GNU_LONGLINK, GNU_TMAGIC, LF_BLK, LF_CHR, LF_CONTIG, LF_DIR, LF_FIFO, LF_GNUTYPE_LONGNAME, LF_LINK, LF_NORMAL, LF_OLDNORM, LF_SYMLINK, MAGICLEN, MAXSIZE, MODELEN, MODTIMELEN, NAMELEN, SIZELEN, TMAGIC, UIDLEN, UNAMELEN
 
Constructor Summary
TarInputArchive(InputStream in)
          Extracts the entire TAR input stream into a temporary directory in order to allow subsequent random access to its entries.
 
Method Summary
 void close()
          Closes this input archive and releases any system resources associated with it.
private  void close0()
           
private static org.apache.tools.tar.TarInputStream createValidatedTarInputStream(InputStream in)
          Returns a newly created and validated TarInputStream.
 Enumeration getArchiveEntries()
          Returns an enumeration of the ArchiveEntry instances in this archive.
 ArchiveEntry getArchiveEntry(String entryName)
          Returns the ArchiveEntry for the given entry name or null if no entry with this name exists.
 InputStream getInputStream(ArchiveEntry entry, ArchiveEntry dstEntry)
          Returns a new InputStream for reading the contents of the given archive entry.
 InputArchiveMetaData getMetaData()
          Returns the meta data for this input archive.
private static String getName(org.apache.tools.tar.TarEntry entry)
          Returns the fixed name of the given TAR entry, ensuring that it ends with a EntryName#SEPARATOR if it's a directory.
 int getNumArchiveEntries()
          Returns the number of ArchiveEntry instances in this archive.
(package private) static InputStream readAhead(InputStream in, byte[] buf)
          Fills buf with data from the given input stream and returns an input stream from which you can still read all data, including the data in buf.
private static void readFully(InputStream in, byte[] buf)
           
 void setMetaData(InputArchiveMetaData metaData)
          Sets the meta data for this input archive.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NULL_RECORD

private static final byte[] NULL_RECORD

CHECKSUM_OFFSET

private static final int CHECKSUM_OFFSET
See Also:
Constant Field Values

TEMP_FILE_PREFIX

private static final String TEMP_FILE_PREFIX
Prefix for temporary files created by the multiplexer.

See Also:
Constant Field Values

entries

private final Map entries
Maps entry names to tar entries [String -> TarEntry].


metaData

private InputArchiveMetaData metaData
Constructor Detail

TarInputArchive

public TarInputArchive(InputStream in)
                throws IOException
Extracts the entire TAR input stream into a temporary directory in order to allow subsequent random access to its entries.

Parameters:
in - The input stream from which this input archive file should be initialized. This stream is not used by any of the methods in this class after the constructor has terminated and is never closed! So it is safe and recommended to close it upon termination of this constructor.
Throws:
IOException
Method Detail

getName

private static String getName(org.apache.tools.tar.TarEntry entry)
Returns the fixed name of the given TAR entry, ensuring that it ends with a EntryName#SEPARATOR if it's a directory.

Parameters:
entry - the TAR entry.
Returns:
the fixed name of the given TAR entry.
See Also:
Issue TRUEZIP-62

createValidatedTarInputStream

private static org.apache.tools.tar.TarInputStream createValidatedTarInputStream(InputStream in)
                                                                          throws IOException
Returns a newly created and validated TarInputStream. This method performs a simple validation by computing the checksum for the first record only. This method is required because the TarInputStream unfortunately does not do any validation!

Throws:
IOException

readAhead

static InputStream readAhead(InputStream in,
                             byte[] buf)
                      throws IOException
Fills buf with data from the given input stream and returns an input stream from which you can still read all data, including the data in buf.

Parameters:
in - The stream to read from. May not be null.
buf - The buffer to fill entirely with data.
Returns:
A stream which holds all the data in did.
Throws:
IOException - If buf couldn't get filled entirely.

readFully

private static void readFully(InputStream in,
                              byte[] buf)
                       throws IOException
Throws:
IOException

getNumArchiveEntries

public int getNumArchiveEntries()
Description copied from interface: InputArchive
Returns the number of ArchiveEntry instances in this archive.

Specified by:
getNumArchiveEntries in interface InputArchive

getArchiveEntries

public Enumeration getArchiveEntries()
Description copied from interface: InputArchive
Returns an enumeration of the ArchiveEntry instances in this archive.

Specified by:
getArchiveEntries in interface InputArchive

getArchiveEntry

public ArchiveEntry getArchiveEntry(String entryName)
Description copied from interface: InputArchive
Returns the ArchiveEntry for the given entry name or null if no entry with this name exists.

Specified by:
getArchiveEntry in interface InputArchive
Parameters:
entryName - A valid archive entry name - never null.
See Also:
Requirements for Archive Entry Names

getInputStream

public InputStream getInputStream(ArchiveEntry entry,
                                  ArchiveEntry dstEntry)
                           throws IOException
Description copied from interface: InputArchive
Returns a new InputStream for reading the contents of the given archive entry.

The returned stream should preferrably be unbuffered, as buffering is usually done in higher layers (all copy routines in TrueZIP do this and most client applications do it, too). Buffering twice does not increase, but decrease performance.

Note that the stream is guaranteed to be closed before the InputArchive.close() method of this archive is called!

Specified by:
getInputStream in interface InputArchive
Parameters:
entry - A valid reference to an archive entry. The runtime class of this entry is the same as the runtime class of the entries returned by InputArchive.getArchiveEntries().
dstEntry - If not null, this identifies the entry to which TrueZIP is actually copying data to and should be used to implement the Direct Data Copying (DDC) feature. Note that there is no guarantee on the runtime type of this object; it may have been created by other drivers.

For example, the ZIP driver family uses this to determine if data should be provided in its deflated form if the destination entry is another ZIP file entry.

Returns:
A (preferrably unbuffered) InputStream to read the archive entry data from. null is not allowed!
Throws:
InputArchiveBusyException - If the archive is currently busy on input for another entry. This exception is guaranteed to be recoverable, meaning it should be possible to read the same entry again as soon as the archive is not busy on input anymore.
FileNotFoundException - If the archive entry does not exist or is not accessible for some reason.
IOException - On any other exceptional condition.

close

public void close()
           throws IOException
Description copied from interface: InputArchive
Closes this input archive and releases any system resources associated with it.

Specified by:
close in interface InputArchive
Throws:
IOException - On any I/O related issue.

close0

private void close0()
             throws IOException
Throws:
IOException

getMetaData

public InputArchiveMetaData getMetaData()
Description copied from interface: InputArchive
Returns the meta data for this input archive. The default value is null.

Specified by:
getMetaData in interface InputArchive

setMetaData

public void setMetaData(InputArchiveMetaData metaData)
Description copied from interface: InputArchive
Sets the meta data for this input archive.

Specified by:
setMetaData in interface InputArchive
Parameters:
metaData - The meta data - may not be null.

TrueZIP 6.8.3

Copyright © 2005-2011 Schlichtherle IT Services. All Rights Reserved.