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

public Class TarUtils

extends Object
Class Inheritance
Imports
java.io.IOException, java.math.BigInteger, java.nio.ByteBuffer, java.util.Arrays, .WeakHashMap, org.apache.tools.zip.ZipEncoding, .ZipEncodingHelper

This class provides static utility methods to work with byte streams.

Field Summary

Modifier and TypeField and Description
private static final int
pack-priv static final ZipEncoding
pack-priv static final ZipEncoding
FALLBACK_ENCODING

Encapsulates the algorithms used up to Ant 1.8 as ZipEncoding.

private static final String
private static final WeakHashMap<ZipEncoding, byte[]>
private static final String
X

private static final String

Constructor Summary

AccessConstructor and Description
private
TarUtils()

Private constructor to prevent instantiation of this utility class.

Method Summary

Modifier and TypeMethod and Description
public static long

Returns:

The computed checksum.
computeCheckSum
(final byte[]
The tar entry's header buffer.
buf
)

Compute the checksum of a tar entry header.

private static String
exceptionMessage(final byte[] buffer, final int offset, final int length, final int current, final byte currentByte)

private static void
formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)

public static int

Returns:

The updated value of offset, i.e. offset+length
formatCheckSumOctalBytes
(final long
The value to convert
value
,
final byte[]
The destination buffer
buf
,
final int
The starting offset into the buffer.
offset
,
final int
The size of the buffer.
length
)

Writes an octal value into a buffer.

private static void
formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)

public static int

Returns:

The updated offset
formatLongOctalBytes
(final long
The value to write as octal
value
,
final byte[]
The destinationbuffer.
buf
,
final int
The starting offset into the buffer.
offset
,
final int
The length of the buffer
length
)

Write an octal long integer into a buffer.

public static int

Returns:

The updated offset.
formatLongOctalOrBinaryBytes
(final long
The value to write into the buffer.
value
,
final byte[]
The destination buffer.
buf
,
final int
The starting offset into the buffer.
offset
,
final int
The length of the buffer.
length
)

Write an long integer into a buffer as an octal string if this will fit, or as a binary number otherwise.

public static int

Returns:

The updated offset, i.e. offset + length
formatNameBytes
(final String
The header name from which to copy the characters.
name
,
final byte[]
The buffer where the name is to be stored.
buf
,
final int
The starting offset into the buffer
offset
,
final int
The maximum number of header bytes to copy.
length
)

Copy a name into a buffer.

public static int

Returns:

The updated offset, i.e. offset + length
formatNameBytes
(final String
The header name from which to copy the characters.
name
,
final byte[]
The buffer where the name is to be stored.
buf
,
final int
The starting offset into the buffer
offset
,
final int
The maximum number of header bytes to copy.
length
,
final ZipEncoding
name of the encoding to use for file names
encoding
)

Copy a name into a buffer.

public static int

Returns:

The updated offset, i.e offset+length
formatOctalBytes
(final long
The value to write
value
,
final byte[]
The buffer to receive the output
buf
,
final int
The starting offset into the buffer
offset
,
final int
The size of the output buffer
length
)

Write an octal integer into a buffer.

public static void
formatUnsignedOctalString(final long
number to convert to octal - treated as unsigned
value
,
final byte[]
destination buffer
buffer
,
final int
starting offset in buffer
offset
,
final int
length of buffer to fill
length
)

Fill buffer with unsigned octal number, padded with leading zeroes.

private static byte[]
private static byte[]
private static long
parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative)

private static long
parseBinaryLong(final byte[] buffer, final int offset, final int length, final boolean negative)

public static boolean

Returns:

The boolean value of the bytes.
parseBoolean
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
)

Parse a boolean byte from a buffer.

public static String

Returns:

The entry name.
parseName
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse.
length
)

Parse an entry name from a buffer.

public static String

Returns:

The entry name.
parseName
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse.
length
,
final ZipEncoding
name of the encoding to use for file names
encoding
)

Parse an entry name from a buffer.

public static long

Returns:

