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 four 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 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 Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 44 | |
| 45 | /** |
| 46 | * Create instance from a number. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 47 | * @param value the long to store as a ZipLong |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 48 | * @since 1.1 |
| 49 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 50 | public ZipLong(long value) { |
| 51 | this.value = value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create instance from bytes. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 56 | * @param bytes the bytes to store as a ZipLong |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 57 | * @since 1.1 |
| 58 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 59 | public ZipLong (byte[] bytes) { |
| 60 | this(bytes, 0); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Create instance from the four bytes starting at offset. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 65 | * @param bytes the bytes to store as a ZipLong |
| 66 | * @param offset the offset to start |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 67 | * @since 1.1 |
| 68 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 69 | public ZipLong (byte[] bytes, int offset) { |
| 70 | value = ZipLong.getValue(bytes, offset); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 74 | * Get value as four bytes in big endian byte order. |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 75 | * @since 1.1 |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 76 | * @return value as four bytes in big endian order |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 77 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 78 | 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 Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 102 | return result; |
| 103 | } |
| 104 | |
| 105 | /** |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 106 | * 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 Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 110 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 111 | 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 Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Override to make two instances with same value equal. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 130 | * @param o an object to compare |
| 131 | * @return true if the objects are equal |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 132 | * @since 1.1 |
| 133 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 134 | public boolean equals(Object o) { |
| 135 | if (o == null || !(o instanceof ZipLong)) { |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 136 | return false; |
| 137 | } |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 138 | return value == ((ZipLong) o).getValue(); |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Override to make two instances with same value equal. |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 143 | * @return the value stored in the ZipLong |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 144 | * @since 1.1 |
| 145 | */ |
Torsten Curdt | ab9ebfc | 2009-01-12 11:15:34 +0000 | [diff] [blame^] | 146 | public int hashCode() { |
| 147 | return (int) value; |
Torsten Curdt | ca16539 | 2008-07-10 10:17:44 +0000 | [diff] [blame] | 148 | } |
| 149 | } |