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 QuotedPrintableOutputStream qpOut = new QuotedPrintableOutputStream(out, true);
057 copy(in, qpOut);
058 qpOut.close();
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 QuotedPrintableOutputStream qpOut = new QuotedPrintableOutputStream(out, false);
071 copy(in, qpOut);
072 qpOut.close();
073 }
074
075 /**
076 * Encodes the given stream using base64.
077 *
078 * @param in not null
079 * @param out not null
080 * @throws IOException if an I/O error occurs
081 */
082 public static void encodeBase64(final InputStream in, final OutputStream out) throws IOException {
083 Base64OutputStream b64Out = new Base64OutputStream(out);
084 copy(in, b64Out);
085 b64Out.close();
086 }
087
088 /**
089 * Wraps the given stream in a Quoted-Printable encoder.
090 * @param out not null
091 * @return encoding outputstream
092 * @throws IOException
093 */
094 public static OutputStream wrapQuotedPrintable(final OutputStream out, boolean binary) throws IOException {
095 return new QuotedPrintableOutputStream(out, binary);
096 }
097
098 /**
099 * Wraps the given stream in a Base64 encoder.
100 * @param out not null
101 * @return encoding outputstream
102 * @throws IOException
103 */
104 public static OutputStream wrapBase64(final OutputStream out) throws IOException {
105 return new Base64OutputStream(out);
106 }
107
108 }