Top Description Fields Constructors Methods
org.apache.tools.tar

public Class TarOutputStream

extends FilterOutputStream
Class Inheritance
Imports
java.io.FilterOutputStream, .IOException, .OutputStream, .StringWriter, java.nio.ByteBuffer, java.nio.charset.StandardCharsets, java.util.Arrays, .Date, .HashMap, .Map, org.apache.tools.zip.ZipEncoding, .ZipEncodingHelper

The TarOutputStream writes a UNIX tar archive as an OutputStream. Methods are provided to put entries, and then write their contents by writing to this stream using write().

Field Summary

Modifier and TypeField and Description
private boolean
private static final ZipEncoding
protected byte[]
protected int
public static final int
BIGNUMBER_ERROR

Fail if a big number (e.g. size > 8GiB) is required in the archive.

public static final int
BIGNUMBER_POSIX

POSIX/PAX extensions are used to store big numbers in the archive.

public static final int
BIGNUMBER_STAR

star/GNU tar/BSD tar extensions are used to store big number in the archive.

private int
protected TarBuffer
private boolean
protected long
protected String
protected long
protected boolean
private final ZipEncoding
private boolean
finished

indicates if this archive is finished

private boolean
haveUnclosedEntry

Indicates if putNextEntry has been called without closeEntry

public static final int
LONGFILE_ERROR

Fail if a long file name is required in the archive.

public static final int
LONGFILE_GNU

GNU tar extensions are used to store long file names in the archive.

public static final int
LONGFILE_POSIX

POSIX/PAX extensions are used to store long file names in the archive.

public static final int
LONGFILE_TRUNCATE

Long paths will be truncated in the archive.

protected int
protected byte[]
protected byte[]
Inherited from java.io.FilterOutputStream:
out

Constructor Summary

AccessConstructor and Description
public
TarOutputStream(OutputStream
the output stream to use
os
)

Constructor for TarInputStream.

public
TarOutputStream(OutputStream
the output stream to use
os
,
String
name of the encoding to use for file names
encoding
)

Constructor for TarInputStream.

public
TarOutputStream(OutputStream
the output stream to use
os
,
int
the block size to use
blockSize
)

Constructor for TarInputStream.

public
TarOutputStream(OutputStream
the output stream to use
os
,
int
the block size to use
blockSize
,
String
name of the encoding to use for file names
encoding
)

Constructor for TarInputStream.

public
TarOutputStream(OutputStream
the output stream to use
os
,
int
the block size to use
blockSize
,
int
the record size to use
recordSize
)

Constructor for TarInputStream.

public
TarOutputStream(OutputStream
the output stream to use
os
,
int
the block size to use
blockSize
,
int
the record size to use
recordSize
,
String
name of the encoding to use for file names
encoding
)

Constructor for TarInputStream.

Method Summary

Modifier and TypeMethod and Description
private void
addPaxHeaderForBigNumber(Map<String, String> paxHeaders, String header, long value, long maxValue)

private void
public void
close()

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Ends the TAR archive and closes the underlying OutputStream.
public void
closeEntry()

Close an entry.

private void
failForBigNumber(String field, long value, long maxValue)

private void
failForBigNumber(String field, long value, long maxValue, String additionalMsg)

private void
private void
failForBigNumberWithPosixMessage(String field, long value, long maxValue)

public void
finish()

Ends the TAR archive without closing the underlying OutputStream.

public int

Returns:

The TarBuffer record size.
getRecordSize
()

Get the record size being used by this stream's TarBuffer.

private boolean

Returns:

whether a pax header has been written.
handleLongName
(TarEntry
entry the name belongs to
entry
,
String
the name to write
name
,
Map<String, String>
current map of pax headers
paxHeaders
,
String
name of the pax header to write
paxHeaderName
,
byte
type of the GNU entry to write
linkType
,
String
the name of the field
fieldName
)

Handles long file or link names according to the longFileMode setting.

public void
putNextEntry(TarEntry
The TarEntry to be written to the archive.
entry
)

Put an entry on the output stream.

public void
setAddPaxHeadersForNonAsciiNames(boolean
boolean
b
)

Whether to add a PAX extension header for non-ASCII file names.

public void
setBigNumberMode(int
the mode to use
bigNumberMode
)

Set the big number mode.

public void
setBufferDebug(boolean
True to turn on debugging.
debug
)

Sets the debugging flag in this stream's TarBuffer.

public void
setDebug(boolean
True to turn on debugging.
debugF
)

Sets the debugging flag.

public void
setLongFileMode(int
the mode to use
longFileMode
)

Set the long file mode.

private String
private void
public void
write(int
The byte written.
b
)

Overrides java.io.FilterOutputStream.write.

Writes a byte to the current tar archive entry.
public void
write(byte[]
The buffer to write to the archive.
wBuf
)

Overrides java.io.FilterOutputStream.write.

Writes bytes to the current tar archive entry.
public void
write(byte[]
The buffer to write to the archive.
wBuf
,
int
The offset in the buffer from which to get bytes.
wOffset
,
int
The number of bytes to write.
numToWrite
)

