org.apache.tools.tar

## Class TarUtils

- java.lang.Object
- - org.apache.tools.tar.TarUtils

    public class TarUtils
    extends java.lang.Object

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

### Method Summary

All Methods Static Methods Concrete Methods
`static long` `computeCheckSum(byte[] buf)` Compute the checksum of a tar entry header.
`static int` `formatCheckSumOctalBytes(long value, byte[] buf, int offset, int length)` Writes an octal value into a buffer.
`static int` `formatLongOctalBytes(long value, byte[] buf, int offset, int length)` Write an octal long integer into a buffer.
`static int` `formatLongOctalOrBinaryBytes(long value, byte[] buf, int offset, int length)` Write an long integer into a buffer as an octal string if this will fit, or as a binary number otherwise.
`static int` `formatNameBytes(java.lang.String name, byte[] buf, int offset, int length)` Copy a name into a buffer.
`static int` `formatNameBytes(java.lang.String name, byte[] buf, int offset, int length, ZipEncoding encoding)` Copy a name into a buffer.
`static int` `formatOctalBytes(long value, byte[] buf, int offset, int length)` Write an octal integer into a buffer.
`static void` `formatUnsignedOctalString(long value, byte[] buffer, int offset, int length)` Fill buffer with unsigned octal number, padded with leading zeroes.
`static boolean` `parseBoolean(byte[] buffer, int offset)` Parse a boolean byte from a buffer.
`static java.lang.String` `parseName(byte[] buffer, int offset, int length)` Parse an entry name from a buffer.
`static java.lang.String` `parseName(byte[] buffer, int offset, int length, ZipEncoding encoding)` Parse an entry name from a buffer.
`static long` `parseOctal(byte[] buffer, int offset, int length)` Parse an octal string from a buffer.
`static long` `parseOctalOrBinary(byte[] buffer, int offset, int length)` Compute the value contained in a byte buffer.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Method Detail

