KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > Hex


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.transformers;
11
12 import java.util.HashMap JavaDoc;
13 import java.util.Map JavaDoc;
14
15 /**
16  * Encode a bytearray to a string by converting every byte to a hexadecimal (00 - FF)
17  * representation, and decode the other way around.
18  *
19  * @author Johannes Verelst
20  * @since MMBase-1.8.1
21  * @version $Id: Hex.java,v 1.2 2006/07/24 14:36:16 pierre Exp $
22  */

23
24 public class Hex extends ByteArrayToCharTransformer implements ByteToCharTransformer, ConfigurableTransformer {
25     private final static String JavaDoc ENCODING = "HEX";
26     private final static int HEX = 1;
27
28     int to = HEX;
29
30     public void configure(int t) {
31         to = t;
32     }
33
34     /**
35      * Used when registering this class as a possible Transformer
36      */

37
38     public Map JavaDoc transformers() {
39         HashMap JavaDoc h = new HashMap JavaDoc();
40         h.put(ENCODING, new Config(Hex.class, HEX, "Encoding bytearrays to and from a hexidecimal string"));
41         return h;
42     }
43
44
45     /**
46      * Transform a bytearray to a string of hexadecimal digits.
47      */

48     public String JavaDoc transform(byte[] bytes) {
49         StringBuffer JavaDoc strbuf = new StringBuffer JavaDoc(bytes.length * 2);
50         for (int i = 0; i<bytes.length; i++) {
51             if (((int) bytes[i] & 0xff) < 0x10) {
52                 strbuf.append("0");
53             }
54             strbuf.append(Long.toString((int) bytes[i] & 0xff, 16));
55         }
56
57         return strbuf.toString();
58     }
59
60     /**
61      * Transform a string of hexadecimal digits to a bytearray.
62      * @param r The string to transform
63      * @return an array of bytes
64      * @throws IllegalArgumentException whenever the input string is not correctly formatted.
65      */

66     public byte[] transformBack(String JavaDoc r) {
67         try {
68             int strlen = r.length();
69             byte[] retval = new byte[strlen/2];
70             for (int i=0; i<strlen; i+=2) {
71                 char c1 = r.charAt(i);
72                 char c2 = r.charAt(i+1);
73                 int b = 0;
74                 if (c1 >= '0' && c1 <= '9') {
75                     b += 16 * (c1 - '0');
76                 } else {
77                     b += 16 * (10 + c1 - 'a');
78                 }
79                 if (c2 >= '0' && c2 <= '9') {
80                     b += (c2 - '0');
81                 } else {
82                     b += (10 + c2 - 'a');
83                 }
84                 retval[i/2] = (byte)b;
85             }
86             return retval;
87         } catch (Exception JavaDoc e) {
88             e.printStackTrace();
89             throw new IllegalArgumentException JavaDoc("the entered string to decode properly was wrong: " + e);
90         }
91     }
92
93     public String JavaDoc getEncoding() {
94         return ENCODING;
95     }
96 }
97
Popular Tags