1 | /**************************************************************** |
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one * |
---|
3 | * or more contributor license agreements. See the NOTICE file * |
---|
4 | * distributed with this work for additional information * |
---|
5 | * regarding copyright ownership. The ASF licenses this file * |
---|
6 | * to you under the Apache License, Version 2.0 (the * |
---|
7 | * "License"); you may not use this file except in compliance * |
---|
8 | * with the License. You may obtain a copy of the License at * |
---|
9 | * * |
---|
10 | * http://www.apache.org/licenses/LICENSE-2.0 * |
---|
11 | * * |
---|
12 | * Unless required by applicable law or agreed to in writing, * |
---|
13 | * software distributed under the License is distributed on an * |
---|
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * |
---|
15 | * KIND, either express or implied. See the License for the * |
---|
16 | * specific language governing permissions and limitations * |
---|
17 | * under the License. * |
---|
18 | ****************************************************************/ |
---|
19 | |
---|
20 | package org.apache.james.mime4j.codec; |
---|
21 | |
---|
22 | import java.io.IOException; |
---|
23 | import java.io.InputStream; |
---|
24 | import java.io.OutputStream; |
---|
25 | |
---|
26 | /** |
---|
27 | * Utility methods related to codecs. |
---|
28 | */ |
---|
29 | public class CodecUtil { |
---|
30 | |
---|
31 | static final int DEFAULT_ENCODING_BUFFER_SIZE = 1024; |
---|
32 | |
---|
33 | /** |
---|
34 | * Copies the contents of one stream to the other. |
---|
35 | * @param in not null |
---|
36 | * @param out not null |
---|
37 | * @throws IOException |
---|
38 | */ |
---|
39 | public static void copy(final InputStream in, final OutputStream out) throws IOException { |
---|
40 | final byte[] buffer = new byte[DEFAULT_ENCODING_BUFFER_SIZE]; |
---|
41 | int inputLength; |
---|
42 | while (-1 != (inputLength = in.read(buffer))) { |
---|
43 | out.write(buffer, 0, inputLength); |
---|
44 | } |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Encodes the given stream using Quoted-Printable. |
---|
49 | * This assumes that stream is binary and therefore escapes |
---|
50 | * all line endings. |
---|
51 | * @param in not null |
---|
52 | * @param out not null |
---|
53 | * @throws IOException |
---|
54 | */ |
---|
55 | public static void encodeQuotedPrintableBinary(final InputStream in, final OutputStream out) throws IOException { |
---|
56 | QuotedPrintableOutputStream qpOut = new QuotedPrintableOutputStream(out, true); |
---|
57 | copy(in, qpOut); |
---|
58 | qpOut.close(); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Encodes the given stream using Quoted-Printable. |
---|
63 | * This assumes that stream is text and therefore does not escape |
---|
64 | * all line endings. |
---|
65 | * @param in not null |
---|
66 | * @param out not null |
---|
67 | * @throws IOException |
---|
68 | */ |
---|
69 | public static void encodeQuotedPrintable(final InputStream in, final OutputStream out) throws IOException { |
---|
70 | QuotedPrintableOutputStream qpOut = new QuotedPrintableOutputStream(out, false); |
---|
71 | copy(in, qpOut); |
---|
72 | qpOut.close(); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Encodes the given stream using base64. |
---|
77 | * |
---|
78 | * @param in not null |
---|
79 | * @param out not null |
---|
80 | * @throws IOException if an I/O error occurs |
---|
81 | */ |
---|
82 | public static void encodeBase64(final InputStream in, final OutputStream out) throws IOException { |
---|
83 | Base64OutputStream b64Out = new Base64OutputStream(out); |
---|
84 | copy(in, b64Out); |
---|
85 | b64Out.close(); |
---|
86 | } |
---|
87 | |
---|
88 | /** |
---|
89 | * Wraps the given stream in a Quoted-Printable encoder. |
---|
90 | * @param out not null |
---|
91 | * @return encoding outputstream |
---|
92 | * @throws IOException |
---|
93 | */ |
---|
94 | public static OutputStream wrapQuotedPrintable(final OutputStream out, boolean binary) throws IOException { |
---|
95 | return new QuotedPrintableOutputStream(out, binary); |
---|
96 | } |
---|
97 | |
---|
98 | /** |
---|
99 | * 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 | } |
---|