View Javadoc

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 cg@cdegroot.com.
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  /**
51   * Miscelaneous conversion utility methods.
52   *
53   * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
54   * @version $Id: Conversion.java,v 1.3 2002/05/31 06:33:20 boisvert Exp $
55   */
56  public class Conversion
57  {
58  
59      /**
60       * Convert a string into a byte array.
61       */
62      public static byte[] convertToByteArray( String s )
63      {
64          try {
65              // see the following page for character encoding
66              // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
67              return s.getBytes( "UTF8" );
68          } catch ( java.io.UnsupportedEncodingException uee ) {
69              uee.printStackTrace();
70              throw new Error( "Platform doesn't support UTF8 encoding" );
71          }
72      }
73  
74  
75      /**
76       * Convert a byte into a byte array.
77       */
78      public static byte[] convertToByteArray( byte n )
79      {
80          n = (byte)( n ^ ( (byte) 0x80 ) ); // flip MSB because "byte" is signed
81          return new byte[] { n };
82      }
83  
84  
85      /**
86       * Convert a short into a byte array.
87       */
88      public static byte[] convertToByteArray( short n )
89      {
90          n = (short) ( n ^ ( (short) 0x8000 ) ); // flip MSB because "short" is signed
91          byte[] key = new byte[ 2 ];
92          pack2( key, 0, n );
93          return key;
94      }
95  
96  
97      /**
98       * Convert an int into a byte array.
99       */
100     public static byte[] convertToByteArray( int n )
101     {
102         n = (n ^ 0x80000000); // flip MSB because "int" is signed
103         byte[] key = new byte[4];
104         pack4(key, 0, n);
105         return key;
106     }
107 
108 
109     /**
110      * Convert a long into a byte array.
111      */
112     public static byte[] convertToByteArray( long n )
113     {
114         n = (n ^ 0x8000000000000000L); // flip MSB because "long" is signed
115         byte[] key = new byte[8];
116         pack8( key, 0, n );
117         return key;
118     }
119 
120 
121     /**
122      * Convert a byte array (encoded as UTF-8) into a String
123      */
124     public static String convertToString( byte[] buf )
125     {
126         try {
127             // see the following page for character encoding
128             // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
129             return new String( buf, "UTF8" );
130         } catch ( java.io.UnsupportedEncodingException uee ) {
131             uee.printStackTrace();
132             throw new Error( "Platform doesn't support UTF8 encoding" );
133         }
134     }
135 
136 
137     /**
138      * Convert a byte array into an integer (signed 32-bit) value.
139      */
140     public static int convertToInt( byte[] buf )
141     {
142         int value = unpack4( buf, 0 );
143         value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed
144         return value;
145     }
146 
147 
148     /**
149      * Convert a byte array into a long (signed 64-bit) value.
150      */
151     public static long convertToLong( byte[] buf )
152     {
153         long value = ( (long) unpack4( buf, 0 ) << 32  )
154                      + ( unpack4( buf, 4 ) & 0xFFFFFFFFL );
155         value = ( value ^ 0x8000000000000000L ); // flip MSB because "long" is signed
156         return value;
157     }
158 
159 
160 
161 
162     static int unpack4( byte[] buf, int offset )
163     {
164         int value = ( buf[ offset ] << 24 )
165             | ( ( buf[ offset+1 ] << 16 ) & 0x00FF0000 )
166             | ( ( buf[ offset+2 ] << 8 ) & 0x0000FF00 )
167             | ( ( buf[ offset+3 ] << 0 ) & 0x000000FF );
168 
169         return value;
170     }
171 
172 
173     static final void pack2( byte[] data, int offs, int val )
174     {
175         data[offs++] = (byte) ( val >> 8 );
176         data[offs++] = (byte) val;
177     }
178 
179 
180     static final void pack4( byte[] data, int offs, int val )
181     {
182         data[offs++] = (byte) ( val >> 24 );
183         data[offs++] = (byte) ( val >> 16 );
184         data[offs++] = (byte) ( val >> 8 );
185         data[offs++] = (byte) val;
186     }
187 
188 
189     static final void pack8( byte[] data, int offs, long val )
190     {
191         pack4( data, 0, (int) ( val >> 32 ) );
192         pack4( data, 4, (int) val );
193     }
194 
195 
196     /**
197      * Test static methods
198      */
199     public static void main( String[] args )
200     {
201         byte[] buf;
202 
203         buf = convertToByteArray( (int) 5 );
204         System.out.println( "int value of 5 is: " + convertToInt( buf ) );
205 
206         buf = convertToByteArray( (int) -1 );
207         System.out.println( "int value of -1 is: " + convertToInt( buf ) );
208 
209         buf = convertToByteArray( (int) 22111000 );
210         System.out.println( "int value of 22111000 is: " + convertToInt( buf ) );
211 
212 
213         buf = convertToByteArray( (long) 5L );
214         System.out.println( "long value of 5 is: " + convertToLong( buf ) );
215 
216         buf = convertToByteArray( (long) -1L );
217         System.out.println( "long value of -1 is: " + convertToLong( buf ) );
218 
219         buf = convertToByteArray( (long) 1112223334445556667L );
220         System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) );
221     }
222 
223 }