1 package com.atlassian.core.spool;
2
3 import java.io.*;
4
5
6
7
8
9
10 public class DeferredSpoolFileOutputStream extends DeferredFileOutputStream
11 {
12 private FileFactory fileFactory = DefaultSpoolFileFactory.getInstance();
13 private boolean unspooling = false;
14
15
16
17
18
19 public DeferredSpoolFileOutputStream(int threshold, File outputFile)
20 {
21 super(threshold, outputFile);
22 }
23
24
25
26
27
28
29
30 public DeferredSpoolFileOutputStream(int threshold, FileFactory fileFactory)
31 {
32 super(threshold, null);
33 this.fileFactory = fileFactory;
34 }
35
36
37
38
39 public boolean isClosed()
40 {
41 return closed;
42 }
43
44
45
46
47
48 public boolean isUnspooling()
49 {
50 return unspooling;
51 }
52
53 protected void thresholdReached() throws IOException
54 {
55 if (outputFile == null)
56 outputFile = fileFactory.createNewFile();
57 super.thresholdReached();
58 }
59
60
61
62
63
64
65
66
67
68 public InputStream getInputStream() throws IOException
69 {
70 if (!isClosed())
71 throw new IOException("Output stream not closed");
72
73 if (isUnspooling())
74 throw new IOException("Stream is already being unspooled");
75
76 InputStream spoolStream;
77
78 if (isInMemory())
79 spoolStream = new ByteArrayInputStream(getData());
80 else
81 try
82 {
83 spoolStream = new SpoolFileInputStream(getFile());
84 }
85 catch (FileNotFoundException ex)
86 {
87 throw new IOException("Deferred file does not exist");
88 }
89
90 unspooling = true;
91 return spoolStream;
92 }
93 }