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.

ZipEightByteInteger.java 7.8 kB

11 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. package org.apache.tools.zip;
  19. import static org.apache.tools.zip.ZipConstants.BYTE_MASK;
  20. import java.math.BigInteger;
  21. /**
  22. * Utility class that represents an eight byte integer with conversion
  23. * rules for the big endian byte order of ZIP files.
  24. */
  25. public final class ZipEightByteInteger {
  26. private static final int BYTE_1 = 1;
  27. private static final int BYTE_1_MASK = 0xFF00;
  28. private static final int BYTE_1_SHIFT = 8;
  29. private static final int BYTE_2 = 2;
  30. private static final int BYTE_2_MASK = 0xFF0000;
  31. private static final int BYTE_2_SHIFT = 16;
  32. private static final int BYTE_3 = 3;
  33. private static final long BYTE_3_MASK = 0xFF000000L;
  34. private static final int BYTE_3_SHIFT = 24;
  35. private static final int BYTE_4 = 4;
  36. private static final long BYTE_4_MASK = 0xFF00000000L;
  37. private static final int BYTE_4_SHIFT = 32;
  38. private static final int BYTE_5 = 5;
  39. private static final long BYTE_5_MASK = 0xFF0000000000L;
  40. private static final int BYTE_5_SHIFT = 40;
  41. private static final int BYTE_6 = 6;
  42. private static final long BYTE_6_MASK = 0xFF000000000000L;
  43. private static final int BYTE_6_SHIFT = 48;
  44. private static final int BYTE_7 = 7;
  45. private static final long BYTE_7_MASK = 0x7F00000000000000L;
  46. private static final int BYTE_7_SHIFT = 56;
  47. private static final int LEFTMOST_BIT_SHIFT = 63;
  48. private static final byte LEFTMOST_BIT = (byte) 0x80;
  49. private final BigInteger value;
  50. public static final ZipEightByteInteger ZERO = new ZipEightByteInteger(0);
  51. /**
  52. * Create instance from a number.
  53. * @param value the long to store as a ZipEightByteInteger
  54. */
  55. public ZipEightByteInteger(long value) {
  56. this(BigInteger.valueOf(value));
  57. }
  58. /**
  59. * Create instance from a number.
  60. * @param value the BigInteger to store as a ZipEightByteInteger
  61. */
  62. public ZipEightByteInteger(BigInteger value) {
  63. this.value = value;
  64. }
  65. /**
  66. * Create instance from bytes.
  67. * @param bytes the bytes to store as a ZipEightByteInteger
  68. */
  69. public ZipEightByteInteger(byte[] bytes) {
  70. this(bytes, 0);
  71. }
  72. /**
  73. * Create instance from the eight bytes starting at offset.
  74. * @param bytes the bytes to store as a ZipEightByteInteger
  75. * @param offset the offset to start
  76. */
  77. public ZipEightByteInteger(byte[] bytes, int offset) {
  78. value = ZipEightByteInteger.getValue(bytes, offset);
  79. }
  80. /**
  81. * Get value as eight bytes in big endian byte order.
  82. * @return value as eight bytes in big endian order
  83. */
  84. public byte[] getBytes() {
  85. return ZipEightByteInteger.getBytes(value);
  86. }
  87. /**
  88. * Get value as Java long.
  89. * @return value as a long
  90. */
  91. public long getLongValue() {
  92. return value.longValue();
  93. }
  94. /**
  95. * Get value as Java long.
  96. * @return value as a long
  97. */
  98. public BigInteger getValue() {
  99. return value;
  100. }
  101. /**
  102. * Get value as eight bytes in big endian byte order.
  103. * @param value the value to convert
  104. * @return value as eight bytes in big endian byte order
  105. */
  106. public static byte[] getBytes(long value) {
  107. return getBytes(BigInteger.valueOf(value));
  108. }
  109. /**
  110. * Get value as eight bytes in big endian byte order.
  111. * @param value the value to convert
  112. * @return value as eight bytes in big endian byte order
  113. */
  114. public static byte[] getBytes(BigInteger value) {
  115. byte[] result = new byte[8];
  116. long val = value.longValue();
  117. result[0] = (byte) ((val & BYTE_MASK));
  118. result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT);
  119. result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT);
  120. result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT);
  121. result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT);
  122. result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT);
  123. result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT);
  124. result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT);
  125. if (value.testBit(LEFTMOST_BIT_SHIFT)) {
  126. result[BYTE_7] |= LEFTMOST_BIT;
  127. }
  128. return result;
  129. }
  130. /**
  131. * Helper method to get the value as a Java long from eight bytes
  132. * starting at given array offset
  133. * @param bytes the array of bytes
  134. * @param offset the offset to start
  135. * @return the corresponding Java long value
  136. */
  137. public static long getLongValue(byte[] bytes, int offset) {
  138. return getValue(bytes, offset).longValue();
  139. }
  140. /**
  141. * Helper method to get the value as a Java BigInteger from eight
  142. * bytes starting at given array offset
  143. * @param bytes the array of bytes
  144. * @param offset the offset to start
  145. * @return the corresponding Java BigInteger value
  146. */
  147. public static BigInteger getValue(byte[] bytes, int offset) {
  148. long value = ((long) bytes[offset + BYTE_7] << BYTE_7_SHIFT) & BYTE_7_MASK;
  149. value += ((long) bytes[offset + BYTE_6] << BYTE_6_SHIFT) & BYTE_6_MASK;
  150. value += ((long) bytes[offset + BYTE_5] << BYTE_5_SHIFT) & BYTE_5_MASK;
  151. value += ((long) bytes[offset + BYTE_4] << BYTE_4_SHIFT) & BYTE_4_MASK;
  152. value += ((long) bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
  153. value += ((long) bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
  154. value += ((long) bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
  155. value += ((long) bytes[offset] & BYTE_MASK);
  156. BigInteger val = BigInteger.valueOf(value);
  157. return (bytes[offset + BYTE_7] & LEFTMOST_BIT) == LEFTMOST_BIT
  158. ? val.setBit(LEFTMOST_BIT_SHIFT) : val;
  159. }
  160. /**
  161. * Helper method to get the value as a Java long from an eight-byte array
  162. * @param bytes the array of bytes
  163. * @return the corresponding Java long value
  164. */
  165. public static long getLongValue(byte[] bytes) {
  166. return getLongValue(bytes, 0);
  167. }
  168. /**
  169. * Helper method to get the value as a Java long from an eight-byte array
  170. * @param bytes the array of bytes
  171. * @return the corresponding Java BigInteger value
  172. */
  173. public static BigInteger getValue(byte[] bytes) {
  174. return getValue(bytes, 0);
  175. }
  176. /**
  177. * Override to make two instances with same value equal.
  178. * @param o an object to compare
  179. * @return true if the objects are equal
  180. */
  181. @Override
  182. public boolean equals(Object o) {
  183. return o instanceof ZipEightByteInteger
  184. && value.equals(((ZipEightByteInteger) o).getValue());
  185. }
  186. /**
  187. * Override to make two instances with same value equal.
  188. * @return the hashCode of the value stored in the ZipEightByteInteger
  189. */
  190. @Override
  191. public int hashCode() {
  192. return value.hashCode();
  193. }
  194. @Override
  195. public String toString() {
  196. return "ZipEightByteInteger value: " + value;
  197. }
  198. }