FileDocCategorySizeDatePackage
FileBackedMimeBodyPart.javaAPI DocBouncy Castle Crypto API 1.41 (Java 1.5)4467Wed Oct 01 10:55:28 BST 2008org.bouncycastle.mail.smime.util

FileBackedMimeBodyPart

public class FileBackedMimeBodyPart extends MimeBodyPart

Fields Summary
private static final int
BUF_SIZE
private final File
_file
Constructors Summary
public FileBackedMimeBodyPart(File file)
Create a MimeBodyPart backed by the data in file.

param
file file containing the body part.
throws
MessagingException an exception occurs parsing file.
throws
IOException an exception occurs accessing file.


                                       
     
         
          
    
        super(new SharedFileInputStream(file));

        _file = file;
    
public FileBackedMimeBodyPart(InputStream content, File file)
Create a MimeBodyPart backed by file based on the headers and content data in content.

param
content an inputstream containing the body part.
param
file a handle to the backing file to use for storage.
throws
MessagingException an exception occurs parsing the resulting body part in file.
throws
IOException an exception occurs accessing file or content.

        this(saveStreamToFile(content, file));
    
public FileBackedMimeBodyPart(InternetHeaders headers, InputStream body, File file)
Create a MimeBodyPart backed by file, with the headers given in headers and body content taken from the stream body.

param
headers headers for the body part.
param
body internal content for the body part.
param
file backing file to use.
throws
MessagingException if the body part can't be produced.
throws
IOException if there is an issue reading stream or writing to file.

        this(saveStreamToFile(headers, body, file));
    
Methods Summary
public voiddispose()
Close off the underlying shared streams and remove the backing file.

throws
IOException if streams cannot be closed or the file cannot be deleted.

        ((SharedFileInputStream)contentStream).getRoot().dispose();
        
        if (_file.exists() && !_file.delete())
        {
            throw new IOException("deletion of underlying file <" + _file.getCanonicalPath() + "> failed.");
        }
    
private static voidsaveContentToStream(java.io.OutputStream out, java.io.InputStream content)

        byte[] buf = new byte[BUF_SIZE];
        int    len;

        while ((len = content.read(buf, 0, buf.length)) > 0)
        {
            out.write(buf, 0, len);
        }

        out.close();
        content.close();
    
private static java.io.FilesaveStreamToFile(java.io.InputStream content, java.io.File tempFile)

        saveContentToStream(new FileOutputStream(tempFile), content);

        return tempFile;
    
private static java.io.FilesaveStreamToFile(javax.mail.internet.InternetHeaders headers, java.io.InputStream content, java.io.File tempFile)

        OutputStream out = new FileOutputStream(tempFile);
        Enumeration en = headers.getAllHeaderLines();

        while (en.hasMoreElements())
        {
            writeHeader(out, (String)en.nextElement());
        }

        writeSeperator(out);

        saveContentToStream(out, content);

        return tempFile;
    
private static voidwriteHeader(java.io.OutputStream out, java.lang.String header)

        for (int i = 0; i != header.length(); i++)
        {
            out.write(header.charAt(i));
        }

        writeSeperator(out);
    
private static voidwriteSeperator(java.io.OutputStream out)

         out.write('\r");
         out.write('\n");
     
public voidwriteTo(java.io.OutputStream out)

        if (!_file.exists())
        {
            throw new IOException("file " + _file.getCanonicalPath() + " no longer exists.");
        }

        super.writeTo(out);