1 | /*
|
---|
2 | * Copyright 2001-2002,2004-2005 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 |
|
---|
18 | package org.apache.tools.zip;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Utility class that represents a two byte integer with conversion
|
---|
22 | * rules for the big endian byte order of ZIP files.
|
---|
23 | *
|
---|
24 | */
|
---|
25 | public final class ZipShort implements Cloneable {
|
---|
26 |
|
---|
27 | private int value;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Create instance from a number.
|
---|
31 | *
|
---|
32 | * @since 1.1
|
---|
33 | */
|
---|
34 | public ZipShort (int value) {
|
---|
35 | this.value = value;
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Create instance from bytes.
|
---|
40 | *
|
---|
41 | * @since 1.1
|
---|
42 | */
|
---|
43 | public ZipShort (byte[] bytes) {
|
---|
44 | this(bytes, 0);
|
---|
45 | }
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * Create instance from the two bytes starting at offset.
|
---|
49 | *
|
---|
50 | * @since 1.1
|
---|
51 | */
|
---|
52 | public ZipShort (byte[] bytes, int offset) {
|
---|
53 | value = (bytes[offset + 1] << 8) & 0xFF00;
|
---|
54 | value += (bytes[offset] & 0xFF);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * Get value as two bytes in big endian byte order.
|
---|
59 | *
|
---|
60 | * @since 1.1
|
---|
61 | */
|
---|
62 | public byte[] getBytes() {
|
---|
63 | byte[] result = new byte[2];
|
---|
64 | result[0] = (byte) (value & 0xFF);
|
---|
65 | result[1] = (byte) ((value & 0xFF00) >> 8);
|
---|
66 | return result;
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Get value as Java int.
|
---|
71 | *
|
---|
72 | * @since 1.1
|
---|
73 | */
|
---|
74 | public int getValue() {
|
---|
75 | return value;
|
---|
76 | }
|
---|
77 |
|
---|
78 | /**
|
---|
79 | * Override to make two instances with same value equal.
|
---|
80 | *
|
---|
81 | * @since 1.1
|
---|
82 | */
|
---|
83 | public boolean equals(Object o) {
|
---|
84 | if (o == null || !(o instanceof ZipShort)) {
|
---|
85 | return false;
|
---|
86 | }
|
---|
87 | return value == ((ZipShort) o).getValue();
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Override to make two instances with same value equal.
|
---|
92 | *
|
---|
93 | * @since 1.1
|
---|
94 | */
|
---|
95 | public int hashCode() {
|
---|
96 | return value;
|
---|
97 | }
|
---|
98 | }
|
---|