blob: 55cc7979a534607e1ef0527b21064d608b698f27 [file] [log] [blame]
Torsten Curdtca165392008-07-10 10:17:44 +00001/*
Torsten Curdtab9ebfc2009-01-12 11:15:34 +00002 * 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
Torsten Curdtca165392008-07-10 10:17:44 +00008 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +00009 * http://www.apache.org/licenses/LICENSE-2.0
Torsten Curdtca165392008-07-10 10:17:44 +000010 *
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000011 * 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.
16 *
Torsten Curdtca165392008-07-10 10:17:44 +000017 */
18package org.apache.commons.compress.archivers.zip;
19
20/**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000021 * Utility class that represents a two byte integer with conversion
22 * rules for the big endian byte order of ZIP files.
23 *
Torsten Curdtca165392008-07-10 10:17:44 +000024 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000025public final class ZipShort implements Cloneable {
26 private static final int BYTE_MASK = 0xFF;
27 private static final int BYTE_1_MASK = 0xFF00;
28 private static final int BYTE_1_SHIFT = 8;
29
30 private int value;
Torsten Curdtca165392008-07-10 10:17:44 +000031
32 /**
33 * Create instance from a number.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000034 * @param value the int to store as a ZipShort
Torsten Curdtca165392008-07-10 10:17:44 +000035 * @since 1.1
36 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000037 public ZipShort (int value) {
38 this.value = value;
Torsten Curdtca165392008-07-10 10:17:44 +000039 }
40
41 /**
42 * Create instance from bytes.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000043 * @param bytes the bytes to store as a ZipShort
Torsten Curdtca165392008-07-10 10:17:44 +000044 * @since 1.1
45 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000046 public ZipShort (byte[] bytes) {
47 this(bytes, 0);
Torsten Curdtca165392008-07-10 10:17:44 +000048 }
49
50 /**
51 * Create instance from the two bytes starting at offset.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000052 * @param bytes the bytes to store as a ZipShort
53 * @param offset the offset to start
Torsten Curdtca165392008-07-10 10:17:44 +000054 * @since 1.1
55 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000056 public ZipShort (byte[] bytes, int offset) {
57 value = ZipShort.getValue(bytes, offset);
Torsten Curdtca165392008-07-10 10:17:44 +000058 }
59
60 /**
61 * Get value as two bytes in big endian byte order.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000062 * @return the value as a a two byte array in big endian byte order
Torsten Curdtca165392008-07-10 10:17:44 +000063 * @since 1.1
64 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000065 public byte[] getBytes() {
66 byte[] result = new byte[2];
67 result[0] = (byte) (value & BYTE_MASK);
68 result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
Torsten Curdtca165392008-07-10 10:17:44 +000069 return result;
70 }
71
72 /**
73 * Get value as Java int.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000074 * @return value as a Java int
Torsten Curdtca165392008-07-10 10:17:44 +000075 * @since 1.1
76 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000077 public int getValue() {
78 return value;
79 }
80
81 /**
82 * Get value as two bytes in big endian byte order.
83 * @param value the Java int to convert to bytes
84 * @return the converted int as a byte array in big endian byte order
85 */
86 public static byte[] getBytes(int value) {
87 byte[] result = new byte[2];
88 result[0] = (byte) (value & BYTE_MASK);
89 result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
90 return result;
91 }
92
93 /**
94 * Helper method to get the value as a java int from two bytes starting at given array offset
95 * @param bytes the array of bytes
96 * @param offset the offset to start
97 * @return the correspondanding java int value
98 */
99 public static int getValue(byte[] bytes, int offset) {
100 int value = (bytes[offset + 1] << BYTE_1_SHIFT) & BYTE_1_MASK;
101 value += (bytes[offset] & BYTE_MASK);
102 return value;
103 }
104
105 /**
106 * Helper method to get the value as a java int from a two-byte array
107 * @param bytes the array of bytes
108 * @return the correspondanding java int value
109 */
110 public static int getValue(byte[] bytes) {
111 return getValue(bytes, 0);
Torsten Curdtca165392008-07-10 10:17:44 +0000112 }
113
114 /**
115 * Override to make two instances with same value equal.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000116 * @param o an object to compare
117 * @return true if the objects are equal
Torsten Curdtca165392008-07-10 10:17:44 +0000118 * @since 1.1
119 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000120 public boolean equals(Object o) {
121 if (o == null || !(o instanceof ZipShort)) {
Torsten Curdtca165392008-07-10 10:17:44 +0000122 return false;
123 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000124 return value == ((ZipShort) o).getValue();
Torsten Curdtca165392008-07-10 10:17:44 +0000125 }
126
127 /**
128 * Override to make two instances with same value equal.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000129 * @return the value stored in the ZipShort
Torsten Curdtca165392008-07-10 10:17:44 +0000130 * @since 1.1
131 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000132 public int hashCode() {
133 return value;
Torsten Curdtca165392008-07-10 10:17:44 +0000134 }
135}