001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019
020 package org.apache.geronimo.mail.util;
021
022 import java.io.ByteArrayOutputStream;
023 import java.io.IOException;
024 import java.io.OutputStream;
025
026 public class UUEncode {
027 private static final Encoder encoder = new UUEncoder();
028
029 /**
030 * encode the input data producing a UUEncoded byte array.
031 *
032 * @return a byte array containing the UUEncoded data.
033 */
034 public static byte[] encode(
035 byte[] data)
036 {
037 return encode(data, 0, data.length);
038 }
039
040 /**
041 * encode the input data producing a UUEncoded byte array.
042 *
043 * @return a byte array containing the UUEncoded data.
044 */
045 public static byte[] encode(
046 byte[] data,
047 int off,
048 int length)
049 {
050 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
051
052 try
053 {
054 encoder.encode(data, off, length, bOut);
055 }
056 catch (IOException e)
057 {
058 throw new RuntimeException("exception encoding UUEncoded string: " + e);
059 }
060
061 return bOut.toByteArray();
062 }
063
064 /**
065 * UUEncode the byte data writing it to the given output stream.
066 *
067 * @return the number of bytes produced.
068 */
069 public static int encode(
070 byte[] data,
071 OutputStream out)
072 throws IOException
073 {
074 return encoder.encode(data, 0, data.length, out);
075 }
076
077 /**
078 * UUEncode the byte data writing it to the given output stream.
079 *
080 * @return the number of bytes produced.
081 */
082 public static int encode(
083 byte[] data,
084 int off,
085 int length,
086 OutputStream out)
087 throws IOException
088 {
089 return encoder.encode(data, 0, data.length, out);
090 }
091
092 /**
093 * decode the UUEncoded input data. It is assumed the input data is valid.
094 *
095 * @return a byte array representing the decoded data.
096 */
097 public static byte[] decode(
098 byte[] data)
099 {
100 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
101
102 try
103 {
104 encoder.decode(data, 0, data.length, bOut);
105 }
106 catch (IOException e)
107 {
108 throw new RuntimeException("exception decoding UUEncoded string: " + e);
109 }
110
111 return bOut.toByteArray();
112 }
113
114 /**
115 * decode the UUEncided String data.
116 *
117 * @return a byte array representing the decoded data.
118 */
119 public static byte[] decode(
120 String data)
121 {
122 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
123
124 try
125 {
126 encoder.decode(data, bOut);
127 }
128 catch (IOException e)
129 {
130 throw new RuntimeException("exception decoding UUEncoded string: " + e);
131 }
132
133 return bOut.toByteArray();
134 }
135
136 /**
137 * decode the UUEncoded encoded String data writing it to the given output stream.
138 *
139 * @return the number of bytes produced.
140 */
141 public static int decode(
142 String data,
143 OutputStream out)
144 throws IOException
145 {
146 return encoder.decode(data, out);
147 }
148 }
149