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.james.mime4j.codec;
021    
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    
026    /**
027     * Utility methods related to codecs.
028     */
029    public class CodecUtil {
030        
031        static final int DEFAULT_ENCODING_BUFFER_SIZE = 1024;
032        
033        /**
034         * Copies the contents of one stream to the other.
035         * @param in not null
036         * @param out not null
037         * @throws IOException
038         */
039        public static void copy(final InputStream in, final OutputStream out) throws IOException {
040            final byte[] buffer = new byte[DEFAULT_ENCODING_BUFFER_SIZE];
041            int inputLength;
042            while (-1 != (inputLength = in.read(buffer))) {
043                out.write(buffer, 0, inputLength);
044            }
045        }
046        
047        /**
048         * Encodes the given stream using Quoted-Printable.
049         * This assumes that stream is binary and therefore escapes
050         * all line endings.
051         * @param in not null
052         * @param out not null
053         * @throws IOException
054         */
055        public static void encodeQuotedPrintableBinary(final InputStream in, final OutputStream out) throws IOException {
056            
057            QuotedPrintableEncoder encoder = new QuotedPrintableEncoder(DEFAULT_ENCODING_BUFFER_SIZE, true);
058            encoder.encode(in, out);
059        }
060        
061        /**
062         * Encodes the given stream using Quoted-Printable.
063         * This assumes that stream is text and therefore does not escape
064         * all line endings.
065         * @param in not null
066         * @param out not null
067         * @throws IOException
068         */
069        public static void encodeQuotedPrintable(final InputStream in, final OutputStream out) throws IOException {
070            final QuotedPrintableEncoder encoder = new QuotedPrintableEncoder(DEFAULT_ENCODING_BUFFER_SIZE, false);
071            encoder.encode(in, out);
072        }
073        
074        /**
075         * Encodes the given stream using base64.
076         *
077         * @param in not null
078         * @param out not null
079         * @throws IOException if an I/O error occurs
080         */
081        public static void encodeBase64(final InputStream in, final OutputStream out) throws IOException {
082            Base64OutputStream b64Out = new Base64OutputStream(out);
083            copy(in, b64Out);
084            b64Out.close();
085        }
086        
087        /**
088         * Wraps the given stream in a Quoted-Printable encoder.
089         * @param out not null
090         * @return encoding outputstream 
091         * @throws IOException
092         */
093        public static OutputStream wrapQuotedPrintable(final OutputStream out, boolean binary) throws IOException {
094            return new QuotedPrintableOutputStream(out, binary);
095        }
096        
097        /**
098         * Wraps the given stream in a Base64 encoder.
099         * @param out not null
100         * @return encoding outputstream 
101         * @throws IOException
102         */
103        public static OutputStream wrapBase64(final OutputStream out) throws IOException {
104            return new Base64OutputStream(out);
105        }
106    
107    }