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 four 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 ZipLong implements Cloneable {
  25. private long value;
  26. /**
  27. * Create instance from a number.
  28. *
  29. * @since 1.1
  30. */
  31. public ZipLong(long value) {
  32. this.value = value;
  33. }
  34. /**
  35. * Create instance from bytes.
  36. *
  37. * @since 1.1
  38. */
  39. public ZipLong (byte[] bytes) {
  40. this(bytes, 0);
  41. }
  42. /**
  43. * Create instance from the four bytes starting at offset.
  44. *
  45. * @since 1.1
  46. */
  47. public ZipLong (byte[] bytes, int offset) {
  48. value = (bytes[offset + 3] << 24) & 0xFF000000L;
  49. value += (bytes[offset + 2] << 16) & 0xFF0000;
  50. value += (bytes[offset + 1] << 8) & 0xFF00;
  51. value += (bytes[offset] & 0xFF);
  52. }
  53. /**
  54. * Get value as two bytes in big endian byte order.
  55. *
  56. * @since 1.1
  57. */
  58. public byte[] getBytes() {
  59. byte[] result = new byte[4];
  60. result[0] = (byte) ((value & 0xFF));
  61. result[1] = (byte) ((value & 0xFF00) >> 8);
  62. result[2] = (byte) ((value & 0xFF0000) >> 16);
  63. result[3] = (byte) ((value & 0xFF000000l) >> 24);
  64. return result;
  65. }
  66. /**
  67. * Get value as Java int.
  68. *
  69. * @since 1.1
  70. */
  71. public long getValue() {
  72. return value;
  73. }
  74. /**
  75. * Override to make two instances with same value equal.
  76. *
  77. * @since 1.1
  78. */
  79. public boolean equals(Object o) {
  80. if (o == null || !(o instanceof ZipLong)) {
  81. return false;
  82. }
  83. return value == ((ZipLong) o).getValue();
  84. }
  85. /**
  86. * Override to make two instances with same value equal.
  87. *
  88. * @since 1.1
  89. */
  90. public int hashCode() {
  91. return (int) value;
  92. }
  93. }