001: /*
002: * Copyright 2001-2002,2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017:
018: package org.apache.tools.zip;
019:
020: /**
021: * Utility class that represents a four byte integer with conversion
022: * rules for the big endian byte order of ZIP files.
023: *
024: * @author Stefan Bodewig
025: * @version $Revision: 1.6.2.3 $
026: */
027: public final class ZipLong implements Cloneable {
028:
029: private long value;
030:
031: /**
032: * Create instance from a number.
033: *
034: * @since 1.1
035: */
036: public ZipLong(long value) {
037: this .value = value;
038: }
039:
040: /**
041: * Create instance from bytes.
042: *
043: * @since 1.1
044: */
045: public ZipLong(byte[] bytes) {
046: this (bytes, 0);
047: }
048:
049: /**
050: * Create instance from the four bytes starting at offset.
051: *
052: * @since 1.1
053: */
054: public ZipLong(byte[] bytes, int offset) {
055: value = (bytes[offset + 3] << 24) & 0xFF000000L;
056: value += (bytes[offset + 2] << 16) & 0xFF0000;
057: value += (bytes[offset + 1] << 8) & 0xFF00;
058: value += (bytes[offset] & 0xFF);
059: }
060:
061: /**
062: * Get value as two bytes in big endian byte order.
063: *
064: * @since 1.1
065: */
066: public byte[] getBytes() {
067: byte[] result = new byte[4];
068: result[0] = (byte) ((value & 0xFF));
069: result[1] = (byte) ((value & 0xFF00) >> 8);
070: result[2] = (byte) ((value & 0xFF0000) >> 16);
071: result[3] = (byte) ((value & 0xFF000000l) >> 24);
072: return result;
073: }
074:
075: /**
076: * Get value as Java int.
077: *
078: * @since 1.1
079: */
080: public long getValue() {
081: return value;
082: }
083:
084: /**
085: * Override to make two instances with same value equal.
086: *
087: * @since 1.1
088: */
089: @Override
090: public boolean equals(Object o) {
091: if (o == null || !(o instanceof ZipLong)) {
092: return false;
093: }
094: return value == ((ZipLong) o).getValue();
095: }
096:
097: /**
098: * Override to make two instances with same value equal.
099: *
100: * @since 1.1
101: */
102: @Override
103: public int hashCode() {
104: return (int) value;
105: }
106: }
|