1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package edu.internet2.middleware.grouperInstallerExt.org.apache.commons.compress.archivers.zip;
19
20 import java.io.Serializable;
21 import java.math.BigInteger;
22
23 import static edu.internet2.middleware.grouperInstallerExt.org.apache.commons.compress.archivers.zip.ZipConstants.BYTE_MASK;
24
25
26
27
28
29
30
31
32 public final class ZipEightByteInteger implements Serializable {
33 private static final long serialVersionUID = 1L;
34
35 private static final int BYTE_1 = 1;
36 private static final int BYTE_1_MASK = 0xFF00;
37 private static final int BYTE_1_SHIFT = 8;
38
39 private static final int BYTE_2 = 2;
40 private static final int BYTE_2_MASK = 0xFF0000;
41 private static final int BYTE_2_SHIFT = 16;
42
43 private static final int BYTE_3 = 3;
44 private static final long BYTE_3_MASK = 0xFF000000L;
45 private static final int BYTE_3_SHIFT = 24;
46
47 private static final int BYTE_4 = 4;
48 private static final long BYTE_4_MASK = 0xFF00000000L;
49 private static final int BYTE_4_SHIFT = 32;
50
51 private static final int BYTE_5 = 5;
52 private static final long BYTE_5_MASK = 0xFF0000000000L;
53 private static final int BYTE_5_SHIFT = 40;
54
55 private static final int BYTE_6 = 6;
56 private static final long BYTE_6_MASK = 0xFF000000000000L;
57 private static final int BYTE_6_SHIFT = 48;
58
59 private static final int BYTE_7 = 7;
60 private static final long BYTE_7_MASK = 0x7F00000000000000L;
61 private static final int BYTE_7_SHIFT = 56;
62
63 private static final int LEFTMOST_BIT_SHIFT = 63;
64 private static final byte LEFTMOST_BIT = (byte) 0x80;
65
66 private final BigInteger value;
67
68 public static final ZipEightByteIntegerare/grouperInstallerExt/org/apache/commons/compress/archivers/zip/ZipEightByteInteger.html#ZipEightByteInteger">ZipEightByteInteger ZERO = new ZipEightByteInteger(0);
69
70
71
72
73
74 public ZipEightByteInteger(long value) {
75 this(BigInteger.valueOf(value));
76 }
77
78
79
80
81
82 public ZipEightByteInteger(BigInteger value) {
83 this.value = value;
84 }
85
86
87
88
89
90 public ZipEightByteInteger (byte[] bytes) {
91 this(bytes, 0);
92 }
93
94
95
96
97
98
99 public ZipEightByteInteger (byte[] bytes, int offset) {
100 value = ZipEightByteInteger.getValue(bytes, offset);
101 }
102
103
104
105
106
107 public byte[] getBytes() {
108 return ZipEightByteInteger.getBytes(value);
109 }
110
111
112
113
114
115 public long getLongValue() {
116 return value.longValue();
117 }
118
119
120
121
122
123 public BigInteger getValue() {
124 return value;
125 }
126
127
128
129
130
131
132 public static byte[] getBytes(long value) {
133 return getBytes(BigInteger.valueOf(value));
134 }
135
136
137
138
139
140
141 public static byte[] getBytes(BigInteger value) {
142 byte[] result = new byte[8];
143 long val = value.longValue();
144 result[0] = (byte) ((val & BYTE_MASK));
145 result[BYTE_1] = (byte) ((val & BYTE_1_MASK) >> BYTE_1_SHIFT);
146 result[BYTE_2] = (byte) ((val & BYTE_2_MASK) >> BYTE_2_SHIFT);
147 result[BYTE_3] = (byte) ((val & BYTE_3_MASK) >> BYTE_3_SHIFT);
148 result[BYTE_4] = (byte) ((val & BYTE_4_MASK) >> BYTE_4_SHIFT);
149 result[BYTE_5] = (byte) ((val & BYTE_5_MASK) >> BYTE_5_SHIFT);
150 result[BYTE_6] = (byte) ((val & BYTE_6_MASK) >> BYTE_6_SHIFT);
151 result[BYTE_7] = (byte) ((val & BYTE_7_MASK) >> BYTE_7_SHIFT);
152 if (value.testBit(LEFTMOST_BIT_SHIFT)) {
153 result[BYTE_7] |= LEFTMOST_BIT;
154 }
155 return result;
156 }
157
158
159
160
161
162
163
164
165 public static long getLongValue(byte[] bytes, int offset) {
166 return getValue(bytes, offset).longValue();
167 }
168
169
170
171
172
173
174
175
176 public static BigInteger getValue(byte[] bytes, int offset) {
177 long value = ((long) bytes[offset + BYTE_7] << BYTE_7_SHIFT) & BYTE_7_MASK;
178 value += ((long) bytes[offset + BYTE_6] << BYTE_6_SHIFT) & BYTE_6_MASK;
179 value += ((long) bytes[offset + BYTE_5] << BYTE_5_SHIFT) & BYTE_5_MASK;
180 value += ((long) bytes[offset + BYTE_4] << BYTE_4_SHIFT) & BYTE_4_MASK;
181 value += ((long) bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
182 value += ((long) bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
183 value += ((long) bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
184 value += ((long) bytes[offset] & BYTE_MASK);
185 BigInteger val = BigInteger.valueOf(value);
186 return (bytes[offset + BYTE_7] & LEFTMOST_BIT) == LEFTMOST_BIT
187 ? val.setBit(LEFTMOST_BIT_SHIFT) : val;
188 }
189
190
191
192
193
194
195 public static long getLongValue(byte[] bytes) {
196 return getLongValue(bytes, 0);
197 }
198
199
200
201
202
203
204 public static BigInteger getValue(byte[] bytes) {
205 return getValue(bytes, 0);
206 }
207
208
209
210
211
212
213 @Override
214 public boolean equals(Object o) {
215 if (o == null || !(o instanceof ZipEightByteInteger)) {
216 return false;
217 }
218 return value.equals(((ZipEightByteInteger) o).getValue());
219 }
220
221
222
223
224
225 @Override
226 public int hashCode() {
227 return value.hashCode();
228 }
229
230 @Override
231 public String toString() {
232 return "ZipEightByteInteger value: " + value;
233 }
234 }