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