FileDocCategorySizeDatePackage
MimeMessageInputStreamSource.javaAPI DocApache James 2.3.15826Fri Jan 12 12:56:24 GMT 2007org.apache.james.core

MimeMessageInputStreamSource

public class MimeMessageInputStreamSource extends MimeMessageSource implements org.apache.avalon.framework.activity.Disposable
Takes an input stream and creates a repeatable input stream source for a MimeMessageWrapper. It does this by completely reading the input stream and saving that to a temporary file that should delete on exit, or when this object is GC'd.
see
MimeMessageWrapper

Fields Summary
File
file
A temporary file used to hold the message stream
String
sourceId
The full path of the temporary file
Constructors Summary
public MimeMessageInputStreamSource(String key, InputStream in)
Construct a new MimeMessageInputStreamSource from an InputStream that contains the bytes of a MimeMessage.

param
key the prefix for the name of the temp file
param
in the stream containing the MimeMessage
throws
MessagingException if an error occurs while trying to store the stream

        //We want to immediately read this into a temporary file
        //Create a temp file and channel the input stream into it
        OutputStream fout = null;
        try {
            file = File.createTempFile(key, ".m64");
            fout = new BufferedOutputStream(new FileOutputStream(file));
            int b = -1;
            while ((b = in.read()) != -1) {
                fout.write(b);
            }
            fout.flush();

            sourceId = file.getCanonicalPath();
        } catch (IOException ioe) {
            // We had an IOException preparing the temporary file, so
            // don't just leave it around to garbage collect later.
            // It isn't as if we are going to use it after we throw
            // the MessagingException.
            if (fout != null) try {
                fout.close();
                fout = null;
            } catch (IOException _) {
                // Ignored - logging unavailable to log this error.
            }

            if (file != null) {
                file.delete();
                file = null;
            }

            throw new MessagingException("Unable to retrieve the data: " + ioe.getMessage(), ioe);
        } finally {
            try {
                if (fout != null) {
                    fout.close();
                }
            } catch (IOException ioe) {
                // Ignored - logging unavailable to log this non-fatal error.
            }

            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ioe) {
                // Ignored - logging unavailable to log this non-fatal error.
            }
        }
    
Methods Summary
public voiddispose()

see
org.apache.avalon.framework.activity.Disposable#dispose()

        try {
            if (file != null && file.exists()) {
                file.delete();
            }
        } catch (Exception e) {
            //ignore
        }
        file = null;
    
public synchronized java.io.InputStreamgetInputStream()
Get an input stream to retrieve the data stored in the temporary file

return
a BufferedInputStream containing the data

        return new SharedFileInputStream(file);
    
public longgetMessageSize()
Get the size of the temp file

return
the size of the temp file
throws
IOException if an error is encoutered while computing the size of the message

        return file.length();
    
public java.lang.StringgetSourceId()
Returns the unique identifier of this input stream source

return
the unique identifier for this MimeMessageInputStreamSource

        return sourceId;