Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 1 | /* |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 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 |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 8 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 10 | * |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 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 | * |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 17 | */ |
| 18 | package org.apache.commons.compress.archivers.zip; |
| 19 | |
| 20 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 21 | * Utility class that represents a two byte integer with conversion |
| 22 | * rules for the big endian byte order of ZIP files. |
| 23 | * |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 24 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 25 | public final class ZipShort implements Cloneable { |
| 26 | private static final int BYTE_MASK = 0xFF; |
| 27 | private static final int BYTE_1_MASK = 0xFF00; |
| 28 | private static final int BYTE_1_SHIFT = 8; |
| 29 | |
| 30 | private int value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 31 | |
| 32 | /** |
| 33 | * Create instance from a number. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 34 | * @param value the int to store as a ZipShort |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 35 | * @since 1.1 |
| 36 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 37 | public ZipShort (int value) { |
| 38 | this.value = value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Create instance from bytes. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 43 | * @param bytes the bytes to store as a ZipShort |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 44 | * @since 1.1 |
| 45 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 46 | public ZipShort (byte[] bytes) { |
| 47 | this(bytes, 0); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Create instance from the two bytes starting at offset. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 52 | * @param bytes the bytes to store as a ZipShort |
| 53 | * @param offset the offset to start |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 54 | * @since 1.1 |
| 55 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 56 | public ZipShort (byte[] bytes, int offset) { |
| 57 | value = ZipShort.getValue(bytes, offset); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Get value as two bytes in big endian byte order. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 62 | * @return the value as a a two byte array in big endian byte order |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 63 | * @since 1.1 |
| 64 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 65 | public byte[] getBytes() { |
| 66 | byte[] result = new byte[2]; |
| 67 | result[0] = (byte) (value & BYTE_MASK); |
| 68 | result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 69 | return result; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get value as Java int. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 74 | * @return value as a Java int |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 75 | * @since 1.1 |
| 76 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 77 | public int getValue() { |
| 78 | return value; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Get value as two bytes in big endian byte order. |
| 83 | * @param value the Java int to convert to bytes |
| 84 | * @return the converted int as a byte array in big endian byte order |
| 85 | */ |
| 86 | public static byte[] getBytes(int value) { |
| 87 | byte[] result = new byte[2]; |
| 88 | result[0] = (byte) (value & BYTE_MASK); |
| 89 | result[1] = (byte) ((value & BYTE_1_MASK) >> BYTE_1_SHIFT); |
| 90 | return result; |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Helper method to get the value as a java int from two bytes starting at given array offset |
| 95 | * @param bytes the array of bytes |
| 96 | * @param offset the offset to start |
| 97 | * @return the correspondanding java int value |
| 98 | */ |
| 99 | public static int getValue(byte[] bytes, int offset) { |
| 100 | int value = (bytes[offset + 1] << BYTE_1_SHIFT) & BYTE_1_MASK; |
| 101 | value += (bytes[offset] & BYTE_MASK); |
| 102 | return value; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Helper method to get the value as a java int from a two-byte array |
| 107 | * @param bytes the array of bytes |
| 108 | * @return the correspondanding java int value |
| 109 | */ |
| 110 | public static int getValue(byte[] bytes) { |
| 111 | return getValue(bytes, 0); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Override to make two instances with same value equal. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 116 | * @param o an object to compare |
| 117 | * @return true if the objects are equal |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 118 | * @since 1.1 |
| 119 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 120 | public boolean equals(Object o) { |
| 121 | if (o == null || !(o instanceof ZipShort)) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 122 | return false; |
| 123 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 124 | return value == ((ZipShort) o).getValue(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Override to make two instances with same value equal. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 129 | * @return the value stored in the ZipShort |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 130 | * @since 1.1 |
| 131 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 132 | public int hashCode() { |
| 133 | return value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 134 | } |
| 135 | } |