org.at4j.tar.builder
Class V7TarEntryStrategy

java.lang.Object
  extended by org.at4j.tar.builder.V7TarEntryStrategy
All Implemented Interfaces:
TarEntryStrategy
Direct Known Subclasses:
UstarEntryStrategy

public class V7TarEntryStrategy
extends Object
implements TarEntryStrategy

This TarEntryStrategy creates Tar entries conforming to the old Unix v7 format. The entries created by this strategy contain the following data:

Property Comment
Absolute location of the entry Limited to 99 characters, excluding the initial slash.
UnixEntityMode A Unix file permission mode such as 0644 or 0755.
Owner UID The user id of the owner of the file system entity that the Tar entry was created from. This may be a value between 0 and 2097151 (inclusive).
Owner GID The group id of the group that owns the file system entity that the tar entry was created from. This may be a value between 0 and 2097151 (inclusive).
File size For files, this is a value between 0 and 8589934591 bytes (~ 8.6 Gb), inclusive.
Link name For symbolic links, this is the link target. Limited to 99 characters.

Since:
1.0
Author:
Karl Gustafsson

Constructor Summary
V7TarEntryStrategy()
          Create a new strategy object that will use the platform's default character encoding to encode text values in the Tar entries.
V7TarEntryStrategy(Charset cs)
          Create a new strategy object that will use the supplied charset for encoding test values in the Tar entries.
 
Method Summary
protected  byte[] addChecksumToHeader(byte[] header)
          Add the checksum field to the Tar entry header.
protected  String getFileName(AbsoluteLocation location, boolean directory)
          Get the file name for the supplied location, discarding the path up to the file name.
protected  byte[] getHeader(Object entity, String fileName, TarEntrySettings effectiveSettings, long fileSize, EntityType entityType, Date lastModified)
          Create a Tar header for the new entry.
protected  Charset getTextEncodingCharset()
          Get the charset that is used to encode text values in Tar entries.
protected  void goToNextBlockBoundary(DataSink out, long sizeOfDataWritten)
          Set the current position of the DataSink to be on the next tar block boundary after a writing operation.
protected  void setFileNameInHeader(byte[] header, String fileName)
          Set the file name field in the Tar entry header.
 void writeDirectory(DataSink out, DirectoryAdapter<?> da, AbsoluteLocation location, TarEntrySettings effectiveSettings, Date lastModified)
          Write a directory entry to the Tar file.
 void writeFile(DataSink out, ReadableFile f, AbsoluteLocation location, TarEntrySettings effectiveSettings, Date lastModified)
          Write a file entry to the Tar file.
 void writeFileFromStream(RandomAccess out, InputStream is, AbsoluteLocation location, TarEntrySettings effectiveSettings, Date lastModified)
          Write a file entry containing all data that can be read from the stream to the Tar file.
protected  long writeFileToArchive(DataSink out, ReadableFile f, TarEntrySettings effectiveSettings)
          Write the contents of the supplied file to the archive.
protected  byte[] zeroPadLeft(String s, int size)
          Add zeroes to the left of the supplied string so that it will have the requested size, and then convert the string to bytes using the platform's default charset.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

V7TarEntryStrategy

public V7TarEntryStrategy()
Create a new strategy object that will use the platform's default character encoding to encode text values in the Tar entries.

See Also:
V7TarEntryStrategy(Charset)

V7TarEntryStrategy

public V7TarEntryStrategy(Charset cs)
Create a new strategy object that will use the supplied charset for encoding test values in the Tar entries.

Parameters:
cs - The charset.
See Also:
V7TarEntryStrategy()
Method Detail

getTextEncodingCharset

protected final Charset getTextEncodingCharset()
Get the charset that is used to encode text values in Tar entries.

Returns:
The charset that is used to encode text values in Tar entries.

zeroPadLeft

protected byte[] zeroPadLeft(String s,
                             int size)
Add zeroes to the left of the supplied string so that it will have the requested size, and then convert the string to bytes using the platform's default charset. This should only be used for text containing characters that will occupy only one byte each.

Parameters:
s - The string to zero pad.
size - The requested size of the returned array.
Returns:
An array of the requested size containing the supplied text padded with zeroes to the left.

goToNextBlockBoundary

protected void goToNextBlockBoundary(DataSink out,
                                     long sizeOfDataWritten)
                              throws WrappedIOException
Set the current position of the DataSink to be on the next tar block boundary after a writing operation. Tar blocks are 512 bytes big.

Parameters:
out - The RandomAccess.
sizeOfDataWritten - The size of the data that was written. It is assumed that the write started on a block boundary.
Throws:
WrappedIOException - On I/O errors.

getFileName

protected String getFileName(AbsoluteLocation location,
                             boolean directory)
Get the file name for the supplied location, discarding the path up to the file name. If it is a directory, add a trailing slash.

Parameters:
location - The absolute location.
directory - Should the file name be for a directory?
Returns:
The file name.

setFileNameInHeader

