KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > webdocwf > util > loader > ToAndFromHex


1 /*
2   Loader - tool for transfering data from one JDBC source to another and
3   doing transformations during copy.
4     Copyright (C) 2002-2003 Together
5     This library is free software; you can redistribute it and/or
6     modify it under the terms of the GNU Lesser General Public
7     License as published by the Free Software Foundation; either
8     version 2.1 of the License, or (at your option) any later version.
9     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
24
25 package org.webdocwf.util.loader;
26
27 import java.io.*;
28
29 /**
30  * ToAndFromHex class is used for transformation binary objects to string object, and
31  * string object to binary object
32  * @author Radoslav Dutina
33  * @version 1.0
34  */

35 public class ToAndFromHex {
36
37   /**
38    * Empty constructor of ToAndFromHex class
39    */

40   public ToAndFromHex() {
41   }
42
43   /**
44    * This method transform binary object to string object
45    * @param b is array of bytes which represents binary object
46    * @return string representation of binary object
47    */

48   public static String JavaDoc getStringFromBlob(byte[] b){
49
50     if(b!=null){
51       ByteArrayInputStream is=new ByteArrayInputStream(b);
52
53       char[] hexBytes = {
54         '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
55       };
56
57       int c;
58       String JavaDoc hexString=new String JavaDoc();
59       while ((c = is.read()) >= 0) {
60         hexString+=(hexBytes[(c >> 4) & 0xf]);
61         hexString+=(hexBytes[c & 0xf]);
62       }
63       return hexString;
64     }else{
65       return null;
66     }
67   }
68
69   /**
70    * This method transform string object to binary object (array of bytes)
71    * @param val is string representation of binary object
72    * @return binary object
73    */

74   public static byte[] getByteArrayFromString(String JavaDoc val){
75     byte[] buf = new byte[val.length() / 2];
76     final char[] hexBytes = {
77       '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'
78     };
79     byte[] hexMap = new byte[256];
80     for (int i = 0; i < hexBytes.length; i++) {
81       hexMap[hexBytes[i]] = (byte)i;
82     }
83     int pos = 0;
84     for (int i = 0; i < buf.length; i++) {
85       buf[i] = (byte)(hexMap[val.charAt(pos++)] << 4);
86       buf[i] += hexMap[val.charAt(pos++)];
87     }
88     return buf;
89   }
90
91
92 }
93
Popular Tags