KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > groboutils > codecoverage > v2 > ant > zip > ZipLong


1 /*
2  * Copyright 2001-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package net.sourceforge.groboutils.codecoverage.v2.ant.zip;
19
20 /**
21  * Utility class that represents a four byte integer with conversion
22  * rules for the big endian byte order of ZIP files.
23  *
24  * @author Stefan Bodewig
25  * @version $Revision: 1.1 $
26  */

27 public final class ZipLong implements Cloneable JavaDoc {
28
29     private long value;
30
31     /**
32      * Create instance from a number.
33      *
34      * @since 1.1
35      */

36     public ZipLong(long value) {
37         this.value = value;
38     }
39
40     /**
41      * Create instance from bytes.
42      *
43      * @since 1.1
44      */

45     public ZipLong (byte[] bytes) {
46         this(bytes, 0);
47     }
48
49     /**
50      * Create instance from the four bytes starting at offset.
51      *
52      * @since 1.1
53      */

54     public ZipLong (byte[] bytes, int offset) {
55         value = (bytes[offset + 3] << 24) & 0xFF000000L;
56         value += (bytes[offset + 2] << 16) & 0xFF0000;
57         value += (bytes[offset + 1] << 8) & 0xFF00;
58         value += (bytes[offset] & 0xFF);
59     }
60
61     /**
62      * Get value as two bytes in big endian byte order.
63      *
64      * @since 1.1
65      */

66     public byte[] getBytes() {
67         byte[] result = new byte[4];
68         result[0] = (byte) ((value & 0xFF));
69         result[1] = (byte) ((value & 0xFF00) >> 8);
70         result[2] = (byte) ((value & 0xFF0000) >> 16);
71         result[3] = (byte) ((value & 0xFF000000l) >> 24);
72         return result;
73     }
74
75     /**
76      * Get value as Java int.
77      *
78      * @since 1.1
79      */

80     public long getValue() {
81         return value;
82     }
83
84     /**
85      * Override to make two instances with same value equal.
86      *
87      * @since 1.1
88      */

89     public boolean equals(Object JavaDoc o) {
90         if (o == null || !(o instanceof ZipLong)) {
91             return false;
92         }
93         return value == ((ZipLong) o).getValue();
94     }
95
96     /**
97      * Override to make two instances with same value equal.
98      *
99      * @since 1.1
100      */

101     public int hashCode() {
102         return (int) value;
103     }
104 }
105
Popular Tags