1   package com.atlassian.core.spool;
2   
3   import java.io.BufferedInputStream;
4   import java.io.BufferedOutputStream;
5   import java.io.File;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.OutputStream;
10  
11  import org.apache.commons.io.IOUtils;
12  
13  /**
14   * Spool bytes via buffered file streams. The returned input stream is a SpoolFileInputStream
15   *
16   * @see SpoolFileInputStream
17   */
18  public class BufferedFileSpool implements FileSpool {
19  	
20      private FileFactory fileFactory = DefaultSpoolFileFactory.getInstance();
21  
22      public FileFactory getFileFactory()
23      {
24          return fileFactory;
25      }
26  
27      public void setFileFactory(FileFactory fileFactory)
28      {
29          this.fileFactory = fileFactory;
30      }
31  
32      public InputStream spool(InputStream is) throws IOException
33      {
34  		File spoolFile = fileFactory.createNewFile();
35  
36  		OutputStream os = new BufferedOutputStream(new FileOutputStream(spoolFile));
37  		IOUtils.copy(is, os);
38  		os.close();
39  		
40  		return new BufferedInputStream(new SpoolFileInputStream(spoolFile));
41  	}
42  
43  }