| 1 |
/* |
| 2 |
* 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 |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* 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 |
* |
| 17 |
*/ |
| 18 |
|
| 19 |
package org.apache.tools.zip; |
| 20 |
|
| 21 |
/** |
| 22 |
* Utility class that represents a four byte integer with conversion |
| 23 |
* rules for the big endian byte order of ZIP files. |
| 24 |
* |
| 25 |
*/ |
| 26 |
public final class ZipLong implements Cloneable { |
| 27 |
|
| 28 |
private static final int WORD = 4; |
| 29 |
// private static final int BYTE_BIT_SIZE = 8; |
| 30 |
private static final int BYTE_MASK = 0xFF; |
| 31 |
|
| 32 |
private static final int BYTE_1 = 1; |
| 33 |
private static final int BYTE_1_MASK = 0xFF00; |
| 34 |
private static final int BYTE_1_SHIFT = 8; |
| 35 |
|
| 36 |
private static final int BYTE_2 = 2; |
| 37 |
private static final int BYTE_2_MASK = 0xFF0000; |
| 38 |
private static final int BYTE_2_SHIFT = 16; |
| 39 |
|
| 40 |
private static final int BYTE_3 = 3; |
| 41 |
private static final long BYTE_3_MASK = 0xFF000000L; |
| 42 |
private static final int BYTE_3_SHIFT = 24; |
| 43 |
|
| 44 |
private long value; |
| 45 |
|
| 46 |
/** |
| 47 |
* Create instance from a number. |
| 48 |
* @param value the long to store as a ZipLong |
| 49 |
* @since 1.1 |
| 50 |
*/ |
| 51 |
public ZipLong(long value) { |
| 52 |
this.value = value; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Create instance from bytes. |
| 57 |
* @param bytes the bytes to store as a ZipLong |
| 58 |
* @since 1.1 |
| 59 |
*/ |
| 60 |
public ZipLong (byte[] bytes) { |
| 61 |
this(bytes, 0); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Create instance from the four bytes starting at offset. |
| 66 |
* @param bytes the bytes to store as a ZipLong |
| 67 |
* @param offset the offset to start |
| 68 |
* @since 1.1 |
| 69 |
*/ |
| 70 |
public ZipLong (byte[] bytes, int offset) { |
| 71 |
value = ZipLong.getValue(bytes, offset); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get value as four bytes in big endian byte order. |
| 76 |
* @since 1.1 |
| 77 |
* @return value as four bytes in big endian order |
| 78 |
*/ |
| 79 |
public byte[] getBytes() { |
| 80 |
return ZipLong.getBytes(value); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Get value as Java long. |
| 85 |
* @since 1.1 |
| 86 |
* @return value as a long |
| 87 |
*/ |
| 88 |
public long getValue() { |
| 89 |
return value; |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Get value as four bytes in big endian byte order. |
| 94 |
* @param value the value to convert |
| 95 |
* @return value as four bytes in big endian byte order |
| 96 |
*/ |
| 97 |
public static byte[] getBytes(long value) { |
| 98 |
byte[] result = new byte[WORD]; |
| 99 |
result[0] = (byte) ((value & BYTE_MASK)); |
| 100 |
result[BYTE_1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT); |
| 101 |
result[BYTE_2] = (byte) ((value & BYTE_2_MASK) >> BYTE_2_SHIFT); |
| 102 |
result[BYTE_3] = (byte) ((value & BYTE_3_MASK) >> BYTE_3_SHIFT); |
| 103 |
return result; |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Helper method to get the value as a Java long from four bytes starting at given array offset |
| 108 |
* @param bytes the array of bytes |
| 109 |
* @param offset the offset to start |
| 110 |
* @return the correspondanding Java long value |
| 111 |
*/ |
| 112 |
public static long getValue(byte[] bytes, int offset) { |
| 113 |
long value = (bytes[offset + BYTE_3] << BYTE_3_SHIFT) & BYTE_3_MASK; |
| 114 |
value += (bytes[offset + BYTE_2] << BYTE_2_SHIFT) & BYTE_2_MASK; |
| 115 |
value += (bytes[offset + BYTE_1] << BYTE_1_SHIFT) & BYTE_1_MASK; |
| 116 |
value += (bytes[offset] & BYTE_MASK); |
| 117 |
return value; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Helper method to get the value as a Java long from a four-byte array |
| 122 |
* @param bytes the array of bytes |
| 123 |
* @return the correspondanding Java long value |
| 124 |
*/ |
| 125 |
public static long getValue(byte[] bytes) { |
| 126 |
return getValue(bytes, 0); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Override to make two instances with same value equal. |
| 131 |
* @param o an object to compare |
| 132 |
* @return true if the objects are equal |
| 133 |
* @since 1.1 |
| 134 |
*/ |
| 135 |
public boolean equals(Object o) { |
| 136 |
if (o == null || !(o instanceof ZipLong)) { |
| 137 |
return false; |
| 138 |
} |
| 139 |
return value == ((ZipLong) o).getValue(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Override to make two instances with same value equal. |
| 144 |
* @return the value stored in the ZipLong |
| 145 |
* @since 1.1 |
| 146 |
*/ |
| 147 |
public int hashCode() { |
| 148 |
return (int) value; |
| 149 |
} |
| 150 |
|
| 151 |
public Object clone() { |
| 152 |
try { |
| 153 |
return super.clone(); |
| 154 |
} catch (CloneNotSupportedException cnfe) { |
| 155 |
// impossible |
| 156 |
throw new RuntimeException(cnfe); |
| 157 |
} |
| 158 |
} |
| 159 |
} |