KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > h2 > util > ByteUtils


1 /*
2  * Copyright 2004-2006 H2 Group. Licensed under the H2 License, Version 1.0 (http://h2database.com/html/license.html).
3  * Initial Developer: H2 Group
4  */

5 package org.h2.util;
6
7 import java.io.ByteArrayInputStream JavaDoc;
8 import java.io.ByteArrayOutputStream JavaDoc;
9 import java.io.ObjectInputStream JavaDoc;
10 import java.io.ObjectOutputStream JavaDoc;
11 import java.sql.SQLException JavaDoc;
12
13 import org.h2.message.Message;
14
15
16 public class ByteUtils {
17     
18     private static final char[] HEX = "0123456789abcdef".toCharArray();
19     
20     public static int readInt(byte[] buff, int pos) {
21         return (buff[pos++]<< 24) + ((buff[pos++] & 0xff) << 16)
22         + ((buff[pos++] & 0xff) << 8) + (buff[pos++] & 0xff);
23     }
24
25     public static long readLong(byte[] buff, int pos) {
26         return ((long)(readInt(buff, pos)) << 32) + (readInt(buff, pos+4) & 0xffffffffL);
27     }
28
29     public static int indexOf(byte[] bytes, byte[] pattern, int start) {
30         if (pattern.length == 0) {
31             return start;
32         }
33         if (start > bytes.length) {
34             return -1;
35         }
36         int last = bytes.length - pattern.length + 1;
37         next:
38         for(;start < last; start++) {
39             for(int i=0; i<pattern.length; i++) {
40                 if(bytes[start + i] != pattern[i]) {
41                     continue next;
42                 }
43             }
44             return start;
45         }
46         return -1;
47     }
48     
49     public static byte[] convertStringToBytes(String JavaDoc s) throws SQLException JavaDoc {
50         int len = s.length();
51         if (len % 2 != 0) {
52             throw Message.getSQLException(Message.HEX_STRING_ODD_1, s);
53         }
54         len /= 2;
55         byte[] buff = new byte[len];
56         try {
57             for (int i = 0; i < len; i++) {
58                 buff[i] = (byte) ((Character.digit(s.charAt(i+i), 16) << 4) | (Character.digit(s.charAt(i+i+1), 16)));
59             }
60         } catch (NumberFormatException JavaDoc e) {
61             throw Message.getSQLException(Message.HEX_STRING_WRONG_1, s);
62         }
63         return buff;
64     }
65     
66     public static int getByteArrayHash(byte[] value) {
67         int h = 1;
68         for (int i = 0; i < value.length;) {
69             h = 31 * h + value[i++];
70         }
71         return h;
72     }
73     
74     public static String JavaDoc convertBytesToString(byte[] value) {
75         return convertBytesToString(value, value.length);
76     }
77
78     public static String JavaDoc convertBytesToString(byte[] value, int len) {
79         char[] buff = new char[len+len];
80         char[] hex = HEX;
81         for (int i = 0; i < len; i++) {
82             int c = value[i] & 0xff;
83             buff[i+i] = hex[c >> 4];
84             buff[i+i+1] = hex[c & 0xf];
85         }
86         return new String JavaDoc(buff);
87     }
88     
89     public static boolean compareSecure(byte[] test, byte[] good) {
90         if((test==null) || (good==null)) {
91             return (test == null) && (good == null);
92         }
93         if(test.length != good.length) {
94             return false;
95         }
96         if(test.length == 0) {
97             return true;
98         }
99         // silly loop: this should help a little against timing attacks
100
boolean correct = true, correct2 = false;
101         for(int i=0; i<good.length; i++) {
102             if(test[i] != good[i]) {
103                 correct = false;
104             } else {
105                 correct2 = true;
106             }
107         }
108         return correct && correct2;
109     }
110     
111     public static void clear(byte[] buff) {
112         for(int i=0; i<buff.length; i++) {
113             buff[i] = 0;
114         }
115     }
116     
117     public static int compareNotNull(byte[] data1, byte[] data2) {
118         int len = Math.min(data1.length, data2.length);
119         for (int i = 0; i < len; i++) {
120             byte b = data1[i];
121             byte b2 = data2[i];
122             if (b != b2) {
123                 return b > b2 ? 1 : -1;
124             }
125         }
126         int c = data1.length - data2.length;
127         return c == 0 ? 0 : (c < 0 ? -1 : 1);
128     }
129
130     public static String JavaDoc convertToBinString(byte[] buff) {
131         char[] chars = new char[buff.length];
132         for(int i=0; i<buff.length; i++) {
133             chars[i] = (char) (buff[i] & 0xff);
134         }
135         return new String JavaDoc(chars);
136     }
137
138     public static byte[] convertBinStringToBytes(String JavaDoc data) {
139         byte[] buff = new byte[data.length()];
140         for(int i=0; i<data.length(); i++) {
141             buff[i] = (byte) (data.charAt(i) & 0xff);
142         }
143         return buff;
144     }
145
146     public static byte[] copy(byte[] source, byte[] target) {
147         int len = source.length;
148         if(len > target.length) {
149             target = new byte[len];
150         }
151         System.arraycopy(source, 0, target, 0, len);
152         return target;
153     }
154
155     public static byte[] cloneByteArray(byte[] b) {
156         int len = b.length;
157         if(len == 0) {
158             return b;
159         }
160         byte[] copy = new byte[len];
161         System.arraycopy(b, 0, copy, 0, len);
162         return copy;
163     }
164
165     public static byte[] serialize(Object JavaDoc obj) throws SQLException JavaDoc {
166         try {
167             ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
168             ObjectOutputStream JavaDoc os = new ObjectOutputStream JavaDoc(out);
169             os.writeObject(obj);
170             return out.toByteArray();
171         } catch(Throwable JavaDoc e) {
172             throw Message.getSQLException(Message.SERIALIZATION_FAILED, null, e);
173         }
174     }
175
176     public static Object JavaDoc deserialize(byte[] data) throws SQLException JavaDoc {
177         try {
178             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(data);
179             ObjectInputStream JavaDoc is = new ObjectInputStream JavaDoc(in);
180             Object JavaDoc obj = is.readObject();
181             return obj;
182         } catch(Throwable JavaDoc e) {
183             throw Message.getSQLException(Message.DESERIALIZATION_FAILED, null, e);
184         }
185     }
186
187 }
188
Popular Tags