| 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * https://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.commons.compress.archivers.zip; |
| 20 | |
| 21 | import java.io.Serializable; |
| 22 | import java.math.BigInteger; |
| 23 | import java.nio.ByteBuffer; |
| 24 | import java.nio.ByteOrder; |
| 25 | |
| 26 | /** |
| 27 | * Utility class that represents an eight byte integer with conversion rules for the little-endian byte order of ZIP files. |
| 28 | * |
| 29 | * @Immutable |
| 30 | * @since 1.2 |
| 31 | */ |
| 32 | public final class ZipEightByteInteger implements Serializable { |
| 33 | |
| 34 | /** |
| 35 | * The number of bytes used to represent an instance in binary form. |
| 36 | */ |
| 37 | static final int BYTES = 8; |
| 38 | |
| 39 | private static final long serialVersionUID = 1L; |
| 40 | |
| 41 | /** |
| 42 | * Constant for a value of zero. |
| 43 | */ |
| 44 | public static final ZipEightByteInteger ZERO = new ZipEightByteInteger(0); |
| 45 | |
| 46 | private static final BigInteger HIGHEST_BIT = BigInteger.ONE.shiftLeft(63); |
| 47 | |
| 48 | /** |
| 49 | * Gets value as eight bytes in big-endian byte order. |
| 50 | * |
| 51 | * @param value the value to convert. |
| 52 | * @return value as eight bytes in big-endian byte order. |
| 53 | */ |
| 54 | public static byte[] getBytes(final BigInteger value) { |
| 55 | return getBytes(value.longValue()); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Gets value as eight bytes in big-endian byte order. |
| 60 | * |
| 61 | * @param value the value to convert. |
| 62 | * @return value as eight bytes in big-endian byte order. |
| 63 | */ |
| 64 | public static byte[] getBytes(final long value) { |
| 65 | return ByteBuffer.allocate(BYTES).order(ByteOrder.LITTLE_ENDIAN).putLong(value).array(); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Gets the value as a Java long from an eight-byte array. |
| 70 | * |
| 71 | * @param bytes the array of bytes. |
| 72 | * @return the corresponding Java long value. |
| 73 | */ |
| 74 | public static long getLongValue(final byte[] bytes) { |
| 75 | return getLongValue(bytes, 0); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the value as a Java long from eight bytes starting at given array offset. |
| 80 | * |
| 81 | * @param bytes the array of bytes. |
| 82 | * @param offset the offset to start. |
| 83 | * @return the corresponding Java long value. |
| 84 | */ |
| 85 | public static long getLongValue(final byte[] bytes, final int offset) { |
| 86 | return ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN).getLong(offset); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Gets the value as a Java long from an eight-byte array. |
| 91 | * |
| 92 | * @param bytes the array of bytes. |
| 93 | * @return the corresponding Java BigInteger value. |
| 94 | */ |
| 95 | public static BigInteger getValue(final byte[] bytes) { |
| 96 | return getValue(bytes, 0); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Gets the value as a Java BigInteger from eight bytes starting at given array offset. |
| 101 | * |
| 102 | * @param bytes the array of bytes. |
| 103 | * @param offset the offset to start. |
| 104 | * @return the corresponding Java BigInteger value. |
| 105 | */ |
| 106 | public static BigInteger getValue(final byte[] bytes, final int offset) { |
| 107 | return toUnsignedBigInteger(getLongValue(bytes, offset)); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * package private for tests only. |
| 112 | */ |
| 113 | static BigInteger toUnsignedBigInteger(final long value) { |
| 114 | if (value >= 0L) { |
| 115 | return BigInteger.valueOf(value); |
| 116 | } |
| 117 | return BigInteger.valueOf(value & Long.MAX_VALUE).add(HIGHEST_BIT); |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * The value is treated as unsigned. |
| 122 | */ |
| 123 | private final long value; |
| 124 | |
| 125 | /** |
| 126 | * Constructs a new instance from a number. |
| 127 | * |
| 128 | * @param value the BigInteger to store as a ZipEightByteInteger |
| 129 | */ |
| 130 | public ZipEightByteInteger(final BigInteger value) { |
| 131 | this.value = value.longValue(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Constructs a new instance from bytes. |
| 136 | * |
| 137 | * @param bytes the bytes to store as a ZipEightByteInteger. |
| 138 | */ |
| 139 | public ZipEightByteInteger(final byte[] bytes) { |
| 140 | this(bytes, 0); |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Constructs a new instance from the eight bytes starting at offset. |
| 145 | * |
| 146 | * @param bytes the bytes to store as a ZipEightByteInteger. |
| 147 | * @param offset the offset to start. |
| 148 | */ |
| 149 | public ZipEightByteInteger(final byte[] bytes, final int offset) { |
| 150 | this.value = getLongValue(bytes, offset); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Constructs a new instance from a number. |
| 155 | * |
| 156 | * @param value the long to store as a ZipEightByteInteger. |
| 157 | */ |
| 158 | public ZipEightByteInteger(final long value) { |
| 159 | this.value = value; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Override to make two instances with same value equal. |
| 164 | * |
| 165 | * @param o an object to compare. |
| 166 | * @return true if the objects are equal. |
| 167 | */ |
| 168 | @Override |
| 169 | public boolean equals(final Object o) { |
| 170 | if (!(o instanceof ZipEightByteInteger)) { |
| 171 | return false; |
| 172 | } |
| 173 | return value == ((ZipEightByteInteger) o).value; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Gets value as eight bytes in big-endian byte order. |
| 178 | * |
| 179 | * @return value as eight bytes in big-endian order. |
| 180 | */ |
| 181 | public byte[] getBytes() { |
| 182 | return getBytes(value); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Gets value as Java long. |
| 187 | * |
| 188 | * @return value as a long. |
| 189 | */ |
| 190 | public long getLongValue() { |
| 191 | return value; |
| 192 | } |
| 193 | |
| 194 | /** |
| 195 | * Gets value as Java BigInteger. |
| 196 | * |
| 197 | * @return value as a BigInteger. |
| 198 | */ |
| 199 | public BigInteger getValue() { |
| 200 | return toUnsignedBigInteger(value); |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Override to make two instances with same value equal. |
| 205 | * |
| 206 | * @return the hash code of the value stored in the ZipEightByteInteger. |
| 207 | */ |
| 208 | @Override |
| 209 | public int hashCode() { |
| 210 | return Long.hashCode(value); |
| 211 | } |
| 212 | |
| 213 | @Override |
| 214 | public String toString() { |
| 215 | return Long.toUnsignedString(value); |
| 216 | } |
| 217 | } |