protected void setFileNameInHeader(byte[] header,
                                   String fileName)
                            throws ArchiveEntryAddException
Set the file name field in the Tar entry header. This implementation throws an ArchiveEntryAddException if the file name is longer than 99 bytes. Subclasses may override this method to implement a different behavior.

Parameters:
header - The header to add the file name to.
fileName - The file name to add.
Throws:
ArchiveEntryAddException - If the file name is longer than 99 bytes.

getHeader

protected byte[] getHeader(Object entity,
                           String fileName,
                           TarEntrySettings effectiveSettings,
                           long fileSize,
                           EntityType entityType,
                           Date lastModified)
                    throws ArchiveEntryAddException
Create a Tar header for the new entry. The header is a 512 bytes long byte array that contains metadata about the file system entity (file, directory, ...) that the Tar entry was created from.

This implementation creates a Unix v7 header (of course). Subclasses may override this method to create other kinds of headers.

This method uses the setFileNameInHeader(byte[], String) method to set the file name.

Parameters:
entity - The entity that the Tar entry should represent. This may be a file, a directory or an InputStream.
fileName - The name of the entity.
effectiveSettings - Effective settings for the entry.
fileSize - If the entry is a file entry, this value contains the file size in bytes.
entityType - The type of the entity being added.
lastModified - When the entity was last modified.
Returns:
A 512 byte long array, without the checksum field filled in. The addChecksumToHeader(byte[]) method may be used to calculate the header's checksum when all data has been added to it.
Throws:
ArchiveEntryAddException - If the entry cannot be created, for some reason.

writeFileToArchive

protected long writeFileToArchive(DataSink out,
                                  ReadableFile f,
                                  TarEntrySettings effectiveSettings)
                           throws WrappedIOException
Write the contents of the supplied file to the archive. This is used to write the file data for file entries.

Parameters:
out - The RandomAccess to write to. When this method is called, it should be positioned at the start of the first block of the file data (just after the file entry header). When this method returns, the RandomAccess is positioned just after the written data. (I.e: probably not at the next Tar block boundary). If this method throws an exception, the position of the RandomAccess is unspecified.
Returns:
The size of the written file.
Throws:
WrappedIOException - On I/O errors.

addChecksumToHeader

protected byte[] addChecksumToHeader(byte[] header)
Add the checksum field to the Tar entry header. The checksum is the sum of all individual bytes in the header, with the checksum field itself taken to be all spaces.

This method should be called after all data has been added to the tar entry header.

Parameters:
header - The Tar entry header.
Returns:
The Tar entry header. (The same byte array as header.)

writeFile

public void writeFile(DataSink out,
                      ReadableFile f,
                      AbsoluteLocation location,
                      TarEntrySettings effectiveSettings,
                      Date lastModified)
Description copied from interface: TarEntryStrategy
Write a file entry to the Tar file.

Specified by:
writeFile in interface TarEntryStrategy
Parameters:
out - The DataSink to write to. This may be a RandomAccess object. When this method is called, the DataSink is positioned at the start of the entry to write. If this method returns successfully, the DataSink should be positioned at the next block boundary after the written entry (the Tar file is divided into 512-byte blocks).
f - The file to add to the archive. If this file is in a locking FileSystem, it is locked for reading by the caller before this method is called.
location - The absolute location of the entry in the Tar archive.
effectiveSettings - The effective settings for the entry.
lastModified - The time when the file entity was last modified.

writeFileFromStream

public void writeFileFromStream(RandomAccess out,
                                InputStream is,
                                AbsoluteLocation location,
                                TarEntrySettings effectiveSettings,
                                Date lastModified)
Description copied from interface: TarEntryStrategy
Write a file entry containing all data that can be read from the stream to the Tar file.

Specified by:
writeFileFromStream in interface TarEntryStrategy
Parameters:
out - The RandomAccess to write to. When this method is called, the RandomAccess is positioned at the start of the entry to write. If this method returns successfully, the RandomAccess should be positioned at the next block boundary after the written entry (the Tar file is divided into 512-byte blocks).
is - The stream containing the file's data.
location - The absolute location of the entry in the Tar archive.
effectiveSettings - The effective settings for the entry.
lastModified - The time when the file entity was last modified.

writeDirectory

public void writeDirectory(DataSink out,
                           DirectoryAdapter<?> da,
                           AbsoluteLocation location,
                           TarEntrySettings effectiveSettings,
                           Date lastModified)
Description copied from interface: TarEntryStrategy
Write a directory entry to the Tar file.

Specified by:
writeDirectory in interface TarEntryStrategy
Parameters:
out - The DataSink to write to. This may be a RandomAccess object. When this method is called, the DataSink is positioned at the start of the entry to write. If this method returns successfully, the DataSink should be positioned at the next block boundary after the written entry (the Tar file is divided into 512-byte blocks).
da - The directory to add to the Tar file.
location - The absolute location of the entry in the Tar archive.
effectiveSettings - The effective settings for the entry.
lastModified - The time when the directory entity was last modified.