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.InputStream;
025    import java.io.OutputStream;
026    
027    public class QuotedPrintable {
028        // NOTE:  the QuotedPrintableEncoder class needs to keep some active state about what's going on with
029        // respect to line breaks and significant white spaces.  This makes it difficult to keep one static
030        // instance of the decode around for reuse.
031    
032    
033        /**
034         * encode the input data producing a Q-P encoded byte array.
035         *
036         * @return a byte array containing the Q-P encoded data.
037         */
038        public static byte[] encode(
039            byte[]    data)
040        {
041            return encode(data, 0, data.length);
042        }
043    
044        /**
045         * encode the input data producing a Q-P encoded byte array.
046         *
047         * @return a byte array containing the Q-P encoded data.
048         */
049        public static byte[] encode(
050            byte[]    data,
051            int       off,
052            int       length)
053        {
054            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
055    
056            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
057    
058            try
059            {
060                encoder.encode(data, off, length, bOut);
061            }
062            catch (IOException e)
063            {
064                throw new RuntimeException("exception encoding Q-P encoded string: " + e);
065            }
066    
067            return bOut.toByteArray();
068        }
069    
070        /**
071         * Q-P encode the byte data writing it to the given output stream.
072         *
073         * @return the number of bytes produced.
074         */
075        public static int encode(
076            byte[]         data,
077            OutputStream   out)
078            throws IOException
079        {
080            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
081    
082            return encoder.encode(data, 0, data.length, out);
083        }
084    
085        /**
086         * Q-P encode the byte data writing it to the given output stream.
087         *
088         * @return the number of bytes produced.
089         */
090        public static int encode(
091            byte[]         data,
092            int            off,
093            int            length,
094            OutputStream   out)
095            throws IOException
096        {
097            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
098            return encoder.encode(data, 0, data.length, out);
099        }
100    
101        /**
102         * decode the Q-P encoded input data. It is assumed the input data is valid.
103         *
104         * @return a byte array representing the decoded data.
105         */
106        public static byte[] decode(
107            byte[]    data)
108        {
109            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
110    
111            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
112            try
113            {
114                encoder.decode(data, 0, data.length, bOut);
115            }
116            catch (IOException e)
117            {
118                throw new RuntimeException("exception decoding Q-P encoded string: " + e);
119            }
120    
121            return bOut.toByteArray();
122        }
123    
124        /**
125         * decode the UUEncided String data.
126         *
127         * @return a byte array representing the decoded data.
128         */
129        public static byte[] decode(
130            String    data)
131        {
132            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
133            ByteArrayOutputStream    bOut = new ByteArrayOutputStream();
134    
135            try
136            {
137                encoder.decode(data, bOut);
138            }
139            catch (IOException e)
140            {
141                throw new RuntimeException("exception decoding Q-P encoded string: " + e);
142            }
143    
144            return bOut.toByteArray();
145        }
146    
147        /**
148         * decode the Q-P encoded encoded String data writing it to the given output stream.
149         *
150         * @return the number of bytes produced.
151         */
152        public static int decode(
153            String          data,
154            OutputStream    out)
155            throws IOException
156        {
157            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
158            return encoder.decode(data, out);
159        }
160    
161        /**
162         * decode the base Q-P encoded String data writing it to the given output stream,
163         * whitespace characters will be ignored.
164         *
165         * @param data   The array data to decode.
166         * @param out    The output stream for the data.
167         *
168         * @return the number of bytes produced.
169         * @exception IOException
170         */
171        public static int decode(byte [] data, OutputStream out) throws IOException
172        {
173            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder();
174            return encoder.decode(data, 0, data.length, out);
175        }
176    }
177