1 18 19 23 24 package org.apache.tools.tar; 25 26 30 public class TarUtils { 31 32 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 76 public static StringBuffer parseName(byte[] header, int offset, int length) { 77 StringBuffer result = new StringBuffer (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 100 public static int getNameBytes(StringBuffer 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 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 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 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 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
|