You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

TarUtils.java 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. * This package is based on the work done by Timothy Gerard Endres
  20. * (time@ice.com) to whom the Ant project is very grateful for his great code.
  21. */
  22. package org.apache.tools.tar;
  23. /**
  24. * This class provides static utility methods to work with byte streams.
  25. *
  26. */
  27. public class TarUtils {
  28. /**
  29. * Parse an octal string from a header buffer. This is used for the
  30. * file permission mode value.
  31. *
  32. * @param header The header buffer from which to parse.
  33. * @param offset The offset into the buffer from which to parse.
  34. * @param length The number of header bytes to parse.
  35. * @return The long value of the octal string.
  36. */
  37. public static long parseOctal(byte[] header, int offset, int length) {
  38. long result = 0;
  39. boolean stillPadding = true;
  40. int end = offset + length;
  41. for (int i = offset; i < end; ++i) {
  42. if (header[i] == 0) {
  43. break;
  44. }
  45. if (header[i] == (byte) ' ' || header[i] == '0') {
  46. if (stillPadding) {
  47. continue;
  48. }
  49. if (header[i] == (byte) ' ') {
  50. break;
  51. }
  52. }
  53. stillPadding = false;
  54. result = (result << 3) + (header[i] - '0');
  55. }
  56. return result;
  57. }
  58. /**
  59. * Parse an entry name from a header buffer.
  60. *
  61. * @param header The header buffer from which to parse.
  62. * @param offset The offset into the buffer from which to parse.
  63. * @param length The number of header bytes to parse.
  64. * @return The header's entry name.
  65. */
  66. public static StringBuffer parseName(byte[] header, int offset, int length) {
  67. StringBuffer result = new StringBuffer(length);
  68. int end = offset + length;
  69. for (int i = offset; i < end; ++i) {
  70. if (header[i] == 0) {
  71. break;
  72. }
  73. result.append((char) header[i]);
  74. }
  75. return result;
  76. }
  77. /**
  78. * Determine the number of bytes in an entry name.
  79. *
  80. * @param name The header name from which to parse.
  81. * @param buf The buffer from which to parse.
  82. * @param offset The offset into the buffer from which to parse.
  83. * @param length The number of header bytes to parse.
  84. * @return The number of bytes in a header's entry name.
  85. */
  86. public static int getNameBytes(StringBuffer name, byte[] buf, int offset, int length) {
  87. int i;
  88. for (i = 0; i < length && i < name.length(); ++i) {
  89. buf[offset + i] = (byte) name.charAt(i);
  90. }
  91. for (; i < length; ++i) {
  92. buf[offset + i] = 0;
  93. }
  94. return offset + length;
  95. }
  96. /**
  97. * Parse an octal integer from a header buffer.
  98. *
  99. * @param value The header value
  100. * @param buf The buffer from which to parse.
  101. * @param offset The offset into the buffer from which to parse.
  102. * @param length The number of header bytes to parse.
  103. * @return The integer value of the octal bytes.
  104. */
  105. public static int getOctalBytes(long value, byte[] buf, int offset, int length) {
  106. int idx = length - 1;
  107. buf[offset + idx] = 0;
  108. --idx;
  109. buf[offset + idx] = (byte) ' ';
  110. --idx;
  111. if (value == 0) {
  112. buf[offset + idx] = (byte) '0';
  113. --idx;
  114. } else {
  115. for (long val = value; idx >= 0 && val > 0; --idx) {
  116. buf[offset + idx] = (byte) ((byte) '0' + (byte) (val & 7));
  117. val = val >> 3;
  118. }
  119. }
  120. for (; idx >= 0; --idx) {
  121. buf[offset + idx] = (byte) ' ';
  122. }
  123. return offset + length;
  124. }
  125. /**
  126. * Parse an octal long integer from a header buffer.
  127. *
  128. * @param value The header value
  129. * @param buf The buffer from which to parse.
  130. * @param offset The offset into the buffer from which to parse.
  131. * @param length The number of header bytes to parse.
  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. * Parse the checksum octal integer from a header buffer.
  142. *
  143. * @param value The header value
  144. * @param buf The buffer from which to parse.
  145. * @param offset The offset into the buffer from which to parse.
  146. * @param length The number of header bytes to parse.
  147. * @return The integer value of the entry's checksum.
  148. */
  149. public static int getCheckSumOctalBytes(long value, byte[] buf, int offset, int length) {
  150. getOctalBytes(value, buf, offset, length);
  151. buf[offset + length - 1] = (byte) ' ';
  152. buf[offset + length - 2] = 0;
  153. return offset + length;
  154. }
  155. /**
  156. * Compute the checksum of a tar entry header.
  157. *
  158. * @param buf The tar entry's header buffer.
  159. * @return The computed checksum.
  160. */
  161. public static long computeCheckSum(byte[] buf) {
  162. long sum = 0;
  163. for (int i = 0; i < buf.length; ++i) {
  164. sum += 255 & buf[i];
  165. }
  166. return sum;
  167. }
  168. }