001: package de.masters_of_disaster.ant.tasks.ar;
002:
003: /**
004: * This class provides static utility methods to work with byte streams.
005: */
006: public class ArUtils {
007: /**
008: * Parse an octal string from a header buffer. This is used for the
009: * file permission mode value.
010: *
011: * @param header The header buffer from which to parse.
012: * @param offset The offset into the buffer from which to parse.
013: * @param length The number of header bytes to parse.
014: * @return The long value of the octal string.
015: */
016: public static long parseOctal(byte[] header, int offset, int length) {
017: long result = 0;
018: int end = offset + length;
019:
020: for (int i = offset; i < end; i++) {
021: if (header[i] == (byte) ' ') {
022: break;
023: }
024: result = (result << 3) + (header[i] - '0');
025: }
026:
027: return result;
028: }
029:
030: /**
031: * Parse an entry name from a header buffer.
032: *
033: * @param header The header buffer from which to parse.
034: * @param offset The offset into the buffer from which to parse.
035: * @param length The number of header bytes to parse.
036: * @return The header's entry name.
037: */
038: public static StringBuffer parseName(byte[] header, int offset,
039: int length) {
040: StringBuffer result = new StringBuffer(length);
041: int end = offset + length;
042:
043: for (int i = offset; i < end; i++) {
044: if (header[i] == ' ') {
045: break;
046: }
047:
048: result.append((char) header[i]);
049: }
050:
051: return result;
052: }
053:
054: /**
055: * Write a name into a byte array.
056: *
057: * @param name The name to write.
058: * @param buf The byte array into which to write.
059: * @param offset The offset into the buffer from which to write.
060: * @param length The number of header bytes to write.
061: * @return The number of bytes written to the buffer.
062: */
063: public static int getNameBytes(StringBuffer name, byte[] buf,
064: int offset, int length) {
065: int i;
066: int c = name.length();
067:
068: for (i = 0; i < length && i < c; i++) {
069: buf[offset + i] = (byte) name.charAt(i);
070: }
071:
072: while (i < length) {
073: buf[offset + i] = (byte) ' ';
074: i++;
075: }
076:
077: return offset + length;
078: }
079:
080: /**
081: * Write a long value into a byte array.
082: *
083: * @param value The value to write.
084: * @param buf The byte array into which to write.
085: * @param offset The offset into the buffer from which to write.
086: * @param length The number of header bytes to write.
087: * @return The number of bytes written to the buffer.
088: */
089: public static int getLongBytes(long value, byte[] buf, int offset,
090: int length) {
091: int i;
092: String tmp = Long.toString(value);
093: int c = tmp.length();
094:
095: for (i = 0; i < length && i < c; i++) {
096: buf[offset + i] = (byte) tmp.charAt(i);
097: }
098:
099: while (i < length) {
100: buf[offset + i] = (byte) ' ';
101: i++;
102: }
103:
104: return offset + length;
105: }
106:
107: /**
108: * Write an int value into a byte array.
109: *
110: * @param value The value to write.
111: * @param buf The byte array into which to write.
112: * @param offset The offset into the buffer from which to write.
113: * @param length The number of header bytes to write.
114: * @return The number of bytes written to the buffer.
115: */
116: public static int getIntegerBytes(int value, byte[] buf,
117: int offset, int length) {
118: int i;
119: String tmp = Integer.toString(value);
120: int c = tmp.length();
121:
122: for (i = 0; i < length && i < c; i++) {
123: buf[offset + i] = (byte) tmp.charAt(i);
124: }
125:
126: while (i < length) {
127: buf[offset + i] = (byte) ' ';
128: i++;
129: }
130:
131: return offset + length;
132: }
133:
134: /**
135: * Write an octal value into a byte array.
136: *
137: * @param value The value to write.
138: * @param buf The byte array into which to write.
139: * @param offset The offset into the buffer from which to write.
140: * @param length The number of header bytes to write.
141: * @return The number of bytes written to the buffer.
142: */
143: public static int getOctalBytes(long value, byte[] buf, int offset,
144: int length) {
145: int i;
146: String tmp = Long.toOctalString(value);
147: int c = tmp.length();
148:
149: for (i = 0; i < length && i < c; i++) {
150: buf[offset + i] = (byte) tmp.charAt(i);
151: }
152:
153: while (i < length) {
154: buf[offset + i] = (byte) ' ';
155: i++;
156: }
157:
158: return offset + length;
159: }
160: }
|