KickJava   Java API By Example, From Geeks To Geeks.

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


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 two 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 ZipShort implements Cloneable JavaDoc {
28
29     private int value;
30
31     /**
32      * Create instance from a number.
33      *
34      * @since 1.1
35      */

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

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

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

64     public byte[] getBytes() {
65         byte[] result = new byte[2];
66         result[0] = (byte) (value & 0xFF);
67         result[1] = (byte) ((value & 0xFF00) >> 8);
68         return result;
69     }
70
71     /**
72      * Get value as Java int.
73      *
74      * @since 1.1
75      */

76     public int getValue() {
77         return value;
78     }
79
80     /**
81      * Override to make two instances with same value equal.
82      *
83      * @since 1.1
84      */

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

97     public int hashCode() {
98         return value;
99     }
100 }
101
Popular Tags