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