001 /**
002 *
003 * Copyright 2003-2006 The Apache Software Foundation
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.geronimo.mail.util;
019
020 import java.io.ByteArrayOutputStream;
021 import java.io.IOException;
022 import java.io.InputStream;
023 import java.io.OutputStream;
024
025 public class QuotedPrintable {
026 // NOTE: the QuotedPrintableEncoder class needs to keep some active state about what's going on with
027 // respect to line breaks and significant white spaces. This makes it difficult to keep one static
028 // instance of the decode around for reuse.
029
030
031 /**
032 * encode the input data producing a Q-P encoded byte array.
033 *
034 * @return a byte array containing the Q-P encoded data.
035 */
036 public static byte[] encode(
037 byte[] data)
038 {
039 return encode(data, 0, data.length);
040 }
041
042 /**
043 * encode the input data producing a Q-P encoded byte array.
044 *
045 * @return a byte array containing the Q-P encoded data.
046 */
047 public static byte[] encode(
048 byte[] data,
049 int off,
050 int length)
051 {
052 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
053
054 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
055
056 try
057 {
058 encoder.encode(data, off, length, bOut);
059 }
060 catch (IOException e)
061 {
062 throw new RuntimeException("exception encoding Q-P encoded string: " + e);
063 }
064
065 return bOut.toByteArray();
066 }
067
068 /**
069 * Q-P encode the byte data writing it to the given output stream.
070 *
071 * @return the number of bytes produced.
072 */
073 public static int encode(
074 byte[] data,
075 OutputStream out)
076 throws IOException
077 {
078 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
079
080 return encoder.encode(data, 0, data.length, out);
081 }
082
083 /**
084 * Q-P encode the byte data writing it to the given output stream.
085 *
086 * @return the number of bytes produced.
087 */
088 public static int encode(
089 byte[] data,
090 int off,
091 int length,
092 OutputStream out)
093 throws IOException
094 {
095 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
096 return encoder.encode(data, 0, data.length, out);
097 }
098
099 /**
100 * decode the Q-P encoded input data. It is assumed the input data is valid.
101 *
102 * @return a byte array representing the decoded data.
103 */
104 public static byte[] decode(
105 byte[] data)
106 {
107 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
108
109 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
110 try
111 {
112 encoder.decode(data, 0, data.length, bOut);
113 }
114 catch (IOException e)
115 {
116 throw new RuntimeException("exception decoding Q-P encoded string: " + e);
117 }
118
119 return bOut.toByteArray();
120 }
121
122 /**
123 * decode the UUEncided String data.
124 *
125 * @return a byte array representing the decoded data.
126 */
127 public static byte[] decode(
128 String data)
129 {
130 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
131 ByteArrayOutputStream bOut = new ByteArrayOutputStream();
132
133 try
134 {
135 encoder.decode(data, bOut);
136 }
137 catch (IOException e)
138 {
139 throw new RuntimeException("exception decoding Q-P encoded string: " + e);
140 }
141
142 return bOut.toByteArray();
143 }
144
145 /**
146 * decode the Q-P encoded encoded String data writing it to the given output stream.
147 *
148 * @return the number of bytes produced.
149 */
150 public static int decode(
151 String data,
152 OutputStream out)
153 throws IOException
154 {
155 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
156 return encoder.decode(data, out);
157 }
158
159 /**
160 * decode the base Q-P encoded String data writing it to the given output stream,
161 * whitespace characters will be ignored.
162 *
163 * @param data The array data to decode.
164 * @param out The output stream for the data.
165 *
166 * @return the number of bytes produced.
167 * @exception IOException
168 */
169 public static int decode(byte [] data, OutputStream out) throws IOException
170 {
171 QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
172 return encoder.decode(data, 0, data.length, out);
173 }
174 }
175