#### parseOctal

    public static long parseOctal(byte[] buffer,
                                  int offset,
                                  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` - The buffer from which to parse.
  `offset` - The offset into the buffer from which to parse.
  `length` - The maximum number of bytes to parse - must be at least 2 bytes.
Returns:
  The long value of the octal string.
Throws:
  `java.lang.IllegalArgumentException` - if the trailing space/NUL is missing or if a invalid byte is detected.

#### parseOctalOrBinary

    public static long parseOctalOrBinary(byte[] buffer,
                                          int offset,
                                          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` - The buffer from which to parse.
  `offset` - The offset into the buffer from which to parse.
  `length` - The maximum number of bytes to parse.
Returns:
  The long value of the octal or binary string.
Throws:
  `java.lang.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.

#### parseBoolean

    public static boolean parseBoolean(byte[] buffer,
                                       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` - The buffer from which to parse.
  `offset` - The offset into the buffer from which to parse.
Returns:
  The boolean value of the bytes.
Throws:
  `java.lang.IllegalArgumentException` - if an invalid byte is detected.

#### parseName

    public static java.lang.String parseName(byte[] buffer,
                                             int offset,
                                             int length)

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

Parameters:
  `buffer` - The buffer from which to parse.
  `offset` - The offset into the buffer from which to parse.
  `length` - The maximum number of bytes to parse.
Returns:
  The entry name.

#### parseName

    public static java.lang.String parseName(byte[] buffer,
                                             int offset,
                                             int length,
                                             ZipEncoding encoding)
                                      throws java.io.IOException

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

Parameters:
  `buffer` - The buffer from which to parse.
  `offset` - The offset into the buffer from which to parse.
  `length` - The maximum number of bytes to parse.
  `encoding` - name of the encoding to use for file names
Returns:
  The entry name.
Throws:
  `java.io.IOException`

#### formatNameBytes

    public static int formatNameBytes(java.lang.String name,
                                      byte[] buf,
                                      int offset,
                                      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` - The header name from which to copy the characters.
  `buf` - The buffer where the name is to be stored.
  `offset` - The starting offset into the buffer
  `length` - The maximum number of header bytes to copy.
Returns:
  The updated offset, i.e. offset + length

#### formatNameBytes

    public static int formatNameBytes(java.lang.String name,
                                      byte[] buf,
                                      int offset,
                                      int length,
                                      ZipEncoding encoding)
                               throws java.io.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` - The header name from which to copy the characters.
  `buf` - The buffer where the name is to be stored.
  `offset` - The starting offset into the buffer
  `length` - The maximum number of header bytes to copy.
  `encoding` - name of the encoding to use for file names
Returns:
  The updated offset, i.e. offset + length
Throws:
  `java.io.IOException`

#### formatUnsignedOctalString

    public static void formatUnsignedOctalString(long value,
                                                 byte[] buffer,
                                                 int offset,
                                                 int length)

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

Parameters:
  `value` - number to convert to octal - treated as unsigned
  `buffer` - destination buffer
  `offset` - starting offset in buffer
  `length` - length of buffer to fill
Throws:
  `java.lang.IllegalArgumentException` - if the value will not fit in the buffer

#### formatOctalBytes

    public static int formatOctalBytes(long value,
                                       byte[] buf,
                                       int offset,
                                       int length)

Write an octal integer into a buffer. Uses `formatUnsignedOctalString(long, byte[], int, int)` to format the value as an octal string with leading zeros. The converted number is followed by space and NUL

Parameters:
  `value` - The value to write
  `buf` - The buffer to receive the output
  `offset` - The starting offset into the buffer
  `length` - The size of the output buffer
Returns:
  The updated offset, i.e offset+length
Throws:
  `java.lang.IllegalArgumentException` - if the value (and trailer) will not fit in the buffer

#### formatLongOctalBytes

    public static int formatLongOctalBytes(long value,
                                           byte[] buf,
                                           int offset,
                                           int length)

Write an octal long integer into a buffer. Uses `formatUnsignedOctalString(long, byte[], int, int)` to format the value as an octal string with leading zeros. The converted number is followed by a space.

Parameters:
  `value` - The value to write as octal
  `buf` - The destinationbuffer.
  `offset` - The starting offset into the buffer.
  `length` - The length of the buffer
Returns:
  The updated offset
Throws:
  `java.lang.IllegalArgumentException` - if the value (and trailer) will not fit in the buffer

#### formatLongOctalOrBinaryBytes

    public static int formatLongOctalOrBinaryBytes(long value,
                                                   byte[] buf,
                                                   int offset,
                                                   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(long, byte[], int, int)` to format the value as an octal string with leading zeros. The converted number is followed by a space.

Parameters:
  `value` - The value to write into the buffer.
  `buf` - The destination buffer.
  `offset` - The starting offset into the buffer.
  `length` - The length of the buffer.
Returns:
  The updated offset.
Throws:
  `java.lang.IllegalArgumentException` - if the value (and trailer) will not fit in the buffer.

#### formatCheckSumOctalBytes

    public static int formatCheckSumOctalBytes(long value,
                                               byte[] buf,
                                               int offset,
                                               int length)

Writes an octal value into a buffer. Uses `formatUnsignedOctalString(long, byte[], int, int)` to format the value as an octal string with leading zeros. The converted number is followed by NUL and then space.

Parameters:
  `value` - The value to convert
  `buf` - The destination buffer
  `offset` - The starting offset into the buffer.
  `length` - The size of the buffer.
Returns:
  The updated value of offset, i.e. offset+length
Throws:
  `java.lang.IllegalArgumentException` - if the value (and trailer) will not fit in the buffer

#### computeCheckSum

    public static long computeCheckSum(byte[] buf)

Compute the checksum of a tar entry header.

Parameters:
  `buf` - The tar entry's header buffer.
Returns:
  The computed checksum.