Debian Codegraph @

jdbm1/src/main/java/jdbm/helper/Conversion.java

raw
1 /**
2 * JDBM LICENSE v1.00
3 *
4 * Redistribution and use of this software and associated documentation
5 * ("Software"), with or without modification, are permitted provided
6 * that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain copyright
9 * statements and notices. Redistributions must also contain a
10 * copy of this document.
11 *
12 * 2. Redistributions in binary form must reproduce the
13 * above copyright notice, this list of conditions and the
14 * following disclaimer in the documentation and/or other
15 * materials provided with the distribution.
16 *
17 * 3. The name "JDBM" must not be used to endorse or promote
18 * products derived from this Software without prior written
19 * permission of Cees de Groot. For written permission,
20 * please contact [email protected].
21 *
22 * 4. Products derived from this Software may not be called "JDBM"
23 * nor may "JDBM" appear in their names without prior written
24 * permission of Cees de Groot.
25 *
26 * 5. Due credit should be given to the JDBM Project
27 * (http://jdbm.sourceforge.net/).
28 *
29 * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
30 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
31 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
32 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
33 * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
34 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
35 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
37 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
38 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
40 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 *
42 * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
43 * Contributions are Copyright (C) 2001 by their associated contributors.
44 *
45 */
46
47 package jdbm.helper;
48
49
50 import jdbm.I18n;
51
52
53 /**
54 * Miscelaneous conversion utility methods.
55 *
56 * @author <a href="mailto:[email protected]">Alex Boisvert</a>
57 */
58 public class Conversion
59 {
60
61 /**
62 * Convert a string into a byte array.
63 */
64 public static byte[] convertToByteArray( String s )
65 {
66 try
67 {
68 // see the following page for character encoding
69 // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
70 return s.getBytes( "UTF8" );
71 }
72 catch ( java.io.UnsupportedEncodingException uee )
73 {
74 uee.printStackTrace();
75 throw new Error( I18n.err( I18n.ERR_527 ) );
76 }
77 }
78
79
80 /**
81 * Convert a byte into a byte array.
82 */
83 public static byte[] convertToByteArray( byte n )
84 {
85 n = ( byte ) ( n ^ ( ( byte ) 0x80 ) ); // flip MSB because "byte" is signed
86 return new byte[]
87 { n };
88 }
89
90
91 /**
92 * Convert a short into a byte array.
93 */
94 public static byte[] convertToByteArray( short n )
95 {
96 n = ( short ) ( n ^ ( ( short ) 0x8000 ) ); // flip MSB because "short" is signed
97 byte[] key = new byte[2];
98 pack2( key, 0, n );
99 return key;
100 }
101
102
103 /**
104 * Convert an int into a byte array.
105 */
106 public static byte[] convertToByteArray( int n )
107 {
108 n = ( n ^ 0x80000000 ); // flip MSB because "int" is signed
109 byte[] key = new byte[4];
110 pack4( key, 0, n );
111 return key;
112 }
113
114
115 /**
116 * Convert a long into a byte array.
117 */
118 public static byte[] convertToByteArray( long n )
119 {
120 n = ( n ^ 0x8000000000000000L ); // flip MSB because "long" is signed
121 byte[] key = new byte[8];
122 pack8( key, 0, n );
123 return key;
124 }
125
126
127 /**
128 * Convert a byte array (encoded as UTF-8) into a String
129 */
130 public static String convertToString( byte[] buf )
131 {
132 try
133 {
134 // see the following page for character encoding
135 // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
136 return new String( buf, "UTF8" );
137 }
138 catch ( java.io.UnsupportedEncodingException uee )
139 {
140 uee.printStackTrace();
141 throw new Error( I18n.err( I18n.ERR_527 ) );
142 }
143 }
144
145
146 /**
147 * Convert a byte array into an integer (signed 32-bit) value.
148 */
149 public static int convertToInt( byte[] buf )
150 {
151 int value = unpack4( buf, 0 );
152 value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed
153 return value;
154 }
155
156
157 /**
158 * Convert a byte array into a long (signed 64-bit) value.
159 */
160 public static long convertToLong( byte[] buf )
161 {
162 long value = ( ( long ) unpack4( buf, 0 ) << 32 )
163 + ( unpack4( buf, 4 ) & 0xFFFFFFFFL );
164 value = ( value ^ 0x8000000000000000L ); // flip MSB because "long" is signed
165 return value;
166 }
167
168
169 static int unpack4( byte[] buf, int offset )
170 {
171 int value = ( buf[offset] << 24 )
172 | ( ( buf[offset + 1] << 16 ) & 0x00FF0000 )
173 | ( ( buf[offset + 2] << 8 ) & 0x0000FF00 )
174 | ( ( buf[offset + 3] << 0 ) & 0x000000FF );
175
176 return value;
177 }
178
179
180 static final void pack2( byte[] data, int offs, int val )
181 {
182 data[offs++] = ( byte ) ( val >> 8 );
183 data[offs++] = ( byte ) val;
184 }
185
186
187 static final void pack4( byte[] data, int offs, int val )
188 {
189 data[offs++] = ( byte ) ( val >> 24 );
190 data[offs++] = ( byte ) ( val >> 16 );
191 data[offs++] = ( byte ) ( val >> 8 );
192 data[offs++] = ( byte ) val;
193 }
194
195
196 static final void pack8( byte[] data, int offs, long val )
197 {
198 pack4( data, 0, ( int ) ( val >> 32 ) );
199 pack4( data, 4, ( int ) val );
200 }
201
202
203 /**
204 * Test static methods
205 */
206 public static void main( String[] args )
207 {
208 byte[] buf;
209
210 buf = convertToByteArray( 5 );
211 System.out.println( "int value of 5 is: " + convertToInt( buf ) );
212
213 buf = convertToByteArray( -1 );
214 System.out.println( "int value of -1 is: " + convertToInt( buf ) );
215
216 buf = convertToByteArray( 22111000 );
217 System.out.println( "int value of 22111000 is: " + convertToInt( buf ) );
218
219 buf = convertToByteArray( 5L );
220 System.out.println( "long value of 5 is: " + convertToLong( buf ) );
221
222 buf = convertToByteArray( -1L );
223 System.out.println( "long value of -1 is: " + convertToLong( buf ) );
224
225 buf = convertToByteArray( 1112223334445556667L );
226 System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) );
227 }
228
229 }