1 package TransactionalIO.Utilities;
3 /* =============================================================
4 * SmallSQL : a free Java DBMS library for the Java(tm) platform
5 * =============================================================
7 * (C) Copyright 2004-2007, by Volker Berlin.
9 * Project Info: http://www.smallsql.de/
11 * This library is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This library is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
18 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
19 * License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
26 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
27 * in the United States and other countries.]
32 * Author: Volker Berlin
37 import java.io.FileNotFoundException;
38 import java.io.RandomAccessFile;
39 import java.nio.channels.FileLock;
40 import java.sql.SQLException;
43 public class Conversions {
48 public static int long2int(long value){
49 if(value > Integer.MAX_VALUE)
50 return Integer.MAX_VALUE;
51 if(value < Integer.MIN_VALUE)
52 return Integer.MIN_VALUE;
56 public static long double2long(double value){
57 if(value > Long.MAX_VALUE)
58 return Long.MAX_VALUE;
59 if(value < Long.MIN_VALUE)
60 return Long.MIN_VALUE;
66 public static float bytes2float( byte[] bytes ){
67 return Float.intBitsToFloat( bytes2int( bytes ) );
70 public static double bytes2double( byte[] bytes ){
71 return Double.longBitsToDouble( bytes2long( bytes ) );
74 public static long bytes2long( byte[] bytes ){
76 int length = Math.min( 8, bytes.length);
77 for(int i=0; i<length; i++){
78 result = (result << 8) | (bytes[i] & 0xFF);
83 public static int bytes2int( byte[] bytes ){
85 int length = Math.min( 4, bytes.length);
86 for(int i=0; i<length; i++){
87 result = (result << 8) | (bytes[i] & 0xFF);
92 static byte[] double2bytes( double value ){
93 return long2bytes(Double.doubleToLongBits(value));
96 public static byte[] float2bytes( float value ){
97 return int2bytes(Float.floatToIntBits(value));
100 public static byte[] long2bytes( long value ){
101 byte[] result = new byte[8];
102 result[0] = (byte)(value >>> 56);
103 result[1] = (byte)(value >>> 48);
104 result[2] = (byte)(value >>> 40);
105 result[3] = (byte)(value >>> 32);
106 result[4] = (byte)(value >>> 24);
107 result[5] = (byte)(value >>> 16);
108 result[6] = (byte)(value >>> 8);
109 result[7] = (byte)(value);
113 public static int money2int( long value ) {
114 if (value < Integer.MIN_VALUE) return Integer.MIN_VALUE;
115 else if (value > Integer.MAX_VALUE) return Integer.MAX_VALUE;
116 else return (int) value;
119 public static byte[] int2bytes( int value ){
120 byte[] result = new byte[4];
121 result[0] = (byte)(value >>> 24);
122 result[1] = (byte)(value >>> 16);
123 result[2] = (byte)(value >>> 8);
124 result[3] = (byte)(value >>> 0);
132 private static int hexDigit2int(char digit){
133 if(digit >= '0' && digit <= '9') return digit - '0';
135 if(digit >= 'a' && digit <= 'f') return digit - 'W'; // -'W' == -'a' + 10
136 throw new RuntimeException();
142 static boolean string2boolean( String val){
144 return Double.parseDouble( val ) != 0;
145 }catch(NumberFormatException e){/*ignore it if it not a number*/}
146 return "true".equalsIgnoreCase( val ) || "yes".equalsIgnoreCase( val ) || "t".equalsIgnoreCase( val );
150 static long doubleToMoney(double value){
152 return (long)(value * 10000 - 0.5);
153 return (long)(value * 10000 + 0.5);
156 static int indexOf( char value, char[] str, int offset, int length ){
158 for(int end = offset+length;offset < end; offset++){
159 if((str[offset] | 0x20) == value) return offset;
164 public static int indexOf( int value, int[] list ){
166 for(int end = list.length; offset < end; offset++){
167 if((list[offset]) == value) return offset;
172 public static int indexOf( byte[] value, byte[] list, int offset ){
173 int length = value.length;
175 for(int end = list.length-length; offset <= end; offset++){
176 for(int i=0; i<length; i++ ){
177 if(list[offset+i] != value[i]){
186 public static int compareBytes( byte[] leftBytes, byte[] rightBytes){
187 int length = Math.min( leftBytes.length, rightBytes.length );
189 for(int i=0; i<length; i++){
190 if(leftBytes[i] != rightBytes[i]){
191 comp = leftBytes[i] < rightBytes[i] ? -1 : 1;
195 if(comp == 0 && leftBytes.length != rightBytes.length){
196 comp = leftBytes.length < rightBytes.length ? -1 : 1;