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