source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/tar/TarUtils.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 5.8 KB
Line 
1/*
2 * Copyright 2000,2002,2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18/*
19 * This package is based on the work done by Timothy Gerard Endres
20 * ([email protected]) to whom the Ant project is very grateful for his great code.
21 */
22
23package org.apache.tools.tar;
24
25/**
26 * This class provides static utility methods to work with byte streams.
27 *
28 */
29public class TarUtils {
30
31 /**
32 * Parse an octal string from a header buffer. This is used for the
33 * file permission mode value.
34 *
35 * @param header The header buffer from which to parse.
36 * @param offset The offset into the buffer from which to parse.
37 * @param length The number of header bytes to parse.
38 * @return The long value of the octal string.
39 */
40 public static long parseOctal(byte[] header, int offset, int length) {
41 long result = 0;
42 boolean stillPadding = true;
43 int end = offset + length;
44
45 for (int i = offset; i < end; ++i) {
46 if (header[i] == 0) {
47 break;
48 }
49
50 if (header[i] == (byte) ' ' || header[i] == '0') {
51 if (stillPadding) {
52 continue;
53 }
54
55 if (header[i] == (byte) ' ') {
56 break;
57 }
58 }
59
60 stillPadding = false;
61 result = (result << 3) + (header[i] - '0');
62 }
63
64 return result;
65 }
66
67 /**
68 * Parse an entry name from a header buffer.
69 *
70 * @param header The header buffer from which to parse.
71 * @param offset The offset into the buffer from which to parse.
72 * @param length The number of header bytes to parse.
73 * @return The header's entry name.
74 */
75 public static StringBuffer parseName(byte[] header, int offset, int length) {
76 StringBuffer result = new StringBuffer(length);
77 int end = offset + length;
78
79 for (int i = offset; i < end; ++i) {
80 if (header[i] == 0) {
81 break;
82 }
83
84 result.append((char) header[i]);
85 }
86
87 return result;
88 }
89
90 /**
91 * Determine the number of bytes in an entry name.
92 *
93 * @param name The header name from which to parse.
94 * @param offset The offset into the buffer from which to parse.
95 * @param length The number of header bytes to parse.
96 * @return The number of bytes in a header's entry name.
97 */
98 public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
99 int i;
100
101 for (i = 0; i < length && i < name.length(); ++i) {
102 buf[offset + i] = (byte) name.charAt(i);
103 }
104
105 for (; i < length; ++i) {
106 buf[offset + i] = 0;
107 }
108
109 return offset + length;
110 }
111
112 /**
113 * Parse an octal integer from a header buffer.
114 *
115 * @param value The header value
116 * @param offset The offset into the buffer from which to parse.
117 * @param length The number of header bytes to parse.
118 * @return The integer value of the octal bytes.
119 */
120 public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
121 int idx = length - 1;
122
123 buf[offset + idx] = 0;
124 --idx;
125 buf[offset + idx] = (byte) ' ';
126 --idx;
127
128 if (value == 0) {
129 buf[offset + idx] = (byte) '0';
130 --idx;
131 } else {
132 for (long val = value; idx >= 0 && val > 0; --idx) {
133 buf[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
134 val = val >> 3;
135 }
136 }
137
138 for (; idx >= 0; --idx) {
139 buf[offset + idx] = (byte) ' ';
140 }
141
142 return offset + length;
143 }
144
145 /**
146 * Parse an octal long integer from a header buffer.
147 *
148 * @param value The header value
149 * @param offset The offset into the buffer from which to parse.
150 * @param length The number of header bytes to parse.
151 * @return The long value of the octal bytes.
152 */
153 public static int getLongOctalBytes(long value, byte[] buf, int offset, int length) {
154 byte[] temp = new byte[length + 1];
155
156 getOctalBytes(value, temp, 0, length + 1);
157 System.arraycopy(temp, 0, buf, offset, length);
158
159 return offset + length;
160 }
161
162 /**
163 * Parse the checksum octal integer from a header buffer.
164 *
165 * @param value The header value
166 * @param offset The offset into the buffer from which to parse.
167 * @param length The number of header bytes to parse.
168 * @return The integer value of the entry's checksum.
169 */
170 public static int getCheckSumOctalBytes(long value, byte[] buf, int offset, int length) {
171 getOctalBytes(value, buf, offset, length);
172
173 buf[offset + length - 1] = (byte) ' ';
174 buf[offset + length - 2] = 0;
175
176 return offset + length;
177 }
178
179 /**
180 * Compute the checksum of a tar entry header.
181 *
182 * @param buf The tar entry's header buffer.
183 * @return The computed checksum.
184 */
185 public static long computeCheckSum(byte[] buf) {
186 long sum = 0;
187
188 for (int i = 0; i < buf.length; ++i) {
189 sum += 255 & buf[i];
190 }
191
192 return sum;
193 }
194}
Note: See TracBrowser for help on using the repository browser.