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    import org.apache.directory.server.i18n.I18n;
050    
051    
052    /**
053     * Miscelaneous conversion utility methods.
054     *
055     * @author <a href="mailto:boisvert@intalio.com">Alex Boisvert</a>
056     * @version $Id: Conversion.java,v 1.3 2002/05/31 06:33:20 boisvert Exp $
057     */
058    public class Conversion
059    {
060    
061        /**
062         * Convert a string into a byte array.
063         */
064        public static byte[] convertToByteArray( String s )
065        {
066            try {
067                // see the following page for character encoding
068                // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
069                return s.getBytes( "UTF8" );
070            } catch ( java.io.UnsupportedEncodingException uee ) {
071                uee.printStackTrace();
072                throw new Error( I18n.err( I18n.ERR_527 ) );
073            }
074        }
075    
076    
077        /**
078         * Convert a byte into a byte array.
079         */
080        public static byte[] convertToByteArray( byte n )
081        {
082            n = (byte)( n ^ ( (byte) 0x80 ) ); // flip MSB because "byte" is signed
083            return new byte[] { n };
084        }
085    
086    
087        /**
088         * Convert a short into a byte array.
089         */
090        public static byte[] convertToByteArray( short n )
091        {
092            n = (short) ( n ^ ( (short) 0x8000 ) ); // flip MSB because "short" is signed
093            byte[] key = new byte[ 2 ];
094            pack2( key, 0, n );
095            return key;
096        }
097    
098    
099        /**
100         * Convert an int into a byte array.
101         */
102        public static byte[] convertToByteArray( int n )
103        {
104            n = (n ^ 0x80000000); // flip MSB because "int" is signed
105            byte[] key = new byte[4];
106            pack4(key, 0, n);
107            return key;
108        }
109    
110    
111        /**
112         * Convert a long into a byte array.
113         */
114        public static byte[] convertToByteArray( long n )
115        {
116            n = (n ^ 0x8000000000000000L); // flip MSB because "long" is signed
117            byte[] key = new byte[8];
118            pack8( key, 0, n );
119            return key;
120        }
121    
122    
123        /**
124         * Convert a byte array (encoded as UTF-8) into a String
125         */
126        public static String convertToString( byte[] buf )
127        {
128            try {
129                // see the following page for character encoding
130                // http://java.sun.com/products/jdk/1.1/docs/guide/intl/encoding.doc.html
131                return new String( buf, "UTF8" );
132            } catch ( java.io.UnsupportedEncodingException uee ) {
133                uee.printStackTrace();
134                throw new Error( I18n.err( I18n.ERR_527 ) );
135            }
136        }
137    
138    
139        /**
140         * Convert a byte array into an integer (signed 32-bit) value.
141         */
142        public static int convertToInt( byte[] buf )
143        {
144            int value = unpack4( buf, 0 );
145            value = ( value ^ 0x80000000 ); // flip MSB because "int" is signed
146            return value;
147        }
148    
149    
150        /**
151         * Convert a byte array into a long (signed 64-bit) value.
152         */
153        public static long convertToLong( byte[] buf )
154        {
155            long value = ( (long) unpack4( buf, 0 ) << 32  )
156                         + ( unpack4( buf, 4 ) & 0xFFFFFFFFL );
157            value = ( value ^ 0x8000000000000000L ); // flip MSB because "long" is signed
158            return value;
159        }
160    
161    
162    
163    
164        static int unpack4( byte[] buf, int offset )
165        {
166            int value = ( buf[ offset ] << 24 )
167                | ( ( buf[ offset+1 ] << 16 ) & 0x00FF0000 )
168                | ( ( buf[ offset+2 ] << 8 ) & 0x0000FF00 )
169                | ( ( buf[ offset+3 ] << 0 ) & 0x000000FF );
170    
171            return value;
172        }
173    
174    
175        static final void pack2( byte[] data, int offs, int val )
176        {
177            data[offs++] = (byte) ( val >> 8 );
178            data[offs++] = (byte) val;
179        }
180    
181    
182        static final void pack4( byte[] data, int offs, int val )
183        {
184            data[offs++] = (byte) ( val >> 24 );
185            data[offs++] = (byte) ( val >> 16 );
186            data[offs++] = (byte) ( val >> 8 );
187            data[offs++] = (byte) val;
188        }
189    
190    
191        static final void pack8( byte[] data, int offs, long val )
192        {
193            pack4( data, 0, (int) ( val >> 32 ) );
194            pack4( data, 4, (int) val );
195        }
196    
197    
198        /**
199         * Test static methods
200         */
201        public static void main( String[] args )
202        {
203            byte[] buf;
204    
205            buf = convertToByteArray( (int) 5 );
206            System.out.println( "int value of 5 is: " + convertToInt( buf ) );
207    
208            buf = convertToByteArray( (int) -1 );
209            System.out.println( "int value of -1 is: " + convertToInt( buf ) );
210    
211            buf = convertToByteArray( (int) 22111000 );
212            System.out.println( "int value of 22111000 is: " + convertToInt( buf ) );
213    
214    
215            buf = convertToByteArray( (long) 5L );
216            System.out.println( "long value of 5 is: " + convertToLong( buf ) );
217    
218            buf = convertToByteArray( (long) -1L );
219            System.out.println( "long value of -1 is: " + convertToLong( buf ) );
220    
221            buf = convertToByteArray( (long) 1112223334445556667L );
222            System.out.println( "long value of 1112223334445556667 is: " + convertToLong( buf ) );
223        }
224    
225    }