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