KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > persistence > btreeimpl > btreestorage > Converter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.mdr.persistence.btreeimpl.btreestorage;
20
21 import java.io.*;
22
23 /**
24 * This class exists only to provide data conversions between primitive
25 * types and byte arrays.
26 */

27 public class Converter {
28
29     /* We encode strings to UTF-8 */
30
31     private static final String JavaDoc ENCODING = "UTF-8";
32
33     /** write a byte to an array
34     * @param array array to write to
35     * @param offset offset in array at which to write byte. On return, this
36     * will point to the next byte in the array
37     * @param data byte to write to array
38     */

39     public static final void writeByte(
40             byte array[], IntHolder offset, byte data){
41         offset.setValue(writeByte(array, offset.getValue(), data));
42     }
43
44     /** write a byte to an array
45     * @param array array to write to
46     * @param offset offset in array at which to write byte
47     * @param data byte to write to array
48     * @return offset in array which follows the written byte
49     */

50     public static final int writeByte(byte array[], int offset, byte data) {
51         array[offset++] = data;
52         return offset;
53     }
54
55     /** write a short to an array
56     * @param array array to write to
57     * @param offset offset in array at which to write short. On return, this
58     * will point to the next byte in the array
59     * @param data short to write to array
60     */

61     public static final void writeShort(
62             byte array[], IntHolder offset, short data){
63         offset.setValue(writeShort(array, offset.getValue(), data));
64     }
65
66     /** write a short to an array
67     * @param array array to write to
68     * @param offset offset in array at which to write short
69     * @param data short to write to array
70     * @return offset in array which follows the written short
71     */

72     public static final int writeShort(byte array[], int offset, short data) {
73         array[offset++] = (byte)(data >> 8);
74         array[offset++] = (byte)data;
75         return offset;
76     }
77
78     /** write an int to an array
79     * @param array array to write to
80     * @param offset offset in array at which to write int. On return, this
81     * will point to the next byte in the array
82     * @param data int to write to array
83     */

84     public static final void writeInt(
85             byte array[], IntHolder offset, int data){
86         offset.setValue(writeInt(array, offset.getValue(), data));
87     }
88
89     /** write a int to an array
90     * @param array array to write to
91     * @param offset offset in array at which to write int
92     * @param data int to write to array
93     * @return offset in array which follows the written int
94     */

95     public static final int writeInt(byte array[], int offset, int data) {
96         array[offset++] = (byte)(data >> 24);
97         array[offset++] = (byte)(data >> 16);
98         array[offset++] = (byte)(data >> 8);
99         array[offset++] = (byte)data;
100         return offset;
101     }
102
103     /** write a long to an array
104     * @param array array to write to
105     * @param offset offset in array at which to write long. On return, this
106     * will point to the next byte in the array
107     * @param data long to write to array
108     */

109     public static final void writeLong(
110             byte array[], IntHolder offset, long data){
111         offset.setValue(writeLong(array, offset.getValue(), data));
112     }
113
114     /** write a long to an array
115     * @param array array to write to
116     * @param offset offset in array at which to write long
117     * @param data long to write to array
118     * @return offset in array which follows the written long
119     */

120     public static final int writeLong(byte array[], int offset, long data) {
121         array[offset++] = (byte)(data >>> 56);
122         array[offset++] = (byte)(data >>> 48);
123         array[offset++] = (byte)(data >>> 40);
124         array[offset++] = (byte)(data >>> 32);
125         array[offset++] = (byte)(data >>> 24);
126         array[offset++] = (byte)(data >>> 16);
127         array[offset++] = (byte)(data >>> 8);
128         array[offset++] = (byte)data;
129         return offset;
130     }
131
132     /** write a string to an array. This uses the same format as
133     * DataOutput.WriteUTF, that is, after converting the String to an array
134     * of bytes in the Java UTF-8 encoding, write two bytes containing the
135     * array length followed by the array itself.
136     * @param array array to write to
137     * @param offset offset in array at which to write string. On return, this
138     * will point to the next byte in the array
139     * @param data string to write to array
140     */

141     public static final void writeString(
142             byte array[], IntHolder offset, String JavaDoc data){
143         offset.setValue(writeString(array, offset.getValue(), data));
144     }
145
146     /** write a string to an array. This uses the same format as
147     * DataOutput.WriteUTF, that is, after converting the String to an array
148     * of bytes in the Java UTF-8 encoding, write two bytes containing the
149     * array length followed by the array itself.
150     * @param array array to write to
151     * @param offset offset in array at which to write string
152     * @param data string to write to array
153     * @return offset in array which follows the written string
154     */

155     public static final int writeString(byte array[], int offset, String JavaDoc data) {
156         byte enc[] = convertStringToUTF8(data);
157         offset = writeShort(array, offset, (short)enc.length);
158         if (enc.length > 0)
159             System.arraycopy(enc, 0, array, offset, enc.length);
160         return offset + enc.length;
161     }
162         
163     /** convert a String to a byte array
164     * @param str string to convert
165     * @return string in UTF-8 format
166     */

167     public static final byte[] convertStringToUTF8(String JavaDoc str) {
168         byte result[] = null;
169         try {
170             result = str.getBytes(ENCODING);
171         } catch (UnsupportedEncodingException ex) {
172             /* UTF-8 should always be supported */
173             throw new RuntimeException JavaDoc("UTF-8 not supported!");
174         }
175         return result;
176     }
177
178     /** read a byte from an array
179     * @param array array to read from
180     * @param offset offset in array from which to read byte. After returning,
181     * this points to the offset in the array past the data read.
182     * @return data read from array
183     */

184     public static final byte readByte(byte array[], IntHolder offset) {
185         int offst = offset.getValue();
186         byte data = readByte(array, offst);
187         offset.setValue(offst+1);
188         return data;
189     }
190
191     /** read a byte from an array
192     * @param array array to read from
193     * @param offset offset in array from which to read byte.
194     * @return data read from array
195     */

196     public static final byte readByte(byte array[], int offst) {
197         byte data = array[offst];
198         return data;
199     }
200
201     /** read a short from an array
202     * @param array array to read from
203     * @param offset offset in array from which to read short. After returning,
204     * this points to the offset in the array past the data read.
205     * @return data read from array
206     */

207     public static final short readShort(byte array[], IntHolder offset) {
208         int offst = offset.getValue();
209         short data = readShort(array, offst);
210         offset.setValue(offst+2);
211         return data;
212     }
213
214     /** read a short from an array
215     * @param array array to read from
216     * @param offset offset in array from which to read short.
217     * @return data read from array
218     */

219     public static final short readShort(byte array[], int offst) {
220         short data = (short)(((short)array[offst++]) & 0xFF);
221         data <<= 8;
222         data |= (((short)array[offst]) & 0xFF);
223         return data;
224     }
225
226     /** read an int from an array
227     * @param array array to read from
228     * @param offset offset in array from which to read int. After returning,
229     * this points to the offset in the array past the data read.
230     * @return data read from array
231     */

232     public static final int readInt(byte array[], IntHolder offset) {
233         int offst = offset.getValue();
234         int data = readInt(array, offst);
235         offset.setValue(offst + 4);
236         return data;
237     }
238
239     /** read an int from an array
240     * @param array array to read from
241     * @param offset offset in array from which to read int.
242     * @return data read from array
243     */

244     public static final int readInt(byte array[], int offst) {
245         int data = (((int)array[offst++]) & 0xFF);
246         for (int i = 0; i < 3; i++) {
247             data <<= 8;
248             data |= (((int)array[offst++]) & 0xFF);
249         }
250         return data;
251     }
252
253     /** read a long from an array
254     * @param array array to read from
255     * @param offset offset in array from which to read long. After returning,
256     * this points to the offset in the array past the data read.
257     * @return data read from array
258     */

259     public static final long readLong(byte array[], IntHolder offset) {
260         int offst = offset.getValue();
261         long data = readLong(array, offst);
262         offset.setValue(offst + 8);
263         return data;
264     }
265
266     /** read a long from an array
267     * @param array array to read from
268     * @param offset offset in array from which to read long.
269     * @return data read from array
270     */

271     public static final long readLong(byte array[], int offst) {
272         long data = (((long)array[offst++]) & 0xFF);
273         for (int i = 0; i < 7; i++) {
274             data <<= 8;
275             data |= (((long)array[offst++]) & 0xFF);
276         }
277         return data;
278     }
279
280     /** read a string from an array. This will read a string written
281     * by WriteString.
282     * @param array array to read from
283     * @param offset offset in array from which to read string. After returning,
284     * this points to the offset in the array past the data read.
285     * @return string read from array
286     */

287     public static final String JavaDoc readString(byte array[], IntHolder offset) {
288         return readString(array, offset.getValue(), offset);
289     }
290
291     /** read a string from an array. This will read a string written
292     * by WriteString.
293     * @param array array to read from
294     * @param offset offset in array from which to read string
295     * @return string read from array
296     */

297     public static final String JavaDoc readString(byte array[], int offset) {
298         return readString(array, offset, null);
299     }
300
301     /** read a string from an array. This will read a string written
302     * by WriteString.
303     * @param array array to read from
304     * @param offset offset in array from which to read string
305     * @param newOffset if non-null, this will receive the offset
306     * following the string
307     * @return string read from array
308     */

309     private static String JavaDoc readString(
310                     byte array[], int offset, IntHolder newOffset) {
311         String JavaDoc str;
312
313         short arrSize = readShort(array, offset);
314         if (arrSize == 0) {
315             str = "";
316         }
317         else {
318             byte enc[] = new byte[arrSize];
319             System.arraycopy(array, offset+2, enc, 0, arrSize);
320             str = convertUTF8ToString(enc);
321         }
322         if (newOffset != null)
323             newOffset.setValue(offset + 2 + arrSize);
324         return str;
325     }
326
327     /** convert a byte array back to a string
328     * @param buffer array of bytes in UTF-8 format to convert
329     * @return string
330     */

331     public static final String JavaDoc convertUTF8ToString(byte buffer[]) {
332         String JavaDoc result = null;
333         try {
334         result = new String JavaDoc(buffer, ENCODING);
335         } catch (UnsupportedEncodingException ex) {
336             /* UTF-8 should always be supported */
337             throw new RuntimeException JavaDoc("UTF-8 not supported!");
338         }
339         return result;
340     }
341
342 }
343     
344
Popular Tags