2 * Copyright 2012 Kamran Zafar
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.kamranzafar.jtar;
21 * @author Kamran Zafar
27 * Parse an octal string from a header buffer. This is used for the file
28 * permission mode value.
31 * The header buffer from which to parse.
33 * The offset into the buffer from which to parse.
35 * The number of header bytes to parse.
37 * @return The long value of the octal string.
39 public static long parseOctal(byte[] header, int offset, int length) {
41 boolean stillPadding = true;
43 int end = offset + length;
44 for (int i = offset; i < end; ++i) {
48 if (header[i] == (byte) ' ' || header[i] == '0') {
52 if (header[i] == (byte) ' ')
58 result = ( result << 3 ) + ( header[i] - '0' );
65 * Parse an octal integer from a header buffer.
69 * The header buffer from which to parse.
71 * The offset into the buffer from which to parse.
73 * The number of header bytes to parse.
75 * @return The integer value of the octal bytes.
77 public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
80 buf[offset + idx] = 0;
82 buf[offset + idx] = (byte) ' ';
86 buf[offset + idx] = (byte) '0';
89 for (long val = value; idx >= 0 && val > 0; --idx) {
90 buf[offset + idx] = (byte) ( (byte) '0' + (byte) ( val & 7 ) );
95 for (; idx >= 0; --idx) {
96 buf[offset + idx] = (byte) ' ';
99 return offset + length;
103 * Parse the checksum octal integer from a header buffer.
107 * The header buffer from which to parse.
109 * The offset into the buffer from which to parse.
111 * The number of header bytes to parse.
112 * @return The integer value of the entry's checksum.
114 public static int getCheckSumOctalBytes(long value, byte[] buf, int offset, int length) {
115 getOctalBytes( value, buf, offset, length );
116 buf[offset + length - 1] = (byte) ' ';
117 buf[offset + length - 2] = 0;
118 return offset + length;
122 * Parse an octal long integer from a header buffer.
126 * The header buffer from which to parse.
128 * The offset into the buffer from which to parse.
130 * The number of header bytes to parse.
132 * @return The long value of the octal bytes.
134 public static int getLongOctalBytes(long value, byte[] buf, int offset, int length) {
135 byte[] temp = new byte[length + 1];
136 getOctalBytes( value, temp, 0, length + 1 );
137 System.arraycopy( temp, 0, buf, offset, length );
138 return offset + length;