KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > util > encoders > Base64


1 /**
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.geronimo.util.encoders;
19
20 import java.io.ByteArrayOutputStream JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.OutputStream JavaDoc;
23
24 public class Base64
25 {
26     private static final Encoder encoder = new Base64Encoder();
27
28     /**
29      * encode the input data producing a base 64 encoded byte array.
30      *
31      * @return a byte array containing the base 64 encoded data.
32      */

33     public static byte[] encode(
34         byte[] data)
35     {
36         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
37
38         try
39         {
40             encoder.encode(data, 0, data.length, bOut);
41         }
42         catch (IOException JavaDoc e)
43         {
44             throw new RuntimeException JavaDoc("exception encoding base64 string: " + e);
45         }
46
47         return bOut.toByteArray();
48     }
49
50     /**
51      * Encode the byte data to base 64 writing it to the given output stream.
52      *
53      * @return the number of bytes produced.
54      */

55     public static int encode(
56         byte[] data,
57         OutputStream JavaDoc out)
58         throws IOException JavaDoc
59     {
60         return encoder.encode(data, 0, data.length, out);
61     }
62
63     /**
64      * Encode the byte data to base 64 writing it to the given output stream.
65      *
66      * @return the number of bytes produced.
67      */

68     public static int encode(
69         byte[] data,
70         int off,
71         int length,
72         OutputStream JavaDoc out)
73         throws IOException JavaDoc
74     {
75         return encoder.encode(data, off, length, out);
76     }
77
78     /**
79      * decode the base 64 encoded input data. It is assumed the input data is valid.
80      *
81      * @return a byte array representing the decoded data.
82      */

83     public static byte[] decode(
84         byte[] data)
85     {
86         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
87
88         try
89         {
90             encoder.decode(data, 0, data.length, bOut);
91         }
92         catch (IOException JavaDoc e)
93         {
94             throw new RuntimeException JavaDoc("exception decoding base64 string: " + e);
95         }
96
97         return bOut.toByteArray();
98     }
99
100     /**
101      * decode the base 64 encoded String data - whitespace will be ignored.
102      *
103      * @return a byte array representing the decoded data.
104      */

105     public static byte[] decode(
106         String JavaDoc data)
107     {
108         ByteArrayOutputStream JavaDoc bOut = new ByteArrayOutputStream JavaDoc();
109
110         try
111         {
112             encoder.decode(data, bOut);
113         }
114         catch (IOException JavaDoc e)
115         {
116             throw new RuntimeException JavaDoc("exception decoding base64 string: " + e);
117         }
118
119         return bOut.toByteArray();
120     }
121
122     /**
123      * decode the base 64 encoded String data writing it to the given output stream,
124      * whitespace characters will be ignored.
125      *
126      * @return the number of bytes produced.
127      */

128     public static int decode(
129         String JavaDoc data,
130         OutputStream JavaDoc out)
131         throws IOException JavaDoc
132     {
133         return encoder.decode(data, out);
134     }
135 }
136
Popular Tags