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.message; |
---|
21 | |
---|
22 | import java.io.ByteArrayOutputStream; |
---|
23 | import java.io.IOException; |
---|
24 | import java.io.InputStream; |
---|
25 | import java.io.UnsupportedEncodingException; |
---|
26 | |
---|
27 | import org.apache.james.mime4j.dom.BinaryBody; |
---|
28 | import org.apache.james.mime4j.dom.TextBody; |
---|
29 | import org.apache.james.mime4j.util.CharsetUtil; |
---|
30 | |
---|
31 | /** |
---|
32 | * Factory for creating message bodies. |
---|
33 | */ |
---|
34 | public class BasicBodyFactory implements BodyFactory { |
---|
35 | |
---|
36 | private static String DEFAULT_CHARSET = CharsetUtil.DEFAULT_CHARSET.name(); |
---|
37 | |
---|
38 | public BinaryBody binaryBody(final InputStream is) throws IOException { |
---|
39 | return new BasicBinaryBody(bufferContent(is)); |
---|
40 | } |
---|
41 | |
---|
42 | public TextBody textBody(final InputStream is, final String mimeCharset) throws IOException { |
---|
43 | return new BasicTextBody(bufferContent(is), mimeCharset); |
---|
44 | } |
---|
45 | |
---|
46 | private static byte[] bufferContent(final InputStream is) throws IOException { |
---|
47 | if (is == null) { |
---|
48 | throw new IllegalArgumentException("Input stream may not be null"); |
---|
49 | } |
---|
50 | ByteArrayOutputStream buf = new ByteArrayOutputStream(); |
---|
51 | byte[] tmp = new byte[2048]; |
---|
52 | int l; |
---|
53 | while ((l = is.read(tmp)) != -1) { |
---|
54 | buf.write(tmp, 0, l); |
---|
55 | } |
---|
56 | return buf.toByteArray(); |
---|
57 | } |
---|
58 | |
---|
59 | public TextBody textBody(final String text, final String mimeCharset) throws UnsupportedEncodingException { |
---|
60 | if (text == null) { |
---|
61 | throw new IllegalArgumentException("Text may not be null"); |
---|
62 | } |
---|
63 | return new BasicTextBody(text.getBytes(mimeCharset), mimeCharset); |
---|
64 | } |
---|
65 | |
---|
66 | public TextBody textBody(final String text) throws UnsupportedEncodingException { |
---|
67 | return textBody(text, DEFAULT_CHARSET); |
---|
68 | } |
---|
69 | |
---|
70 | public BinaryBody binaryBody(final byte[] buf) { |
---|
71 | return new BasicBinaryBody(buf); |
---|
72 | } |
---|
73 | |
---|
74 | } |
---|