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