blob: 01c5f274794b6ebe075d251156ab94c2e74f2463 [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 four 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 ZipLong implements Cloneable {
26
27 private static final int WORD = 4;
28 //private static final int BYTE_BIT_SIZE = 8;
29 private static final int BYTE_MASK = 0xFF;
30
31 private static final int BYTE_1 = 1;
32 private static final int BYTE_1_MASK = 0xFF00;
33 private static final int BYTE_1_SHIFT = 8;
34
35 private static final int BYTE_2 = 2;
36 private static final int BYTE_2_MASK = 0xFF0000;
37 private static final int BYTE_2_SHIFT = 16;
38
39 private static final int BYTE_3 = 3;
40 private static final long BYTE_3_MASK = 0xFF000000L;
41 private static final int BYTE_3_SHIFT = 24;
42
43 private long value;
Torsten Curdtca165392008-07-10 10:17:44 +000044
45 /**
46 * Create instance from a number.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000047 * @param value the long to store as a ZipLong
Torsten Curdtca165392008-07-10 10:17:44 +000048 * @since 1.1
49 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000050 public ZipLong(long value) {
51 this.value = value;
Torsten Curdtca165392008-07-10 10:17:44 +000052 }
53
54 /**
55 * Create instance from bytes.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000056 * @param bytes the bytes to store as a ZipLong
Torsten Curdtca165392008-07-10 10:17:44 +000057 * @since 1.1
58 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000059 public ZipLong (byte[] bytes) {
60 this(bytes, 0);
Torsten Curdtca165392008-07-10 10:17:44 +000061 }
62
63 /**
64 * Create instance from the four bytes starting at offset.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000065 * @param bytes the bytes to store as a ZipLong
66 * @param offset the offset to start
Torsten Curdtca165392008-07-10 10:17:44 +000067 * @since 1.1
68 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000069 public ZipLong (byte[] bytes, int offset) {
70 value = ZipLong.getValue(bytes, offset);
Torsten Curdtca165392008-07-10 10:17:44 +000071 }
72
73 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000074 * Get value as four bytes in big endian byte order.
Torsten Curdtca165392008-07-10 10:17:44 +000075 * @since 1.1
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000076 * @return value as four bytes in big endian order
Torsten Curdtca165392008-07-10 10:17:44 +000077 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +000078 public byte[] getBytes() {
79 return ZipLong.getBytes(value);
80 }
81
82 /**
83 * Get value as Java long.
84 * @since 1.1
85 * @return value as a long
86 */
87 public long getValue() {
88 return value;
89 }
90
91 /**
92 * Get value as four bytes in big endian byte order.
93 * @param value the value to convert
94 * @return value as four bytes in big endian byte order
95 */
96 public static byte[] getBytes(long value) {
97 byte[] result = new byte[WORD];
98 result[0] = (byte) ((value & BYTE_MASK));
99 result[BYTE_1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT);
100 result[BYTE_2] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT);
101 result[BYTE_3] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT);
Torsten Curdtca165392008-07-10 10:17:44 +0000102 return result;
103 }
104
105 /**
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000106 * Helper method to get the value as a Java long from four bytes starting at given array offset
107 * @param bytes the array of bytes
108 * @param offset the offset to start
109 * @return the correspondanding Java long value
Torsten Curdtca165392008-07-10 10:17:44 +0000110 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000111 public static long getValue(byte[] bytes, int offset) {
112 long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK;
113 value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK;
114 value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK;
115 value += (bytes[offset] & BYTE_MASK);
116 return value;
117 }
118
119 /**
120 * Helper method to get the value as a Java long from a four-byte array
121 * @param bytes the array of bytes
122 * @return the correspondanding Java long value
123 */
124 public static long getValue(byte[] bytes) {
125 return getValue(bytes, 0);
Torsten Curdtca165392008-07-10 10:17:44 +0000126 }
127
128 /**
129 * Override to make two instances with same value equal.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000130 * @param o an object to compare
131 * @return true if the objects are equal
Torsten Curdtca165392008-07-10 10:17:44 +0000132 * @since 1.1
133 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000134 public boolean equals(Object o) {
135 if (o == null || !(o instanceof ZipLong)) {
Torsten Curdtca165392008-07-10 10:17:44 +0000136 return false;
137 }
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000138 return value == ((ZipLong) o).getValue();
Torsten Curdtca165392008-07-10 10:17:44 +0000139 }
140
141 /**
142 * Override to make two instances with same value equal.
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000143 * @return the value stored in the ZipLong
Torsten Curdtca165392008-07-10 10:17:44 +0000144 * @since 1.1
145 */
Torsten Curdtab9ebfc2009-01-12 11:15:34 +0000146 public int hashCode() {
147 return (int) value;
Torsten Curdtca165392008-07-10 10:17:44 +0000148 }
149}