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.

ZipLong.java 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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 static org.apache.tools.zip.ZipConstants.WORD;
  21. /**
  22. * Utility class that represents a four byte integer with conversion
  23. * rules for the big endian byte order of ZIP files.
  24. *
  25. */
  26. public final class ZipLong implements Cloneable {
  27. //private static final int BYTE_BIT_SIZE = 8;
  28. private static final int BYTE_1 = 1;
  29. private static final int BYTE_1_MASK = 0xFF00;
  30. private static final int BYTE_1_SHIFT = 8;
  31. private static final int BYTE_2 = 2;
  32. private static final int BYTE_2_MASK = 0xFF0000;
  33. private static final int BYTE_2_SHIFT = 16;
  34. private static final int BYTE_3 = 3;
  35. private static final long BYTE_3_MASK = 0xFF000000L;
  36. private static final int BYTE_3_SHIFT = 24;
  37. private final long value;
  38. /** Central File Header Signature */
  39. public static final ZipLong CFH_SIG = new ZipLong(0X02014B50L);
  40. /** Local File Header Signature */
  41. public static final ZipLong LFH_SIG = new ZipLong(0X04034B50L);
  42. /**
  43. * Data Descriptor signature
  44. */
  45. public static final ZipLong DD_SIG = new ZipLong(0X08074B50L);
  46. /**
  47. * Value stored in size and similar fields if ZIP64 extensions are
  48. * used.
  49. */
  50. static final ZipLong ZIP64_MAGIC = new ZipLong(ZipConstants.ZIP64_MAGIC);
  51. /**
  52. * Create instance from a number.
  53. * @param value the long to store as a ZipLong
  54. * @since 1.1
  55. */
  56. public ZipLong(long value) {
  57. this.value = value;
  58. }
  59. /**
  60. * Create instance from bytes.
  61. * @param bytes the bytes to store as a ZipLong
  62. * @since 1.1
  63. */
  64. public ZipLong(byte[] bytes) {
  65. this(bytes, 0);
  66. }
  67. /**
  68. * Create instance from the four bytes starting at offset.
  69. * @param bytes the bytes to store as a ZipLong
  70. * @param offset the offset to start
  71. * @since 1.1
  72. */
  73. public ZipLong(byte[] bytes, int offset) {
  74. value = ZipLong.getValue(bytes, offset);
  75. }
  76. /**
  77. * Get value as four bytes in big endian byte order.
  78. * @since 1.1
  79. * @return value as four bytes in big endian order
  80. */
  81. public byte[] getBytes() {
  82. return ZipLong.getBytes(value);
  83. }
  84. /**
  85. * Get value as Java long.
  86. * @since 1.1
  87. * @return value as a long
  88. */
  89. public long getValue() {
  90. return value;
  91. }
  92. /**
  93. * Get value as four bytes in big endian byte order.
  94. * @param value the value to convert
  95. * @return value as four bytes in big endian byte order
  96. */
  97. public static byte[] getBytes(long value) {
  98. byte[] result = new byte[WORD];
  99. putLong(value, result, 0);
  100. return result;
  101. }
  102. /**
  103. * put the value as four bytes in big endian byte order.
  104. * @param value the Java long to convert to bytes
  105. * @param buf the output buffer
  106. * @param offset
  107. * The offset within the output buffer of the first byte to be written.
  108. * must be non-negative and no larger than <tt>buf.length-4</tt>
  109. */
  110. public static void putLong(long value, byte[] buf, int offset) {
  111. buf[offset++] = (byte) ((value & BYTE_MASK));
  112. buf[offset++] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
  113. buf[offset++] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT);
  114. buf[offset] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT);
  115. }
  116. public void putLong(byte[] buf, int offset) {
  117. putLong(value, buf, offset);
  118. }
  119. /**
  120. * Helper method to get the value as a Java long from four bytes starting at given array offset
  121. * @param bytes the array of bytes
  122. * @param offset the offset to start
  123. * @return the corresponding Java long value
  124. */
  125. public static long getValue(byte[] bytes, int offset) {
  126. long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
  127. value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
  128. value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
  129. value += (bytes[offset] & BYTE_MASK);
  130. return value;
  131. }
  132. /**
  133. * Helper method to get the value as a Java long from a four-byte array
  134. * @param bytes the array of bytes
  135. * @return the corresponding Java long value
  136. */
  137. public static long getValue(byte[] bytes) {
  138. return getValue(bytes, 0);
  139. }
  140. /**
  141. * Override to make two instances with same value equal.
  142. * @param o an object to compare
  143. * @return true if the objects are equal
  144. * @since 1.1
  145. */
  146. @Override
  147. public boolean equals(Object o) {
  148. return o instanceof ZipLong && value == ((ZipLong) o).getValue();
  149. }
  150. /**
  151. * Override to make two instances with same value equal.
  152. * @return the value stored in the ZipLong
  153. * @since 1.1
  154. */
  155. @Override
  156. public int hashCode() {
  157. return (int) value;
  158. }
  159. @Override
  160. public Object clone() {
  161. try {
  162. return super.clone();
  163. } catch (CloneNotSupportedException cnfe) {
  164. // impossible
  165. throw new RuntimeException(cnfe); //NOSONAR
  166. }
  167. }
  168. @Override
  169. public String toString() {
  170. return "ZipLong value: " + value;
  171. }
  172. }