1. /*
  2. * Copyright 2001-2002,2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.zip;
  18. /**
  19. * Utility class that represents a two byte integer with conversion
  20. * rules for the big endian byte order of ZIP files.
  21. *
  22. * @version $Revision: 1.6.2.4 $
  23. */
  24. public final class ZipShort implements Cloneable {
  25. private int value;
  26. /**
  27. * Create instance from a number.
  28. *
  29. * @since 1.1
  30. */
  31. public ZipShort (int value) {
  32. this.value = value;
  33. }
  34. /**
  35. * Create instance from bytes.
  36. *
  37. * @since 1.1
  38. */
  39. public ZipShort (byte[] bytes) {
  40. this(bytes, 0);
  41. }
  42. /**
  43. * Create instance from the two bytes starting at offset.
  44. *
  45. * @since 1.1
  46. */
  47. public ZipShort (byte[] bytes, int offset) {
  48. value = (bytes[offset + 1] << 8) & 0xFF00;
  49. value += (bytes[offset] & 0xFF);
  50. }
  51. /**
  52. * Get value as two bytes in big endian byte order.
  53. *
  54. * @since 1.1
  55. */
  56. public byte[] getBytes() {
  57. byte[] result = new byte[2];
  58. result[0] = (byte) (value & 0xFF);
  59. result[1] = (byte) ((value & 0xFF00) >> 8);
  60. return result;
  61. }
  62. /**
  63. * Get value as Java int.
  64. *
  65. * @since 1.1
  66. */
  67. public int getValue() {
  68. return value;
  69. }
  70. /**
  71. * Override to make two instances with same value equal.
  72. *
  73. * @since 1.1
  74. */
  75. public boolean equals(Object o) {
  76. if (o == null || !(o instanceof ZipShort)) {
  77. return false;
  78. }
  79. return value == ((ZipShort) o).getValue();
  80. }
  81. /**
  82. * Override to make two instances with same value equal.
  83. *
  84. * @since 1.1
  85. */
  86. public int hashCode() {
  87. return value;
  88. }
  89. }