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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 package org.apache.tools.zip;
22 * Utility class that represents a four byte integer with conversion rules for
23 * the big endian byte order of ZIP files.
26 public final class ZipLong implements Cloneable {
31 * Create instance from a number.
34 * the long to store as a ZipLong
37 public ZipLong(long value) {
42 * Create instance from bytes.
45 * the bytes to store as a ZipLong
48 public ZipLong(byte[] bytes) {
53 * Create instance from the four bytes starting at offset.
56 * the bytes to store as a ZipLong
61 public ZipLong(byte[] bytes, int offset) {
62 value = ZipLong.getValue(bytes, offset);
66 * Get value as four bytes in big endian byte order.
69 * @return value as four bytes in big endian order
71 public byte[] getBytes() {
72 return ZipLong.getBytes(value);
76 * Get value as Java long.
79 * @return value as a long
81 public long getValue() {
86 * Get value as four bytes in big endian byte order.
89 * the value to convert
90 * @return value as four bytes in big endian byte order
92 public static byte[] getBytes(long value) {
93 byte[] result = new byte[4];
94 result[0] = (byte) ((value & 0xFF));
95 result[1] = (byte) ((value & 0xFF00) >> 8);
96 result[2] = (byte) ((value & 0xFF0000) >> 16);
97 result[3] = (byte) ((value & 0xFF000000L) >> 24);
102 * Helper method to get the value as a Java long from four bytes starting at
108 * the offset to start
109 * @return the correspondanding Java long value
111 public static long getValue(byte[] bytes, int offset) {
112 long value = (bytes[offset + 3] << 24) & 0xFF000000L;
113 value += (bytes[offset + 2] << 16) & 0xFF0000;
114 value += (bytes[offset + 1] << 8) & 0xFF00;
115 value += (bytes[offset] & 0xFF);
120 * Helper method to get the value as a Java long from a four-byte array
124 * @return the correspondanding Java long value
126 public static long getValue(byte[] bytes) {
127 return getValue(bytes, 0);
131 * Override to make two instances with same value equal.
134 * an object to compare
135 * @return true if the objects are equal
138 public boolean equals(Object o) {
139 if (o == null || !(o instanceof ZipLong)) {
142 return value == ((ZipLong) o).getValue();
146 * Override to make two instances with same value equal.
148 * @return the value stored in the ZipLong
151 public int hashCode() {