Overrides java.io.FilterOutputStream.write.

Writes bytes to the current tar archive entry.
private void
writeEOFRecord()

Write an EOF (end of archive) record to the tar archive.

pack-priv void
writePaxHeaders(TarEntry entry, String entryName, Map<String, String> headers)

Writes a PAX extended header with the given map as contents.

Inherited from java.io.FilterOutputStream:
flush

Field Detail

addPaxHeadersForNonAsciiNamesback to summary
private boolean addPaxHeadersForNonAsciiNames
ASCIIback to summary
private static final ZipEncoding ASCII
assemBufback to summary
protected byte[] assemBuf
assemLenback to summary
protected int assemLen
BIGNUMBER_ERRORback to summary
public static final int BIGNUMBER_ERROR

Fail if a big number (e.g. size > 8GiB) is required in the archive.

BIGNUMBER_POSIXback to summary
public static final int BIGNUMBER_POSIX

POSIX/PAX extensions are used to store big numbers in the archive.

BIGNUMBER_STARback to summary
public static final int BIGNUMBER_STAR

star/GNU tar/BSD tar extensions are used to store big number in the archive.

bigNumberModeback to summary
private int bigNumberMode
bufferback to summary
protected TarBuffer buffer
closedback to summary
private boolean closed

Hides java.io.FilterOutputStream.closed.

currBytesback to summary
protected long currBytes
currNameback to summary
protected String currName
currSizeback to summary
protected long currSize
debugback to summary
protected boolean debug
encodingback to summary
private final ZipEncoding encoding
finishedback to summary
private boolean finished

indicates if this archive is finished

haveUnclosedEntryback to summary
private boolean haveUnclosedEntry

Indicates if putNextEntry has been called without closeEntry

LONGFILE_ERRORback to summary
public static final int LONGFILE_ERROR

Fail if a long file name is required in the archive.

LONGFILE_GNUback to summary
public static final int LONGFILE_GNU

GNU tar extensions are used to store long file names in the archive.

LONGFILE_POSIXback to summary
public static final int LONGFILE_POSIX

POSIX/PAX extensions are used to store long file names in the archive.

LONGFILE_TRUNCATEback to summary
public static final int LONGFILE_TRUNCATE

Long paths will be truncated in the archive.

longFileModeback to summary
protected int longFileMode
oneBufback to summary
protected byte[] oneBuf
recordBufback to summary
protected byte[] recordBuf

Constructor Detail

TarOutputStreamback to summary
public TarOutputStream(OutputStream os)

Constructor for TarInputStream.

Parameters
os:OutputStream

the output stream to use

TarOutputStreamback to summary
public TarOutputStream(OutputStream os, String encoding)

Constructor for TarInputStream.

Parameters
os:OutputStream

the output stream to use

encoding:String

name of the encoding to use for file names

TarOutputStreamback to summary
public TarOutputStream(OutputStream os, int blockSize)

Constructor for TarInputStream.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use

TarOutputStreamback to summary
public TarOutputStream(OutputStream os, int blockSize, String encoding)

Constructor for TarInputStream.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use

encoding:String

name of the encoding to use for file names

TarOutputStreamback to summary
public TarOutputStream(OutputStream os, int blockSize, int recordSize)

Constructor for TarInputStream.

Parameters
os:OutputStream

the output stream to use

blockSize:int

the block size to use

recordSize:int

the record size to use

TarOutputStreamback to summary
public TarOutputStream(OutputStream os, int blockSize, int recordSize, String encoding)

Constructor for TarInputStream.

Parameters
os:OutputStream

the output 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

Method Detail

addPaxHeaderForBigNumberback to summary
private void addPaxHeaderForBigNumber(Map<String, String> paxHeaders, String header, long value, long maxValue)
addPaxHeadersForBigNumbersback to summary
private void addPaxHeadersForBigNumbers(Map<String, String> paxHeaders, TarEntry entry)
closeback to summary
public void close() throws IOException

Overrides java.io.FilterOutputStream.close.

Implements java.io.Closeable.close.

Ends the TAR archive and closes the underlying OutputStream. This means that finish() is called followed by calling the TarBuffer's close().

Annotations
@Override
Exceptions
IOException:
on error
closeEntryback to summary
public void closeEntry() throws IOException

Close an entry. This method MUST be called for all file entries that contain data. The reason is that we must buffer data written to the stream in order to satisfy the buffer's record based writes. Thus, there may be data fragments still being assembled that must be written to the output stream before this entry is closed and the next entry written.

Exceptions
IOException:
on error
failForBigNumberback to summary
private void failForBigNumber(String field, long value, long maxValue)
failForBigNumberback to summary
private void failForBigNumber(String field, long value, long maxValue, String additionalMsg)
failForBigNumbersback to summary
private void failForBigNumbers(TarEntry entry)
failForBigNumberWithPosixMessageback to summary
private void failForBigNumberWithPosixMessage(String field, long value, long maxValue)
finishback to summary
public void finish() throws IOException

