|
1 |
| -/** |
2 |
| - * Copyright 2012 Kamran Zafar |
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 |
| -package org.kamranzafar.jtar; |
19 |
| - |
20 |
| -/** |
21 |
| - * @author Kamran Zafar |
22 |
| - * |
23 |
| - */ |
24 |
| -public class Octal { |
25 |
| - |
26 |
| - /** |
27 |
| - * Parse an octal string from a header buffer. This is used for the file |
28 |
| - * permission mode value. |
29 |
| - * |
30 |
| - * @param header |
31 |
| - * The header buffer from which to parse. |
32 |
| - * @param offset |
33 |
| - * The offset into the buffer from which to parse. |
34 |
| - * @param length |
35 |
| - * The number of header bytes to parse. |
36 |
| - * |
37 |
| - * @return The long value of the octal string. |
38 |
| - */ |
39 |
| - public static long parseOctal(byte[] header, int offset, int length) { |
40 |
| - long result = 0; |
41 |
| - boolean stillPadding = true; |
42 |
| - |
43 |
| - int end = offset + length; |
44 |
| - for (int i = offset; i < end; ++i) { |
45 |
| - if (header[i] == 0) |
46 |
| - break; |
47 |
| - |
48 |
| - if (header[i] == (byte) ' ' || header[i] == '0') { |
49 |
| - if (stillPadding) |
50 |
| - continue; |
51 |
| - |
52 |
| - if (header[i] == (byte) ' ') |
53 |
| - break; |
54 |
| - } |
55 |
| - |
56 |
| - stillPadding = false; |
57 |
| - |
58 |
| - result = ( result << 3 ) + ( header[i] - '0' ); |
59 |
| - } |
60 |
| - |
61 |
| - return result; |
62 |
| - } |
63 |
| - |
64 |
| - /** |
65 |
| - * Parse an octal integer from a header buffer. |
66 |
| - * |
67 |
| - * @param value |
68 |
| - * @param buf |
69 |
| - * The header buffer from which to parse. |
70 |
| - * @param offset |
71 |
| - * The offset into the buffer from which to parse. |
72 |
| - * @param length |
73 |
| - * The number of header bytes to parse. |
74 |
| - * |
75 |
| - * @return The integer value of the octal bytes. |
76 |
| - */ |
77 |
| - public static int getOctalBytes(long value, byte[] buf, int offset, int length) { |
78 |
| - int idx = length - 1; |
79 |
| - |
80 |
| - buf[offset + idx] = 0; |
81 |
| - --idx; |
82 |
| - buf[offset + idx] = (byte) ' '; |
83 |
| - --idx; |
84 |
| - |
85 |
| - if (value == 0) { |
86 |
| - buf[offset + idx] = (byte) '0'; |
87 |
| - --idx; |
88 |
| - } else { |
89 |
| - for (long val = value; idx >= 0 && val > 0; --idx) { |
90 |
| - buf[offset + idx] = (byte) ( (byte) '0' + (byte) ( val & 7 ) ); |
91 |
| - val = val >> 3; |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - for (; idx >= 0; --idx) { |
96 |
| - buf[offset + idx] = (byte) ' '; |
97 |
| - } |
98 |
| - |
99 |
| - return offset + length; |
100 |
| - } |
101 |
| - |
102 |
| - /** |
103 |
| - * Parse the checksum octal integer from a header buffer. |
104 |
| - * |
105 |
| - * @param value |
106 |
| - * @param buf |
107 |
| - * The header buffer from which to parse. |
108 |
| - * @param offset |
109 |
| - * The offset into the buffer from which to parse. |
110 |
| - * @param length |
111 |
| - * The number of header bytes to parse. |
112 |
| - * @return The integer value of the entry's checksum. |
113 |
| - */ |
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; |
119 |
| - } |
120 |
| - |
121 |
| - /** |
122 |
| - * Parse an octal long integer from a header buffer. |
123 |
| - * |
124 |
| - * @param value |
125 |
| - * @param buf |
126 |
| - * The header buffer from which to parse. |
127 |
| - * @param offset |
128 |
| - * The offset into the buffer from which to parse. |
129 |
| - * @param length |
130 |
| - * The number of header bytes to parse. |
131 |
| - * |
132 |
| - * @return The long value of the octal bytes. |
133 |
| - */ |
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; |
139 |
| - } |
140 |
| - |
141 |
| -} |
| 1 | +/** |
| 2 | + * Copyright 2012 Kamran Zafar |
| 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 | +package org.kamranzafar.jtar; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Kamran Zafar |
| 22 | + * |
| 23 | + */ |
| 24 | +public class Octal { |
| 25 | + |
| 26 | + /** |
| 27 | + * Parse an octal string from a header buffer. This is used for the file |
| 28 | + * permission mode value. |
| 29 | + * |
| 30 | + * @param header |
| 31 | + * The header buffer from which to parse. |
| 32 | + * @param offset |
| 33 | + * The offset into the buffer from which to parse. |
| 34 | + * @param length |
| 35 | + * The number of header bytes to parse. |
| 36 | + * |
| 37 | + * @return The long value of the octal string. |
| 38 | + */ |
| 39 | + public static long parseOctal(byte[] header, int offset, int length) { |
| 40 | + long result = 0; |
| 41 | + boolean stillPadding = true; |
| 42 | + |
| 43 | + int end = offset + length; |
| 44 | + for (int i = offset; i < end; ++i) { |
| 45 | + if (header[i] == 0) |
| 46 | + break; |
| 47 | + |
| 48 | + if (header[i] == (byte) ' ' || header[i] == '0') { |
| 49 | + if (stillPadding) |
| 50 | + continue; |
| 51 | + |
| 52 | + if (header[i] == (byte) ' ') |
| 53 | + break; |
| 54 | + } |
| 55 | + |
| 56 | + stillPadding = false; |
| 57 | + |
| 58 | + result = ( result << 3 ) + ( header[i] - '0' ); |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Parse an octal integer from a header buffer. |
| 66 | + * |
| 67 | + * @param value |
| 68 | + * @param buf |
| 69 | + * The header buffer from which to parse. |
| 70 | + * @param offset |
| 71 | + * The offset into the buffer from which to parse. |
| 72 | + * @param length |
| 73 | + * The number of header bytes to parse. |
| 74 | + * |
| 75 | + * @return The integer value of the octal bytes. |
| 76 | + */ |
| 77 | + public static int getOctalBytes(long value, byte[] buf, int offset, int length) { |
| 78 | + int idx = length - 1; |
| 79 | + |
| 80 | + buf[offset + idx] = 0; |
| 81 | + --idx; |
| 82 | + buf[offset + idx] = (byte) ' '; |
| 83 | + --idx; |
| 84 | + |
| 85 | + if (value == 0) { |
| 86 | + buf[offset + idx] = (byte) '0'; |
| 87 | + --idx; |
| 88 | + } else { |
| 89 | + for (long val = value; idx >= 0 && val > 0; --idx) { |
| 90 | + buf[offset + idx] = (byte) ( (byte) '0' + (byte) ( val & 7 ) ); |
| 91 | + val = val >> 3; |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + for (; idx >= 0; --idx) { |
| 96 | + buf[offset + idx] = (byte) ' '; |
| 97 | + } |
| 98 | + |
| 99 | + return offset + length; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Parse the checksum octal integer from a header buffer. |
| 104 | + * |
| 105 | + * @param value |
| 106 | + * @param buf |
| 107 | + * The header buffer from which to parse. |
| 108 | + * @param offset |
| 109 | + * The offset into the buffer from which to parse. |
| 110 | + * @param length |
| 111 | + * The number of header bytes to parse. |
| 112 | + * @return The integer value of the entry's checksum. |
| 113 | + */ |
| 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; |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Parse an octal long integer from a header buffer. |
| 123 | + * |
| 124 | + * @param value |
| 125 | + * @param buf |
| 126 | + * The header buffer from which to parse. |
| 127 | + * @param offset |
| 128 | + * The offset into the buffer from which to parse. |
| 129 | + * @param length |
| 130 | + * The number of header bytes to parse. |
| 131 | + * |
| 132 | + * @return The long value of the octal bytes. |
| 133 | + */ |
| 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; |
| 139 | + } |
| 140 | + |
| 141 | +} |
0 commit comments