KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > tar > TarUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 /*
20  * This package is based on the work done by Timothy Gerard Endres
21  * (time@ice.com) to whom the Ant project is very grateful for his great code.
22  */

23
24 package org.apache.tools.tar;
25
26 /**
27  * This class provides static utility methods to work with byte streams.
28  *
29  */

30 public class TarUtils {
31
32     /**
33      * Parse an octal string from a header buffer. This is used for the
34      * file permission mode value.
35      *
36      * @param header The header buffer from which to parse.
37      * @param offset The offset into the buffer from which to parse.
38      * @param length The number of header bytes to parse.
39      * @return The long value of the octal string.
40      */

41     public static long parseOctal(byte[] header, int offset, int length) {
42         long result = 0;
43         boolean stillPadding = true;
44         int end = offset + length;
45
46         for (int i = offset; i < end; ++i) {
47             if (header[i] == 0) {
48                 break;
49             }
50
51             if (header[i] == (byte) ' ' || header[i] == '0') {
52                 if (stillPadding) {
53                     continue;
54                 }
55
56                 if (header[i] == (byte) ' ') {
57                     break;
58                 }
59             }
60
61             stillPadding = false;
62             result = (result << 3) + (header[i] - '0');
63         }
64
65         return result;
66     }
67
68     /**
69      * Parse an entry name from a header buffer.
70      *
71      * @param header The header buffer from which to parse.
72      * @param offset The offset into the buffer from which to parse.
73      * @param length The number of header bytes to parse.
74      * @return The header's entry name.
75      */

76     public static StringBuffer JavaDoc parseName(byte[] header, int offset, int length) {
77         StringBuffer JavaDoc result = new StringBuffer JavaDoc(length);
78         int end = offset + length;
79
80         for (int i = offset; i < end; ++i) {
81             if (header[i] == 0) {
82                 break;
83             }
84
85             result.append((char) header[i]);
86         }
87
88         return result;
89     }
90
91     /**
92      * Determine the number of bytes in an entry name.
93      *
94      * @param name The header name from which to parse.
95      * @param buf The buffer from which to parse.
96      * @param offset The offset into the buffer from which to parse.
97      * @param length The number of header bytes to parse.
98      * @return The number of bytes in a header's entry name.
99      */

100     public static int getNameBytes(StringBuffer JavaDoc name, byte[] buf, int offset, int length) {
101         int i;
102
103         for (i = 0; i < length && i < name.length(); ++i) {
104             buf[offset + i] = (byte) name.charAt(i);
105         }
106
107         for (; i < length; ++i) {
108             buf[offset + i] = 0;
109         }
110
111         return offset + length;
112     }
113
114     /**
115      * Parse an octal integer from a header buffer.
116      *
117      * @param value The header value
118      * @param buf The buffer from which to parse.
119      * @param offset The offset into the buffer from which to parse.
120      * @param length The number of header bytes to parse.
121      * @return The integer value of the octal bytes.
122      */

123     public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
124         int idx = length - 1;
125
126         buf[offset + idx] = 0;
127         --idx;
128         buf[offset + idx] = (byte) ' ';
129         --idx;
130
131         if (value == 0) {
132             buf[offset + idx] = (byte) '0';
133             --idx;
134         } else {
135             for (long val = value; idx >= 0 && val > 0; --idx) {
136                 buf[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
137                 val = val >> 3;
138             }
139         }
140
141         for (; idx >= 0; --idx) {
142             buf[offset + idx] = (byte) ' ';
143         }
144
145         return offset + length;
146     }
147
148     /**
149      * Parse an octal long integer from a header buffer.
150      *
151      * @param value The header value
152      * @param buf The buffer from which to parse.
153      * @param offset The offset into the buffer from which to parse.
154      * @param length The number of header bytes to parse.
155      * @return The long value of the octal bytes.
156      */

157     public static int getLongOctalBytes(long value, byte[] buf, int offset, int length) {
158         byte[] temp = new byte[length + 1];
159
160         getOctalBytes(value, temp, 0, length + 1);
161         System.arraycopy(temp, 0, buf, offset, length);
162
163         return offset + length;
164     }
165
166     /**
167      * Parse the checksum octal integer from a header buffer.
168      *
169      * @param value The header value
170      * @param buf The buffer from which to parse.
171      * @param offset The offset into the buffer from which to parse.
172      * @param length The number of header bytes to parse.
173      * @return The integer value of the entry's checksum.
174      */

175     public static int getCheckSumOctalBytes(long value, byte[] buf, int offset, int length) {
176         getOctalBytes(value, buf, offset, length);
177
178         buf[offset + length - 1] = (byte) ' ';
179         buf[offset + length - 2] = 0;
180
181         return offset + length;
182     }
183
184     /**
185      * Compute the checksum of a tar entry header.
186      *
187      * @param buf The tar entry's header buffer.
188      * @return The computed checksum.
189      */

190     public static long computeCheckSum(byte[] buf) {
191         long sum = 0;
192
193         for (int i = 0; i < buf.length; ++i) {
194             sum += 255 & buf[i];
195         }
196
197         return sum;
198     }
199 }
200
Popular Tags