001    /**
002     * JDBM LICENSE v1.00
003     *
004     * Redistribution and use of this software and associated documentation
005     * ("Software"), with or without modification, are permitted provided
006     * that the following conditions are met:
007     *
008     * 1. Redistributions of source code must retain copyright
009     *    statements and notices.  Redistributions must also contain a
010     *    copy of this document.
011     *
012     * 2. Redistributions in binary form must reproduce the
013     *    above copyright notice, this list of conditions and the
014     *    following disclaimer in the documentation and/or other
015     *    materials provided with the distribution.
016     *
017     * 3. The name "JDBM" must not be used to endorse or promote
018     *    products derived from this Software without prior written
019     *    permission of Cees de Groot.  For written permission,
020     *    please contact cg@cdegroot.com.
021     *
022     * 4. Products derived from this Software may not be called "JDBM"
023     *    nor may "JDBM" appear in their names without prior written
024     *    permission of Cees de Groot.
025     *
026     * 5. Due credit should be given to the JDBM Project
027     *    (http://jdbm.sourceforge.net/).
028     *
029     * THIS SOFTWARE IS PROVIDED BY THE JDBM PROJECT AND CONTRIBUTORS
030     * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
031     * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
032     * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
033     * CEES DE GROOT OR ANY CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
034     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
035     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
036     * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
037     * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
038     * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039     * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
040     * OF THE POSSIBILITY OF SUCH DAMAGE.
041     *
042     * Copyright 2001 (C) Alex Boisvert. All Rights Reserved.
043     * Contributions are Copyright (C) 2001 by their associated contributors.
044     *
045     */
046    
047    package jdbm.helper;
048    
049    
050    /**
051     * Miscelaneous conversion utility methods.
052     *
053     * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
054     * @version $Id: Conversion.java,v 1.3 2002/05/31 06:33:20 boisvert Exp $
055     */
056    public class Conversion
057    {
058    
059        /**
060         * Convert a string into a byte array.
061         */
062        public static byte[] convertToByteArray( String s )
063        {
064            try {
065                // see the following page for character encoding
066                // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
067                return s.getBytes( "UTF8" );
068            } catch ( java.io.UnsupportedEncodingException uee ) {
069                uee.printStackTrace();
070                throw new Error( "Platform doesn't support UTF8 encoding" );
071            }
072        }
073    
074    
075        /**
076         * Convert a byte into a byte array.
077         */
078        public static byte[] convertToByteArray( byte n )
079        {
080            n = (byte)( n ^ ( (byte) 0x80 ) ); // flip MSB because "byte" is signed
081            return new byte[] { n };
082        }
083    
084    
085        /**
086         * Convert a short into a byte array.
087         */
088        public static byte[] convertToByteArray( short n )
089        {
090            n = (short) ( n ^ ( (short) 0x8000 ) ); // flip MSB because "short" is signed
091            byte[] key = new byte[ 2 ];
092            pack2( key, 0, n );
093            return key;
094        }
095    
096    
097        /**
098         * Convert an int into a byte array.
099         */
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    }