Ends the TAR archive without closing the underlying OutputStream. An archive consists of a series of file entries terminated by an end-of-archive entry, which consists of two 512 blocks of zero bytes. POSIX.1 requires two EOF records, like some other implementations.

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

Get the record size being used by this stream's TarBuffer.

Returns:int

The TarBuffer record size.

handleLongNameback to summary
private boolean handleLongName(TarEntry entry, String name, Map<String, String> paxHeaders, String paxHeaderName, byte linkType, String fieldName) throws IOException

Handles long file or link names according to the longFileMode setting.

I.e. if the given name is too long to be written to a plain tar header then

  • it creates a pax header who's name is given by the paxHeaderName parameter if longFileMode is POSIX
  • it creates a GNU longlink entry who's type is given by the linkType parameter if longFileMode is GNU
  • it throws an exception if longFileMode is ERROR
  • it truncates the name if longFileMode is TRUNCATE

Parameters
entry:TarEntry

entry the name belongs to

name:String

the name to write

paxHeaders:Map<String, String>

current map of pax headers

paxHeaderName:String

name of the pax header to write

linkType:byte

type of the GNU entry to write

fieldName:String

the name of the field

Returns:boolean

whether a pax header has been written.

putNextEntryback to summary
public void putNextEntry(TarEntry entry) throws IOException

Put an entry on the output stream. This writes the entry's header record and positions the output stream for writing the contents of the entry. Once this method is called, the stream is ready for calls to write() to write the entry's contents. Once the contents are written, closeEntry() MUST be called to ensure that all buffered data is completely written to the output stream.

Parameters
entry:TarEntry

The TarEntry to be written to the archive.

Exceptions
IOException:
on error
setAddPaxHeadersForNonAsciiNamesback to summary
public void setAddPaxHeadersForNonAsciiNames(boolean b)

Whether to add a PAX extension header for non-ASCII file names.

Parameters
b:boolean

boolean

setBigNumberModeback to summary
public void setBigNumberMode(int bigNumberMode)

Set the big number mode. This can be BIGNUMBER_ERROR(0), BIGNUMBER_POSIX(1) or BIGNUMBER_STAR(2). This specifies the treatment of big files (sizes > TarConstants.MAXSIZE) and other numeric values to big to fit into a traditional tar header. Default is BIGNUMBER_ERROR.

Parameters
bigNumberMode:int

the mode to use

setBufferDebugback to summary
public void setBufferDebug(boolean debug)

Sets the debugging flag in this stream's TarBuffer.

Parameters
debug:boolean

True to turn on debugging.

setDebugback to summary
public void setDebug(boolean debugF)

Sets the debugging flag.

Parameters
debugF:boolean

True to turn on debugging.

setLongFileModeback to summary
public void setLongFileMode(int longFileMode)

Set the long file mode. This can be LONGFILE_ERROR(0), LONGFILE_TRUNCATE(1) or LONGFILE_GNU(2). This specifies the treatment of long file names (names >= TarConstants.NAMELEN). Default is LONGFILE_ERROR.

Parameters
longFileMode:int

the mode to use

stripTo7Bitsback to summary
private String stripTo7Bits(String name)
transferModTimeback to summary
private void transferModTime(TarEntry from, TarEntry to)
writeback to summary
public void write(int b) throws IOException

Overrides java.io.FilterOutputStream.write.

Writes a byte to the current tar archive entry. This method simply calls read(byte[], int, int).

Parameters
b:int

The byte written.

Annotations
@Override
Exceptions
IOException:
on error
writeback to summary
public void write(byte[] wBuf) throws IOException

Overrides java.io.FilterOutputStream.write.

Writes bytes to the current tar archive entry. This method simply calls write(byte[], int, int).

Parameters
wBuf:byte[]

The buffer to write to the archive.

Annotations
@Override
Exceptions
IOException:
on error
writeback to summary
public void write(byte[] wBuf, int wOffset, int numToWrite) throws IOException

Overrides java.io.FilterOutputStream.write.

Writes bytes to the current tar archive entry. This method is aware of the current entry and will throw an exception if you attempt to write bytes past the length specified for the current entry. The method is also (painfully) aware of the record buffering required by TarBuffer, and manages buffers that are not a multiple of recordsize in length, including assembling records from small buffers.

Parameters
wBuf:byte[]

The buffer to write to the archive.

wOffset:int

The offset in the buffer from which to get bytes.

numToWrite:int

The number of bytes to write.

Annotations
@Override
Exceptions
IOException:
on error
writeEOFRecordback to summary
private void writeEOFRecord() throws IOException

Write an EOF (end of archive) record to the tar archive. An EOF record consists of a record of all zeros.

writePaxHeadersback to summary
pack-priv void writePaxHeaders(TarEntry entry, String entryName, Map<String, String> headers) throws IOException

Writes a PAX extended header with the given map as contents.