The long value of the octal string.
parseOctal
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse - must be at least 2 bytes.
length
)

Parse an octal string from a buffer.

public static long

Returns:

The long value of the octal or binary string.
parseOctalOrBinary
(final byte[]
The buffer from which to parse.
buffer
,
final int
The offset into the buffer from which to parse.
offset
,
final int
The maximum number of bytes to parse.
length
)

Compute the value contained in a byte buffer.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

BYTE_MASKback to summary
private static final int BYTE_MASK
DEFAULT_ENCODINGback to summary
pack-priv static final ZipEncoding DEFAULT_ENCODING
FALLBACK_ENCODINGback to summary
pack-priv static final ZipEncoding FALLBACK_ENCODING

Encapsulates the algorithms used up to Ant 1.8 as ZipEncoding.

NULback to summary
private static final String NUL
NUL_BY_ENCODINGback to summary
private static final WeakHashMap<ZipEncoding, byte[]> NUL_BY_ENCODING
Xback to summary
private static final String X
X_NULback to summary
private static final String X_NUL

Constructor Detail

TarUtilsback to summary
private TarUtils()

Private constructor to prevent instantiation of this utility class.

Method Detail

computeCheckSumback to summary
public static long computeCheckSum(final byte[] buf)

Compute the checksum of a tar entry header.

Parameters
buf:byte[]

The tar entry's header buffer.

Returns:long

The computed checksum.

exceptionMessageback to summary
private static String exceptionMessage(final byte[] buffer, final int offset, final int length, final int current, final byte currentByte)
formatBigIntegerBinaryback to summary
private static void formatBigIntegerBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)
formatCheckSumOctalBytesback to summary
public static int formatCheckSumOctalBytes(final long value, final byte[] buf, final int offset, final int length)

Writes an octal value into a buffer. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by NUL and then space.

Parameters
value:long

The value to convert

buf:byte[]

The destination buffer

offset:int

The starting offset into the buffer.

length:int

The size of the buffer.

Returns:int

The updated value of offset, i.e. offset+length

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer
formatLongBinaryback to summary
private static void formatLongBinary(final long value, final byte[] buf, final int offset, final int length, final boolean negative)
formatLongOctalBytesback to summary
public static int formatLongOctalBytes(final long value, final byte[] buf, final int offset, final int length)

Write an octal long integer into a buffer. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by a space.

Parameters
value:long

The value to write as octal

buf:byte[]

The destinationbuffer.

offset:int

The starting offset into the buffer.

length:int

The length of the buffer

Returns:int

The updated offset

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer
formatLongOctalOrBinaryBytesback to summary
public static int formatLongOctalOrBinaryBytes(final long value, final byte[] buf, final int offset, final int length)

Write an long integer into a buffer as an octal string if this will fit, or as a binary number otherwise. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by a space.

Parameters
value:long

The value to write into the buffer.

buf:byte[]

The destination buffer.

offset:int

The starting offset into the buffer.

length:int

The length of the buffer.

Returns:int

The updated offset.

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer.
formatNameBytesback to summary
public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length)

Copy a name into a buffer. Copies characters from the name into the buffer starting at the specified offset. If the buffer is longer than the name, the buffer is filled with trailing NULs. If the name is longer than the buffer, the output is truncated.

Parameters
name:String

The header name from which to copy the characters.

buf:byte[]

The buffer where the name is to be stored.

offset:int

The starting offset into the buffer

length:int

The maximum number of header bytes to copy.

Returns:int

The updated offset, i.e. offset + length

formatNameBytesback to summary
public static int formatNameBytes(final String name, final byte[] buf, final int offset, final int length, final ZipEncoding encoding) throws IOException

Copy a name into a buffer. Copies characters from the name into the buffer starting at the specified offset. If the buffer is longer than the name, the buffer is filled with trailing NULs. If the name is longer than the buffer, the output is truncated.

Parameters
name:String

The header name from which to copy the characters.

buf:byte[]

The buffer where the name is to be stored.

offset:int

The starting offset into the buffer

length:int

The maximum number of header bytes to copy.

