KickJava   Java API By Example, From Geeks To Geeks.

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


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 import sun.misc.BASE64Decoder;
16 import sun.misc.BASE64Encoder;
17
18 /**
19  * Uses the sun.misc classes to do BASE64 encoding and decoding. The
20  * sun.misc classes are not supported by Sun. Perhaps once we have to
21  * plug in another class.
22  *
23  * @author Michiel Meeuwissen
24  */

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

39
40     public Map JavaDoc transformers() {
41         HashMap JavaDoc h = new HashMap JavaDoc();
42         h.put(ENCODING, new Config(Base64.class, BASE_64, "Base 64 encoding base on sun.misc.BASE64* classes"));
43         return h;
44     }
45
46
47     public String JavaDoc transform(byte[] bytes) {
48         return new BASE64Encoder().encode(bytes);
49     }
50
51     public byte[] transformBack(String JavaDoc r) {
52         try {
53             BASE64Decoder dec = new BASE64Decoder();
54             return dec.decodeBuffer(r);
55         } catch (Exception JavaDoc e) {
56             e.printStackTrace();
57             throw new IllegalArgumentException JavaDoc("the entered string to decode properly was wrong: " + e);
58         }
59     }
60
61     public String JavaDoc getEncoding() {
62         return ENCODING;
63     }
64 }
65
Popular Tags