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