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 two 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 ZipShort implements Cloneable {
028:
029: private int value;
030:
031: /**
032: * Create instance from a number.
033: *
034: * @since 1.1
035: */
036: public ZipShort(int value) {
037: this .value = value;
038: }
039:
040: /**
041: * Create instance from bytes.
042: *
043: * @since 1.1
044: */
045: public ZipShort(byte[] bytes) {
046: this (bytes, 0);
047: }
048:
049: /**
050: * Create instance from the two bytes starting at offset.
051: *
052: * @since 1.1
053: */
054: public ZipShort(byte[] bytes, int offset) {
055: value = (bytes[offset + 1] << 8) & 0xFF00;
056: value += (bytes[offset] & 0xFF);
057: }
058:
059: /**
060: * Get value as two bytes in big endian byte order.
061: *
062: * @since 1.1
063: */
064: public byte[] getBytes() {
065: byte[] result = new byte[2];
066: result[0] = (byte) (value & 0xFF);
067: result[1] = (byte) ((value & 0xFF00) >> 8);
068: return result;
069: }
070:
071: /**
072: * Get value as Java int.
073: *
074: * @since 1.1
075: */
076: public int getValue() {
077: return value;
078: }
079:
080: /**
081: * Override to make two instances with same value equal.
082: *
083: * @since 1.1
084: */
085: @Override
086: public boolean equals(Object o) {
087: if (o == null || !(o instanceof ZipShort)) {
088: return false;
089: }
090: return value == ((ZipShort) o).getValue();
091: }
092:
093: /**
094: * Override to make two instances with same value equal.
095: *
096: * @since 1.1
097: */
098: @Override
099: public int hashCode() {
100: return value;
101: }
102: }
|