MemoryTextBody.java revision 8978aac1977408b05e386ae846c30920c7faa0a6
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 20package org.apache.james.mime4j.message; 21 22import java.io.ByteArrayInputStream; 23import java.io.ByteArrayOutputStream; 24import java.io.IOException; 25import java.io.InputStream; 26import java.io.InputStreamReader; 27import java.io.OutputStream; 28import java.io.Reader; 29import java.io.UnsupportedEncodingException; 30 31import org.apache.commons.io.IOUtils; 32import org.apache.commons.logging.Log; 33import org.apache.commons.logging.LogFactory; 34import org.apache.james.mime4j.util.CharsetUtil; 35import org.apache.james.mime4j.util.TempPath; 36import org.apache.james.mime4j.util.TempStorage; 37 38 39/** 40 * Text body backed by a {@link org.apache.james.mime4j.util.TempFile}. 41 * 42 * 43 * @version $Id: TempFileTextBody.java,v 1.3 2004/10/25 07:26:46 ntherning Exp $ 44 */ 45class MemoryTextBody extends AbstractBody implements TextBody { 46 private static Log log = LogFactory.getLog(MemoryTextBody.class); 47 48 private String mimeCharset = null; 49 private byte[] tempFile = null; 50 51 public MemoryTextBody(InputStream is) throws IOException { 52 this(is, null); 53 } 54 55 public MemoryTextBody(InputStream is, String mimeCharset) 56 throws IOException { 57 58 this.mimeCharset = mimeCharset; 59 60 TempPath tempPath = TempStorage.getInstance().getRootTempPath(); 61 62 ByteArrayOutputStream out = new ByteArrayOutputStream(); 63 IOUtils.copy(is, out); 64 out.close(); 65 tempFile = out.toByteArray(); 66 } 67 68 /** 69 * @see org.apache.james.mime4j.message.TextBody#getReader() 70 */ 71 public Reader getReader() throws UnsupportedEncodingException, IOException { 72 String javaCharset = null; 73 if (mimeCharset != null) { 74 javaCharset = CharsetUtil.toJavaCharset(mimeCharset); 75 } 76 77 if (javaCharset == null) { 78 javaCharset = "ISO-8859-1"; 79 80 if (log.isWarnEnabled()) { 81 if (mimeCharset == null) { 82 log.warn("No MIME charset specified. Using " + javaCharset 83 + " instead."); 84 } else { 85 log.warn("MIME charset '" + mimeCharset + "' has no " 86 + "corresponding Java charset. Using " + javaCharset 87 + " instead."); 88 } 89 } 90 } 91 /* 92 if (log.isWarnEnabled()) { 93 if (mimeCharset == null) { 94 log.warn("No MIME charset specified. Using the " 95 + "platform's default charset."); 96 } else { 97 log.warn("MIME charset '" + mimeCharset + "' has no " 98 + "corresponding Java charset. Using the " 99 + "platform's default charset."); 100 } 101 } 102 103 return new InputStreamReader(tempFile.getInputStream()); 104 }*/ 105 106 return new InputStreamReader(new ByteArrayInputStream(tempFile), javaCharset); 107 } 108 109 110 /** 111 * @see org.apache.james.mime4j.message.Body#writeTo(java.io.OutputStream) 112 */ 113 public void writeTo(OutputStream out) throws IOException { 114 IOUtils.copy(new ByteArrayInputStream(tempFile), out); 115 } 116} 117