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