FileDocCategorySizeDatePackage
TarUtils.javaAPI DocApache Ant 1.706175Wed Dec 13 06:16:18 GMT 2006org.apache.tools.tar

TarUtils

public class TarUtils extends Object
This class provides static utility methods to work with byte streams.

Fields Summary
Constructors Summary
Methods Summary
public static longcomputeCheckSum(byte[] buf)
Compute the checksum of a tar entry header.

param
buf The tar entry's header buffer.
return
The computed checksum.

        long sum = 0;

        for (int i = 0; i < buf.length; ++i) {
            sum += 255 & buf[i];
        }

        return sum;
    
public static intgetCheckSumOctalBytes(long value, byte[] buf, int offset, int length)
Parse the checksum octal integer from a header buffer.

param
value The header value
param
buf The buffer from which to parse.
param
offset The offset into the buffer from which to parse.
param
length The number of header bytes to parse.
return
The integer value of the entry's checksum.

        getOctalBytes(value, buf, offset, length);

        buf[offset + length - 1] = (byte) ' ";
        buf[offset + length - 2] = 0;

        return offset + length;
    
public static intgetLongOctalBytes(long value, byte[] buf, int offset, int length)
Parse an octal long integer from a header buffer.

param
value The header value
param
buf The buffer from which to parse.
param
offset The offset into the buffer from which to parse.
param
length The number of header bytes to parse.
return
The long value of the octal bytes.

        byte[] temp = new byte[length + 1];

        getOctalBytes(value, temp, 0, length + 1);
        System.arraycopy(temp, 0, buf, offset, length);

        return offset + length;
    
public static intgetNameBytes(java.lang.StringBuffer name, byte[] buf, int offset, int length)
Determine the number of bytes in an entry name.

param
name The header name from which to parse.
param
buf The buffer from which to parse.
param
offset The offset into the buffer from which to parse.
param
length The number of header bytes to parse.
return
The number of bytes in a header's entry name.

        int i;

        for (i = 0; i < length && i < name.length(); ++i) {
            buf[offset + i] = (byte) name.charAt(i);
        }

        for (; i < length; ++i) {
            buf[offset + i] = 0;
        }

        return offset + length;
    
public static intgetOctalBytes(long value, byte[] buf, int offset, int length)
Parse an octal integer from a header buffer.

param
value The header value
param
buf The buffer from which to parse.
param
offset The offset into the buffer from which to parse.
param
length The number of header bytes to parse.
return
The integer value of the octal bytes.

        int    idx = length - 1;

        buf[offset + idx] = 0;
        --idx;
        buf[offset + idx] = (byte) ' ";
        --idx;

        if (value == 0) {
            buf[offset + idx] = (byte) '0";
            --idx;
        } else {
            for (long val = value; idx >= 0 && val > 0; --idx) {
                buf[offset + idx] = (byte) ((byte) '0" + (byte) (val & 7));
                val = val >> 3;
            }
        }

        for (; idx >= 0; --idx) {
            buf[offset + idx] = (byte) ' ";
        }

        return offset + length;
    
public static java.lang.StringBufferparseName(byte[] header, int offset, int length)
Parse an entry name from a header buffer.

param
header The header buffer from which to parse.
param
offset The offset into the buffer from which to parse.
param
length The number of header bytes to parse.
return
The header's entry name.

        StringBuffer result = new StringBuffer(length);
        int          end = offset + length;

        for (int i = offset; i < end; ++i) {
            if (header[i] == 0) {
                break;
            }

            result.append((char) header[i]);
        }

        return result;
    
public static longparseOctal(byte[] header, int offset, int length)
Parse an octal string from a header buffer. This is used for the file permission mode value.

param
header The header buffer from which to parse.
param
offset The offset into the buffer from which to parse.
param
length The number of header bytes to parse.
return
The long value of the octal string.

        long    result = 0;
        boolean stillPadding = true;
        int     end = offset + length;

        for (int i = offset; i < end; ++i) {
            if (header[i] == 0) {
                break;
            }

            if (header[i] == (byte) ' " || header[i] == '0") {
                if (stillPadding) {
                    continue;
                }

                if (header[i] == (byte) ' ") {
                    break;
                }
            }

            stillPadding = false;
            result = (result << 3) + (header[i] - '0");
        }

        return result;