Top Description Inners Fields Constructors Methods
org.hsqldb.lib.tar

public Class TarFileOutputStream

extends Object
Class Inheritance
Imports
java.io.File, .FileOutputStream, .IOException, .OutputStream, java.util.zip.GZIPOutputStream

Note that this class is not a java.io.FileOutputStream, because our goal is to greatly restrict the public methods of FileOutputStream, yet we must use public methods of the underlying FileOutputStream internally. Can't accomplish these goals in Java if we subclass.

This class is ignorant about Tar header fields, attributes and such. It is concerned with reading and writing blocks of data in conformance with Tar formatting, in a way convenient to those who want to write the header and data blocks.

Users write file data by means of populating the provided, public byte array, then calling the single write(int) method to write a portion of that array. This design purposefully goes with efficiency, simplicity, and performance over Java convention, which would not use public fields.

At this time, we do not support appending. That would greatly decrease the generality and simplicity of the our design, since appending is trivial without compression and very difficult with compression.

Users must finish tar file creation by using the finish() method. Just like a normal OutputStream, if processing is aborted for any reason, the close() method must be used to free up system resources.

Security Note

Due to pitiful lack of support for file security in Java before version 1.6, this class will only explicitly set permissions if it is compiled for Java 1.6. If it was not, and if your tar entries contain private data in files with 0400 or similar, be aware that that file can be pretty much be extracted by anybody with access to the tar file.

See Also
finish, close

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static interface

Field Summary

Modifier and TypeField and Description
protected int
protected long
public static final boolean
private final File
public byte[]
private final File
private OutputStream
pack-priv static final byte[]

Constructor Summary

AccessConstructor and Description
public
TarFileOutputStream(File
File
targetFile
)

Convenience wrapper to use default blocksPerRecord and compressionType.

public
TarFileOutputStream(File
File
targetFile
,
int
int
compressionType
)

Convenience wrapper to use default blocksPerRecord.

public
TarFileOutputStream(File
File
targetFile
,
int
int
compressionType
,
int
int
blocksPerRecord
)

This class does no validation or enforcement of file naming conventions.

Method Summary

Modifier and TypeMethod and Description
public void
public int
public void
close()

Implements java.io.Closeable.

public void
finish()

(Only) when this method returns successfully, the generated file will be a valid tar file.

public void
flush()

Implements java.io.Flushable.

public long
public void
padCurrentBlock()

Rounds out the current block to the next block boundary.

public void
write(byte[]
byte[]
byteArray
,
int
int
byteCount
)

This class and subclasses should write to the underlying writeStream ONLY WITH THIS METHOD.

public void
write(int
int
byteCount
)

The normal way to write file data (as opposed to header data or padding) using this class.

public void
writeBlock(byte[]
byte[]
block
)

Write a user-specified 512-byte block.

public void
writePadBlock()

Writes a single zero'd block.

public void
writePadBlocks(int
int
blockCount
)

Writes the specified quantity of zero'd blocks.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

blocksPerRecordback to summary
protected int blocksPerRecord
bytesWrittenback to summary
protected long bytesWritten
debugback to summary
public static final boolean debug
targetFileback to summary
private final File targetFile
writeBufferback to summary
public byte[] writeBuffer
writeFileback to summary
private final File writeFile
writeStreamback to summary
private OutputStream writeStream
ZERO_BLOCKback to summary
pack-priv static final byte[] ZERO_BLOCK

Constructor Detail

TarFileOutputStreamback to summary
public TarFileOutputStream(File targetFile) throws IOException

Convenience wrapper to use default blocksPerRecord and compressionType.

Parameters
targetFile:File

File

Exceptions
IOException:
on failure
See Also
TarFileOutputStream(File, int, int)
TarFileOutputStreamback to summary
public TarFileOutputStream(File targetFile, int compressionType) throws IOException

Convenience wrapper to use default blocksPerRecord.

Parameters
targetFile:File

File

compressionType:int

int

Exceptions
IOException:
on failure
See Also
TarFileOutputStream(File, int, int)
TarFileOutputStreamback to summary
public TarFileOutputStream(File targetFile, int compressionType, int blocksPerRecord) throws IOException

This class does no validation or enforcement of file naming conventions. If desired, the caller should enforce extensions like "tar" and "tar.gz" (and that they match the specified compression type). It also overwrites files without warning (just like FileOutputStream).

Parameters
targetFile:File

File

compressionType:int

int

blocksPerRecord:int

int

Exceptions
IOException:
on failure

Method Detail

assertAtBlockBoundaryback to summary
public void assertAtBlockBoundary()
Exceptions
IllegalStateException:
if end of file not on a block boundary.
bytesLeftInBlockback to summary
public int bytesLeftInBlock()
closeback to summary
public void close() throws IOException

Implements java.io.Closeable.

Important Note

This method deletes the work file after closing it!

Exceptions
IOException:
on failure
See Also
java.io.Closeable
finishback to summary
public void finish() throws IOException

(Only) when this method returns successfully, the generated file will be a valid tar file. This method always performs a close, so you never need to call the close if your code makes it to this method. (You do need to call close if processing is aborted before calling finish()).

Exceptions
IOException:
on failure
See Also
close
flushback to summary
public void flush() throws IOException

Implements java.io.Flushable.

Exceptions
IOException:
on failure
See Also
java.io.Flushable
getBytesWrittenback to summary
public long getBytesWritten()
padCurrentBlockback to summary
public void padCurrentBlock() throws IOException

Rounds out the current block to the next block boundary. If we are currently at a block boundary, nothing is done.

Exceptions
IOException:
on failure
writeback to summary
public void write(byte[] byteArray, int byteCount) throws IOException

This class and subclasses should write to the underlying writeStream ONLY WITH THIS METHOD. That way we can be confident that bytesWritten will always be accurate.

Parameters
byteArray:byte[]

byte[]

byteCount:int

int

Exceptions
IOException:
on failure
writeback to summary
public void write(int byteCount) throws IOException

The normal way to write file data (as opposed to header data or padding) using this class.

Parameters
byteCount:int

int

Exceptions
IOException:
on failure
writeBlockback to summary
public void writeBlock(byte[] block) throws IOException

Write a user-specified 512-byte block. For efficiency, write(int) should be used when writing file body content.

Parameters
block:byte[]

byte[]

Exceptions
IOException:
on failure
See Also
write(int)
writePadBlockback to summary
public void writePadBlock() throws IOException

Writes a single zero'd block.

Exceptions
IOException:
on failure
writePadBlocksback to summary
public void writePadBlocks(int blockCount) throws IOException

Writes the specified quantity of zero'd blocks.

Parameters
blockCount:int

int

Exceptions
IOException:
on failure
org.hsqldb.lib.tar back to summary

public Interface TarFileOutputStream.Compression


Field Summary

Modifier and TypeField and Description
public static final int
public static final int
public static final int
public static final int

Field Detail

DEFAULT_BLOCKS_PER_RECORDback to summary
public static final int DEFAULT_BLOCKS_PER_RECORD
DEFAULT_COMPRESSIONback to summary
public static final int DEFAULT_COMPRESSION
GZIP_COMPRESSIONback to summary
public static final int GZIP_COMPRESSION
NO_COMPRESSIONback to summary
public static final int NO_COMPRESSION