KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > core > MimeMessageInputStreamSource


1 /***********************************************************************
2  * Copyright (c) 2000-2004 The Apache Software Foundation. *
3  * All rights reserved. *
4  * ------------------------------------------------------------------- *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you *
6  * may not use this file except in compliance with the License. You *
7  * may obtain a copy of the License at: *
8  * *
9  * http://www.apache.org/licenses/LICENSE-2.0 *
10  * *
11  * Unless required by applicable law or agreed to in writing, software *
12  * distributed under the License is distributed on an "AS IS" BASIS, *
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or *
14  * implied. See the License for the specific language governing *
15  * permissions and limitations under the License. *
16  ***********************************************************************/

17
18 package org.apache.james.core;
19
20 import java.io.*;
21 import javax.mail.MessagingException JavaDoc;
22
23 import org.apache.avalon.framework.activity.Disposable;
24
25 /**
26  * Takes an input stream and creates a repeatable input stream source
27  * for a MimeMessageWrapper. It does this by completely reading the
28  * input stream and saving that to a temporary file that should delete on exit,
29  * or when this object is GC'd.
30  *
31  * @see MimeMessageWrapper
32  *
33  *
34  */

35 public class MimeMessageInputStreamSource
36     extends MimeMessageSource
37     implements Disposable {
38
39     /**
40      * A temporary file used to hold the message stream
41      */

42     File file = null;
43
44     /**
45      * The full path of the temporary file
46      */

47     String JavaDoc sourceId = null;
48
49     /**
50      * Construct a new MimeMessageInputStreamSource from an
51      * <code>InputStream</code> that contains the bytes of a
52      * MimeMessage.
53      *
54      * @param key the prefix for the name of the temp file
55      * @param in the stream containing the MimeMessage
56      *
57      * @throws MessagingException if an error occurs while trying to store
58      * the stream
59      */

60     public MimeMessageInputStreamSource(String JavaDoc key, InputStream in)
61             throws MessagingException JavaDoc {
62         //We want to immediately read this into a temporary file
63
//Create a temp file and channel the input stream into it
64
OutputStream fout = null;
65         try {
66             file = File.createTempFile(key, ".m64");
67             fout = new BufferedOutputStream(new FileOutputStream(file));
68             int b = -1;
69             while ((b = in.read()) != -1) {
70                 fout.write(b);
71             }
72             fout.flush();
73
74             sourceId = file.getCanonicalPath();
75         } catch (IOException ioe) {
76             throw new MessagingException JavaDoc("Unable to retrieve the data: " + ioe.getMessage(), ioe);
77         } finally {
78             try {
79                 if (fout != null) {
80                     fout.close();
81                 }
82             } catch (IOException ioe) {
83                 // Ignored - logging unavailable to log this non-fatal error.
84
}
85
86             try {
87                 if (in != null) {
88                     in.close();
89                 }
90             } catch (IOException ioe) {
91                 // Ignored - logging unavailable to log this non-fatal error.
92
}
93         }
94     }
95
96     /**
97      * Returns the unique identifier of this input stream source
98      *
99      * @return the unique identifier for this MimeMessageInputStreamSource
100      */

101     public String JavaDoc getSourceId() {
102         return sourceId;
103     }
104
105     /**
106      * Get an input stream to retrieve the data stored in the temporary file
107      *
108      * @return a <code>BufferedInputStream</code> containing the data
109      */

110     public synchronized InputStream getInputStream() throws IOException {
111         return new BufferedInputStream(new FileInputStream(file));
112     }
113
114     /**
115      * Get the size of the temp file
116      *
117      * @return the size of the temp file
118      *
119      * @throws IOException if an error is encoutered while computing the size of the message
120      */

121     public long getMessageSize() throws IOException {
122         return file.length();
123     }
124
125     /**
126      * @see org.apache.avalon.framework.activity.Disposable#dispose()
127      */

128     public void dispose() {
129         try {
130             if (file != null && file.exists()) {
131                 file.delete();
132             }
133         } catch (Exception JavaDoc e) {
134             //ignore
135
}
136         file = null;
137     }
138
139     /**
140      * <p>Finalizer that closes and deletes the temp file. Very bad.</p>
141      * We're leaving this in temporarily, while also establishing a more
142      * formal mechanism for cleanup through use of the dispose() method.
143      *
144      */

145     public void finalize() {
146         dispose();
147     }
148 }
149
Popular Tags