KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > ClassID


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    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 package org.apache.poi.hpsf;
19
20 import org.apache.poi.util.HexDump;
21
22 /**
23  * <p>Represents a class ID (16 bytes). Unlike other little-endian
24  * type the {@link ClassID} is not just 16 bytes stored in the wrong
25  * order. Instead, it is a double word (4 bytes) followed by two
26  * words (2 bytes each) followed by 8 bytes.</p>
27  *
28  * @author Rainer Klute <a
29  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
30  * @version $Id: ClassID.java,v 1.14 2004/08/13 22:38:52 klute Exp $
31  * @since 2002-02-09
32  */

33 public class ClassID
34 {
35
36     /**
37      * <p>The bytes making out the class ID in correct order,
38      * i.e. big-endian.</p>
39      */

40     protected byte[] bytes;
41
42
43
44     /**
45      * <p>Creates a {@link ClassID} and reads its value from a byte
46      * array.</p>
47      *
48      * @param src The byte array to read from.
49      * @param offset The offset of the first byte to read.
50      */

51     public ClassID(final byte[] src, final int offset)
52     {
53         read(src, offset);
54     }
55
56
57     /**
58      * <p>Creates a {@link ClassID} and initializes its value with
59      * 0x00 bytes.</p>
60      */

61     public ClassID()
62     {
63         bytes = new byte[LENGTH];
64         for (int i = 0; i < LENGTH; i++)
65             bytes[i] = 0x00;
66     }
67
68
69
70     /** <p>The number of bytes occupied by this object in the byte
71      * stream.</p> */

72     public static final int LENGTH = 16;
73
74     /**
75      * @return The number of bytes occupied by this object in the byte
76      * stream.
77      */

78     public int length()
79     {
80         return LENGTH;
81     }
82
83
84
85     /**
86      * <p>Gets the bytes making out the class ID. They are returned in
87      * correct order, i.e. big-endian.</p>
88      *
89      * @return the bytes making out the class ID.
90      */

91     public byte[] getBytes()
92     {
93         return bytes;
94     }
95
96
97
98     /**
99      * <p>Sets the bytes making out the class ID.</p>
100      *
101      * @param bytes The bytes making out the class ID in big-endian format. They
102      * are copied without their order being changed.
103      */

104     public void setBytes(final byte[] bytes)
105     {
106         for (int i = 0; i < this.bytes.length; i++)
107             this.bytes[i] = bytes[i];
108     }
109
110
111
112     /**
113      * <p>Reads the class ID's value from a byte array by turning
114      * little-endian into big-endian.</p>
115      *
116      * @param src The byte array to read from
117      *
118      * @param offset The offset within the <var>src</var> byte array
119      *
120      * @return A byte array containing the class ID.
121      */

122     public byte[] read(final byte[] src, final int offset)
123     {
124         bytes = new byte[16];
125
126         /* Read double word. */
127         bytes[0] = src[3 + offset];
128         bytes[1] = src[2 + offset];
129         bytes[2] = src[1 + offset];
130         bytes[3] = src[0 + offset];
131
132         /* Read first word. */
133         bytes[4] = src[5 + offset];
134         bytes[5] = src[4 + offset];
135
136         /* Read second word. */
137         bytes[6] = src[7 + offset];
138         bytes[7] = src[6 + offset];
139
140         /* Read 8 bytes. */
141         for (int i = 8; i < 16; i++)
142             bytes[i] = src[i + offset];
143
144         return bytes;
145     }
146
147
148
149     /**
150      * <p>Writes the class ID to a byte array in the
151      * little-endian format.</p>
152      *
153      * @param dst The byte array to write to.
154      *
155      * @param offset The offset within the <var>dst</var> byte array.
156      *
157      * @exception ArrayStoreException if there is not enough room for the class
158      * ID 16 bytes in the byte array after the <var>offset</var> position.
159      */

160     public void write(final byte[] dst, final int offset)
161     throws ArrayStoreException JavaDoc
162     {
163         /* Check array size: */
164         if (dst.length < 16)
165             throw new ArrayStoreException JavaDoc
166                 ("Destination byte[] must have room for at least 16 bytes, " +
167                  "but has a length of only " + dst.length + ".");
168         /* Write double word. */
169         dst[0 + offset] = bytes[3];
170         dst[1 + offset] = bytes[2];
171         dst[2 + offset] = bytes[1];
172         dst[3 + offset] = bytes[0];
173
174         /* Write first word. */
175         dst[4 + offset] = bytes[5];
176         dst[5 + offset] = bytes[4];
177
178         /* Write second word. */
179         dst[6 + offset] = bytes[7];
180         dst[7 + offset] = bytes[6];
181
182         /* Write 8 bytes. */
183         for (int i = 8; i < 16; i++)
184             dst[i + offset] = bytes[i];
185     }
186
187
188
189     /**
190      * <p>Checks whether this <code>ClassID</code> is equal to another
191      * object.</p>
192      *
193      * @param o the object to compare this <code>PropertySet</code> with
194      * @return <code>true</code> if the objects are equal, else
195      * <code>false</code>.</p>
196      */

197     public boolean equals(final Object JavaDoc o)
198     {
199         if (o == null || !(o instanceof ClassID))
200             return false;
201         final ClassID cid = (ClassID) o;
202         if (bytes.length != cid.bytes.length)
203             return false;
204         for (int i = 0; i < bytes.length; i++)
205             if (bytes[i] != cid.bytes[i])
206                 return false;
207         return true;
208     }
209
210
211
212     /**
213      * @see Object#hashCode()
214      */

215     public int hashCode()
216     {
217         return new String JavaDoc(bytes).hashCode();
218     }
219
220
221
222     /**
223      * <p>Returns a human-readable representation of the Class ID in standard
224      * format <code>"{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"</code>.</p>
225      *
226      * @return String representation of the Class ID represented by this object.
227      */

228     public String JavaDoc toString()
229     {
230         StringBuffer JavaDoc sbClassId = new StringBuffer JavaDoc(38);
231         sbClassId.append('{');
232         for (int i = 0; i < 16; i++)
233         {
234             sbClassId.append(HexDump.toHex(bytes[i]));
235             if (i == 3 || i == 5 || i == 7 || i == 9)
236                 sbClassId.append('-');
237         }
238         sbClassId.append('}');
239         return sbClassId.toString();
240     }
241
242 }
243
Popular Tags