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.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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. result[0] = (byte) (value & BYTE_MASK);
  62. result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
  63. return result;
  64. }
  65. /**
  66. * Get value as Java int.
  67. * @return value as a Java int
  68. * @since 1.1
  69. */
  70. public int getValue() {
  71. return value;
  72. }
  73. /**
  74. * Get value as two bytes in big endian byte order.
  75. * @param value the Java int to convert to bytes
  76. * @return the converted int as a byte array in big endian byte order
  77. */
  78. public static byte[] getBytes(int value) {
  79. byte[] result = new byte[2];
  80. result[0] = (byte) (value & BYTE_MASK);
  81. result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
  82. return result;
  83. }
  84. /**
  85. * Helper method to get the value as a java int from two bytes starting at given array offset
  86. * @param bytes the array of bytes
  87. * @param offset the offset to start
  88. * @return the corresponding java int value
  89. */
  90. public static int getValue(byte[] bytes, int offset) {
  91. int value = (bytes[offset + 1] << BYTE_1_SHIFT) & BYTE_1_MASK;
  92. value += (bytes[offset] & BYTE_MASK);
  93. return value;
  94. }
  95. /**
  96. * Helper method to get the value as a java int from a two-byte array
  97. * @param bytes the array of bytes
  98. * @return the corresponding java int value
  99. */
  100. public static int getValue(byte[] bytes) {
  101. return getValue(bytes, 0);
  102. }
  103. /**
  104. * Override to make two instances with same value equal.
  105. * @param o an object to compare
  106. * @return true if the objects are equal
  107. * @since 1.1
  108. */
  109. @Override
  110. public boolean equals(Object o) {
  111. if (o == null || !(o instanceof ZipShort)) {
  112. return false;
  113. }
  114. return value == ((ZipShort) o).getValue();
  115. }
  116. /**
  117. * Override to make two instances with same value equal.
  118. * @return the value stored in the ZipShort
  119. * @since 1.1
  120. */
  121. @Override
  122. public int hashCode() {
  123. return value;
  124. }
  125. @Override
  126. public Object clone() {
  127. try {
  128. return super.clone();
  129. } catch (CloneNotSupportedException cnfe) {
  130. // impossible
  131. throw new RuntimeException(cnfe);
  132. }
  133. }
  134. @Override
  135. public String toString() {
  136. return "ZipShort value: " + value;
  137. }
  138. }