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.

ZipShort.java 4.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. /**
  21. * Utility class that represents a two byte integer with conversion
  22. * rules for the big endian byte order of ZIP files.
  23. *
  24. */
  25. public final class ZipShort implements Cloneable {
  26. private static final int BYTE_1_MASK = 0xFF00;
  27. private static final int BYTE_1_SHIFT = 8;
  28. private final int value;
  29. /**
  30. * Create instance from a number.
  31. * @param value the int to store as a ZipShort
  32. * @since 1.1
  33. */
  34. public ZipShort(int value) {
  35. this.value = value;
  36. }
  37. /**
  38. * Create instance from bytes.
  39. * @param bytes the bytes to store as a ZipShort
  40. * @since 1.1
  41. */
  42. public ZipShort(byte[] bytes) {
  43. this(bytes, 0);
  44. }
  45. /**
  46. * Create instance from the two bytes starting at offset.
  47. * @param bytes the bytes to store as a ZipShort
  48. * @param offset the offset to start
  49. * @since 1.1
  50. */
  51. public ZipShort(byte[] bytes, int offset) {
  52. value = ZipShort.getValue(bytes, offset);
  53. }
  54. /**
  55. * Get value as two bytes in big endian byte order.
  56. * @return the value as a a two byte array in big endian byte order
  57. * @since 1.1
  58. */
  59. public byte[] getBytes() {
  60. byte[] result = new byte[2];
  61. putShort(value, result, 0);
  62. return result;
  63. }
  64. /**
  65. * put the value as two bytes in big endian byte order.
  66. * @param value the Java int to convert to bytes
  67. * @param buf the output buffer
  68. * @param offset
  69. * The offset within the output buffer of the first byte to be written.
  70. * must be non-negative and no larger than <tt>buf.length-2</tt>
  71. */
  72. public static void putShort(int value, byte[] buf, int offset) {
  73. buf[offset] = (byte) (value & BYTE_MASK);
  74. buf[offset + 1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
  75. }
  76. /**
  77. * Get value as Java int.
  78. * @return value as a Java int
  79. * @since 1.1
  80. */
  81. public int getValue() {
  82. return value;
  83. }
  84. /**
  85. * Get value as two bytes in big endian byte order.
  86. * @param value the Java int to convert to bytes
  87. * @return the converted int as a byte array in big endian byte order
  88. */
  89. public static byte[] getBytes(int value) {
  90. byte[] result = new byte[2];
  91. result[0] = (byte) (value & BYTE_MASK);
  92. result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
  93. return result;
  94. }
  95. /**
  96. * Helper method to get the value as a java int from two bytes starting at given array offset
  97. * @param bytes the array of bytes
  98. * @param offset the offset to start
  99. * @return the corresponding java int value
  100. */
  101. public static int getValue(byte[] bytes, int offset) {
  102. int value = (bytes[offset + 1] << BYTE_1_SHIFT) & BYTE_1_MASK;
  103. value += (bytes[offset] & BYTE_MASK);
  104. return value;
  105. }
  106. /**
  107. * Helper method to get the value as a java int from a two-byte array
  108. * @param bytes the array of bytes
  109. * @return the corresponding java int value
  110. */
  111. public static int getValue(byte[] bytes) {
  112. return getValue(bytes, 0);
  113. }
  114. /**
  115. * Override to make two instances with same value equal.
  116. * @param o an object to compare
  117. * @return true if the objects are equal
  118. * @since 1.1
  119. */
  120. @Override
  121. public boolean equals(Object o) {
  122. if (o == null || !(o instanceof ZipShort)) {
  123. return false;
  124. }
  125. return value == ((ZipShort) o).getValue();
  126. }
  127. /**
  128. * Override to make two instances with same value equal.
  129. * @return the value stored in the ZipShort
  130. * @since 1.1
  131. */
  132. @Override
  133. public int hashCode() {
  134. return value;
  135. }
  136. @Override
  137. public Object clone() {
  138. try {
  139. return super.clone();
  140. } catch (CloneNotSupportedException cnfe) {
  141. // impossible
  142. throw new RuntimeException(cnfe); //NOSONAR
  143. }
  144. }
  145. @Override
  146. public String toString() {
  147. return "ZipShort value: " + value;
  148. }
  149. }