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.message;
021
022 import java.io.ByteArrayOutputStream;
023 import java.io.IOException;
024 import java.io.InputStream;
025 import java.io.UnsupportedEncodingException;
026 import java.nio.charset.Charset;
027 import java.nio.charset.UnsupportedCharsetException;
028
029 import org.apache.james.mime4j.dom.BinaryBody;
030 import org.apache.james.mime4j.dom.TextBody;
031 import org.apache.james.mime4j.util.CharsetUtil;
032
033 /**
034 * Factory for creating message bodies.
035 */
036 public class BasicBodyFactory implements BodyFactory {
037
038 public BinaryBody binaryBody(final InputStream is) throws IOException {
039 return new BasicBinaryBody(bufferContent(is));
040 }
041
042 public TextBody textBody(final InputStream is, final String mimeCharset) throws IOException {
043 return new BasicTextBody(bufferContent(is), mimeCharset);
044 }
045
046 private static byte[] bufferContent(final InputStream is) throws IOException {
047 if (is == null) {
048 throw new IllegalArgumentException("Input stream may not be null");
049 }
050 ByteArrayOutputStream buf = new ByteArrayOutputStream();
051 byte[] tmp = new byte[2048];
052 int l;
053 while ((l = is.read(tmp)) != -1) {
054 buf.write(tmp, 0, l);
055 }
056 return buf.toByteArray();
057 }
058
059 public TextBody textBody(final String text, final String mimeCharset) throws UnsupportedEncodingException {
060 if (text == null) {
061 throw new IllegalArgumentException("Text may not be null");
062 }
063 Charset charset = Charset.forName(mimeCharset);
064 try {
065 return new StringBody(text, charset);
066 } catch (UnsupportedCharsetException ex) {
067 throw new UnsupportedEncodingException(ex.getMessage());
068 }
069 }
070
071 public TextBody textBody(final String text, final Charset charset) {
072 if (text == null) {
073 throw new IllegalArgumentException("Text may not be null");
074 }
075 return new StringBody(text, charset);
076 }
077
078 public TextBody textBody(final String text) {
079 return textBody(text, CharsetUtil.DEFAULT_CHARSET);
080 }
081
082 public BinaryBody binaryBody(final byte[] buf) {
083 return new BasicBinaryBody(buf);
084 }
085
086 }