01: /*
02: Loader - tool for transfering data from one JDBC source to another and
03: doing transformations during copy.
04: Copyright (C) 2002-2003 Together
05: This library is free software; you can redistribute it and/or
06: modify it under the terms of the GNU Lesser General Public
07: License as published by the Free Software Foundation; either
08: version 2.1 of the License, or (at your option) any later version.
09: This library is distributed in the hope that it will be useful,
10: but WITHOUT ANY WARRANTY; without even the implied warranty of
11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12: Lesser General Public License for more details.
13: You should have received a copy of the GNU Lesser General Public
14: License along with this library; if not, write to the Free Software
15: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16: Loader.java
17: Date: 03.03.2003.
18: @version 2.1 alpha
19: @authors:
20: Radoslav Dutina rale@prozone.co.yu
21: */
22:
23: package org.webdocwf.util.loader;
24:
25: import java.io.*;
26:
27: /**
28: * ToAndFromHex class is used for transformation binary objects to string object, and
29: * string object to binary object
30: * @author Radoslav Dutina
31: * @version 1.0
32: */
33: public class ToAndFromHex {
34:
35: /**
36: * Empty constructor of ToAndFromHex class
37: */
38: public ToAndFromHex() {
39: }
40:
41: /**
42: * This method transform binary object to string object
43: * @param b is array of bytes which represents binary object
44: * @return string representation of binary object
45: */
46: public static String getStringFromBlob(byte[] b) {
47:
48: if (b != null) {
49: ByteArrayInputStream is = new ByteArrayInputStream(b);
50:
51: char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6', '7',
52: '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
53:
54: int c;
55: String hexString = new String();
56: while ((c = is.read()) >= 0) {
57: hexString += (hexBytes[(c >> 4) & 0xf]);
58: hexString += (hexBytes[c & 0xf]);
59: }
60: return hexString;
61: } else {
62: return null;
63: }
64: }
65:
66: /**
67: * This method transform string object to binary object (array of bytes)
68: * @param val is string representation of binary object
69: * @return binary object
70: */
71: public static byte[] getByteArrayFromString(String val) {
72: byte[] buf = new byte[val.length() / 2];
73: final char[] hexBytes = { '0', '1', '2', '3', '4', '5', '6',
74: '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
75: byte[] hexMap = new byte[256];
76: for (int i = 0; i < hexBytes.length; i++) {
77: hexMap[hexBytes[i]] = (byte) i;
78: }
79: int pos = 0;
80: for (int i = 0; i < buf.length; i++) {
81: buf[i] = (byte) (hexMap[val.charAt(pos++)] << 4);
82: buf[i] += hexMap[val.charAt(pos++)];
83: }
84: return buf;
85: }
86:
87: }
|