1 /**
2 *
3 * Copyright 2003-2004 The Apache Software Foundation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * 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.mail.util;
19
20 import java.io.ByteArrayOutputStream;
21 import java.io.IOException;
22 import java.io.OutputStream;
23
24 public class UUEncode {
25 private static final Encoder encoder = new UUEncoder();
26
27 /**
28 * encode the input data producing a UUEncoded byte array.
29 *
30 * @return a byte array containing the UUEncoded data.
31 */
32 public static byte[] encode(
33 byte[] data)
34 {
35 return encode(data, 0, data.length);
36 }
37
38 /**
39 * encode the input data producing a UUEncoded byte array.
40 *
41 * @return a byte array containing the UUEncoded data.
42 */
43 public static byte[] encode(
44 byte[] data,
45 int off,
46 int length)
47 {
48 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
49
50 try
51 {
52 encoder.encode(data, off, length, bOut);
53 }
54 catch (IOException e)
55 {
56 throw new RuntimeException("exception encoding UUEncoded string: " + e);
57 }
58
59 return bOut.toByteArray();
60 }
61
62 /**
63 * UUEncode the byte data writing it to the given output stream.
64 *
65 * @return the number of bytes produced.
66 */
67 public static int encode(
68 byte[] data,
69 OutputStream out)
70 throws IOException
71 {
72 return encoder.encode(data, 0, data.length, out);
73 }
74
75 /**
76 * UUEncode the byte data writing it to the given output stream.
77 *
78 * @return the number of bytes produced.
79 */
80 public static int encode(
81 byte[] data,
82 int off,
83 int length,
84 OutputStream out)
85 throws IOException
86 {
87 return encoder.encode(data, 0, data.length, out);
88 }
89
90 /**
91 * decode the UUEncoded input data. It is assumed the input data is valid.
92 *
93 * @return a byte array representing the decoded data.
94 */
95 public static byte[] decode(
96 byte[] data)
97 {
98 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
99
100 try
101 {
102 encoder.decode(data, 0, data.length, bOut);
103 }
104 catch (IOException e)
105 {
106 throw new RuntimeException("exception decoding UUEncoded string: " + e);
107 }
108
109 return bOut.toByteArray();
110 }
111
112 /**
113 * decode the UUEncided String data.
114 *
115 * @return a byte array representing the decoded data.
116 */
117 public static byte[] decode(
118 String data)
119 {
120 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
121
122 try
123 {
124 encoder.decode(data, bOut);
125 }
126 catch (IOException e)
127 {
128 throw new RuntimeException("exception decoding UUEncoded string: " + e);
129 }
130
131 return bOut.toByteArray();
132 }
133
134 /**
135 * decode the UUEncoded encoded String data writing it to the given output stream.
136 *
137 * @return the number of bytes produced.
138 */
139 public static int decode(
140 String data,
141 OutputStream out)
142 throws IOException
143 {
144 return encoder.decode(data, out);
145 }
146 }
147