package org.apache.james.core;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class MimeMessageInputStreamSource extends MimeMessageSource {

    String key = null;
	InputStream in = null;
    File file = null;

    //If you try to access this size first, it will load it into a temp file
    //  and work from there.

	public MimeMessageInputStreamSource(String key, InputStream in) {
        this.key = key;
		this.in = in;
	}

	/**
	 * Return an input stream to the data
	 */
    public synchronized InputStream getInputStream() throws IOException {
        if (file == null) {
            return in;
        } else {
            return new FileInputStream(file);
        }
	}

    /**
     * If not already, read the stream into a temp file
     */
    public synchronized long getSize() throws IOException {
        if (file == null) {
            //Create a temp file and channel the input stream into it
            file = File.createTempFile(key, ".m64");
            FileOutputStream fout = new FileOutputStream(file);
            int b = -1;
            while ((b = in.read()) != -1) {
                fout.write(b);
            }
            fout.close();
            in.close();
            file.deleteOnExit();
        }
        return file.length();
    }
}

@@ -1,178 +1,722 @@
-package org.apache.james.core;%0A%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0A
+/*%0A * Copyright (C) The Apache Software Foundation. All rights reserved.%0A *%0A * This software is published under the terms of the Apache Software License%0A * version 1.1, a copy of which has been included  with this distribution in%0A * the LICENSE file.%0A */%0Apackage org.apache.james.core;%0A%0Aimport java.io.BufferedInputStream;%0Aimport java.io.BufferedOutputStream;%0Aimport java.io.File;%0Aimport java.io.FileInputStream;%0Aimport java.io.FileOutputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.io.OutputStream;%0A%0A/**%0A * MimeMessageInputStreamSource.java%0A *%0A *%0A * Created:%0A *%0A * @author%0A * @version%0A *%0A * Modified by %3Ca href=%22mailto:okidz@pindad.com%22%3EOki DZ%3C/a%3E%0A * Thu Oct  4 15:15:27 WIT 2001%0A *%0A */
 %0Apub
@@ -1281,24 +1281,48 @@
  return new 
+BufferedInputStream(new 
 FileInputStr
@@ -1322,32 +1322,33 @@
 nputStream(file)
+)
 ;%0A        %7D%0A%09%7D%0A%0A
@@ -1642,20 +1642,16 @@
         
-File
 OutputSt
@@ -1662,24 +1662,49 @@
  fout = new 
+BufferedOutputStream(new 
 FileOutputSt
@@ -1713,16 +1713,17 @@
 am(file)
+)
 ;%0A      
@@ -1957,11 +1957,218 @@
 );%0A    %7D
+%0A%0A    public void finalize() %7B%0A        try %7B%0A            if (file != null && file.exists()) %7B%0A                file.delete();%0A            %7D%0A        %7D catch (Exception e) %7B%0A            //ignore%0A        %7D%0A    %7D
 %0A%7D%0A

Patch from Oki Dz. Uses Buffered streams, adds license, and adds a delete to the finalize of this object, to reduce file usage during JVM existance (rather than waiting until JVM exited)