encoding:ZipEncoding

name of the encoding to use for file names

Returns:int

The updated offset, i.e. offset + length

Exceptions
IOException:
if encode fails
formatOctalBytesback to summary
public static int formatOctalBytes(final long value, final byte[] buf, final int offset, final int length)

Write an octal integer into a buffer. Uses formatUnsignedOctalString to format the value as an octal string with leading zeros. The converted number is followed by space and NUL

Parameters
value:long

The value to write

buf:byte[]

The buffer to receive the output

offset:int

The starting offset into the buffer

length:int

The size of the output buffer

Returns:int

The updated offset, i.e offset+length

Exceptions
IllegalArgumentException:
if the value (and trailer) will not fit in the buffer
formatUnsignedOctalStringback to summary
public static void formatUnsignedOctalString(final long value, final byte[] buffer, final int offset, final int length)

Fill buffer with unsigned octal number, padded with leading zeroes.

Parameters
value:long

number to convert to octal - treated as unsigned

buffer:byte[]

destination buffer

offset:int

starting offset in buffer

length:int

length of buffer to fill

Exceptions
IllegalArgumentException:
if the value will not fit in the buffer
getNulByteEquivalentback to summary
private static byte[] getNulByteEquivalent(ZipEncoding encoding) throws IOException
getUncachedNulByteEquivalentback to summary
private static byte[] getUncachedNulByteEquivalent(ZipEncoding encoding) throws IOException
parseBinaryBigIntegerback to summary
private static long parseBinaryBigInteger(final byte[] buffer, final int offset, final int length, final boolean negative)
parseBinaryLongback to summary
private static long parseBinaryLong(final byte[] buffer, final int offset, final int length, final boolean negative)
parseBooleanback to summary
public static boolean parseBoolean(final byte[] buffer, final int offset)

Parse a boolean byte from a buffer. Leading spaces and NUL are ignored. The buffer may contain trailing spaces or NULs.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

Returns:boolean

The boolean value of the bytes.

Exceptions
IllegalArgumentException:
if an invalid byte is detected.
parseNameback to summary
public static String parseName(final byte[] buffer, final int offset, final int length)

Parse an entry name from a buffer. Parsing stops when a NUL is found or the buffer length is reached.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse.

Returns:String

The entry name.

parseNameback to summary
public static String parseName(final byte[] buffer, final int offset, final int length, final ZipEncoding encoding) throws IOException

Parse an entry name from a buffer. Parsing stops when a NUL is found or the buffer length is reached.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse.

encoding:ZipEncoding

name of the encoding to use for file names

Returns:String

The entry name.

Exceptions
IOException:
if decode fails
parseOctalback to summary
public static long parseOctal(final byte[] buffer, final int offset, final int length)

Parse an octal string from a buffer.

Leading spaces are ignored. The buffer must contain a trailing space or NUL, and may contain an additional trailing space or NUL.

The input buffer is allowed to contain all NULs, in which case the method returns 0L (this allows for missing fields).

To work-around some tar implementations that insert a leading NUL this method returns 0 if it detects a leading NUL since Ant 1.9.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse - must be at least 2 bytes.

Returns:long

The long value of the octal string.

Exceptions
IllegalArgumentException:
if the trailing space/NUL is missing or if a invalid byte is detected.
parseOctalOrBinaryback to summary
public static long parseOctalOrBinary(final byte[] buffer, final int offset, final int length)

Compute the value contained in a byte buffer. If the most significant bit of the first byte in the buffer is set, this bit is ignored and the rest of the buffer is interpreted as a binary number. Otherwise, the buffer is interpreted as an octal number as per the parseOctal function above.

Parameters
buffer:byte[]

The buffer from which to parse.

offset:int

The offset into the buffer from which to parse.

length:int

The maximum number of bytes to parse.

Returns:long

The long value of the octal or binary string.

Exceptions
IllegalArgumentException:
if the trailing space/NUL is missing or an invalid byte is detected in an octal number, or if a binary number would exceed the size of a signed long 64-bit integer.