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