001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.vfs.provider.tar;
018:
019: /**
020: * This class provides static utility methods to work with byte streams.
021: *
022: * @author <a href="mailto:time@ice.com">Timothy Gerard Endres</a>
023: * @author <a href="mailto:stefano@apache.org">Stefano Mazzocchi</a>
024: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
025: */
026: class TarUtils {
027: /**
028: * Parse the checksum octal integer from a header buffer.
029: *
030: * @param offset The offset into the buffer from which to parse.
031: * @param length The number of header bytes to parse.
032: * @param value Description of Parameter
033: * @param buf Description of Parameter
034: * @return The integer value of the entry's checksum.
035: */
036: public static int getCheckSumOctalBytes(final long value,
037: final byte[] buf, final int offset, final int length) {
038: getOctalBytes(value, buf, offset, length);
039:
040: buf[offset + length - 1] = (byte) ' ';
041: buf[offset + length - 2] = 0;
042:
043: return offset + length;
044: }
045:
046: /**
047: * Parse an octal long integer from a header buffer.
048: *
049: * @param offset The offset into the buffer from which to parse.
050: * @param length The number of header bytes to parse.
051: * @param value Description of Parameter
052: * @param buf Description of Parameter
053: * @return The long value of the octal bytes.
054: */
055: public static int getLongOctalBytes(final long value,
056: final byte[] buf, final int offset, final int length) {
057: byte[] temp = new byte[length + 1];
058:
059: getOctalBytes(value, temp, 0, length + 1);
060: System.arraycopy(temp, 0, buf, offset, length);
061:
062: return offset + length;
063: }
064:
065: /**
066: * Determine the number of bytes in an entry name.
067: *
068: * @param offset The offset into the buffer from which to parse.
069: * @param length The number of header bytes to parse.
070: * @param name Description of Parameter
071: * @param buffer Description of Parameter
072: * @return The number of bytes in a header's entry name.
073: */
074: public static int getNameBytes(final StringBuffer name,
075: final byte[] buffer, final int offset, final int length) {
076: int i;
077:
078: for (i = 0; i < length && i < name.length(); ++i) {
079: buffer[offset + i] = (byte) name.charAt(i);
080: }
081:
082: for (; i < length; ++i) {
083: buffer[offset + i] = 0;
084: }
085:
086: return offset + length;
087: }
088:
089: /**
090: * Parse an octal integer from a header buffer.
091: *
092: * @param offset The offset into the buffer from which to parse.
093: * @param length The number of header bytes to parse.
094: * @return The integer value of the octal bytes.
095: */
096: public static int getOctalBytes(final long value,
097: final byte[] buffer, final int offset, final int length) {
098: int idx = length - 1;
099:
100: buffer[offset + idx] = 0;
101: --idx;
102: buffer[offset + idx] = (byte) ' ';
103: --idx;
104:
105: if (value == 0) {
106: buffer[offset + idx] = (byte) '0';
107: --idx;
108: } else {
109: long val = value;
110: while (idx >= 0 && val > 0) {
111: buffer[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
112: val = val >> 3;
113: idx--;
114: }
115: }
116:
117: while (idx >= 0) {
118: buffer[offset + idx] = (byte) ' ';
119: idx--;
120: }
121:
122: return offset + length;
123: }
124:
125: /**
126: * Compute the checksum of a tar entry header.
127: *
128: * @param buffer The tar entry's header buffer.
129: * @return The computed checksum.
130: */
131: public static long computeCheckSum(final byte[] buffer) {
132: long sum = 0;
133:
134: for (int i = 0; i < buffer.length; ++i) {
135: sum += 255 & buffer[i];
136: }
137:
138: return sum;
139: }
140:
141: /**
142: * Parse an entry name from a header buffer.
143: *
144: * @param header The header buffer from which to parse.
145: * @param offset The offset into the buffer from which to parse.
146: * @param length The number of header bytes to parse.
147: * @return The header's entry name.
148: */
149: public static StringBuffer parseName(final byte[] header,
150: final int offset, final int length) {
151: StringBuffer result = new StringBuffer(length);
152: int end = offset + length;
153:
154: for (int i = offset; i < end; ++i) {
155: if (header[i] == 0) {
156: break;
157: }
158:
159: result.append((char) header[i]);
160: }
161:
162: return result;
163: }
164:
165: /**
166: * Parse an octal string from a header buffer. This is used for the file
167: * permission mode value.
168: *
169: * @param header The header buffer from which to parse.
170: * @param offset The offset into the buffer from which to parse.
171: * @param length The number of header bytes to parse.
172: * @return The long value of the octal string.
173: */
174: public static long parseOctal(final byte[] header,
175: final int offset, final int length) {
176: long result = 0;
177: boolean stillPadding = true;
178: int end = offset + length;
179:
180: for (int i = offset; i < end; ++i) {
181: if (header[i] == 0) {
182: break;
183: }
184:
185: if (header[i] == (byte) ' ' || header[i] == '0') {
186: if (stillPadding) {
187: continue;
188: }
189:
190: if (header[i] == (byte) ' ') {
191: break;
192: }
193: }
194:
195: stillPadding = false;
196: result = (result << 3) + (header[i] - '0');
197: }
198:
199: return result;
200: }
201: }
|