23
23
import java .io .OutputStream ;
24
24
import java .io .SequenceInputStream ;
25
25
import java .nio .file .Files ;
26
+ import java .nio .file .Path ;
26
27
27
28
import org .apache .commons .io .output .ThresholdingOutputStream ;
28
29
29
30
/**
30
- * Offloads to disk when a given memory consumption has been reacehd
31
+ * Offloads to disk when a given memory consumption has been reached
31
32
*/
32
33
class OffloadingOutputStream extends ThresholdingOutputStream
33
34
{
@@ -48,9 +49,9 @@ class OffloadingOutputStream extends ThresholdingOutputStream
48
49
private OutputStream currentOutputStream ;
49
50
50
51
/**
51
- * The file to which output will be directed if the threshold is exceeded.
52
+ * The path to which output will be directed if the threshold is exceeded.
52
53
*/
53
- private File outputFile = null ;
54
+ private Path outputPath = null ;
54
55
55
56
/**
56
57
* The temporary file prefix.
@@ -62,58 +63,30 @@ class OffloadingOutputStream extends ThresholdingOutputStream
62
63
*/
63
64
private final String suffix ;
64
65
65
- /**
66
- * The directory to use for temporary files.
67
- */
68
- private final File directory ;
69
-
70
- /**
71
- * True when close() has been called successfully.
72
- */
73
- private boolean closed = false ;
74
-
75
66
// ----------------------------------------------------------- Constructors
76
67
77
68
/**
78
69
* Constructs an instance of this class which will trigger an event at the
79
70
* specified threshold, and save data to a temporary file beyond that point.
80
71
*
81
72
* @param threshold The number of bytes at which to trigger an event.
82
- * @param prefix Prefix to use for the temporary file.
83
- * @param suffix Suffix to use for the temporary file.
84
- * @param directory Temporary file directory.
85
- *
73
+ * @param prefix Prefix to use for the temporary file.
74
+ * @param suffix Suffix to use for the temporary file.
86
75
* @since 1.4
87
76
*/
88
- public OffloadingOutputStream ( int threshold , String prefix , String suffix , File directory )
77
+ public OffloadingOutputStream ( int threshold , String prefix , String suffix )
89
78
{
90
- this ( threshold , null , prefix , suffix , directory );
79
+ super ( threshold );
80
+
91
81
if ( prefix == null )
92
82
{
93
83
throw new IllegalArgumentException ( "Temporary file prefix is missing" );
94
84
}
95
- }
96
-
97
- /**
98
- * Constructs an instance of this class which will trigger an event at the
99
- * specified threshold, and save data either to a file beyond that point.
100
- *
101
- * @param threshold The number of bytes at which to trigger an event.
102
- * @param outputFile The file to which data is saved beyond the threshold.
103
- * @param prefix Prefix to use for the temporary file.
104
- * @param suffix Suffix to use for the temporary file.
105
- * @param directory Temporary file directory.
106
- */
107
- private OffloadingOutputStream ( int threshold , File outputFile , String prefix , String suffix , File directory )
108
- {
109
- super ( threshold );
110
- this .outputFile = outputFile ;
111
85
112
86
memoryOutputStream = new ByteArrayOutputStream ( threshold / 10 );
113
87
currentOutputStream = memoryOutputStream ;
114
88
this .prefix = prefix ;
115
89
this .suffix = suffix ;
116
- this .directory = directory ;
117
90
}
118
91
119
92
// --------------------------------------- ThresholdingOutputStream methods
@@ -123,8 +96,7 @@ private OffloadingOutputStream( int threshold, File outputFile, String prefix, S
123
96
* based, depending on the current state with respect to the threshold.
124
97
*
125
98
* @return The underlying output stream.
126
- *
127
- * @exception java.io.IOException if an error occurs.
99
+ * @throws java.io.IOException if an error occurs.
128
100
*/
129
101
@ Override
130
102
protected OutputStream getStream () throws IOException
@@ -138,27 +110,24 @@ protected OutputStream getStream() throws IOException
138
110
* much data is being written to keep in memory, so we elect to switch to
139
111
* disk-based storage.
140
112
*
141
- * @exception java.io.IOException if an error occurs.
113
+ * @throws java.io.IOException if an error occurs.
142
114
*/
143
115
@ Override
144
116
protected void thresholdReached () throws IOException
145
117
{
146
- if ( prefix != null )
147
- {
148
- outputFile = File .createTempFile ( prefix , suffix , directory );
149
- }
150
- currentOutputStream = Files .newOutputStream ( outputFile .toPath () );
118
+ outputPath = Files .createTempFile ( prefix , suffix );
119
+ currentOutputStream = Files .newOutputStream ( outputPath );
151
120
}
152
121
153
122
public InputStream getInputStream () throws IOException
154
123
{
155
124
156
125
InputStream memoryAsInput = memoryOutputStream .toInputStream ();
157
- if ( outputFile == null )
126
+ if ( outputPath == null )
158
127
{
159
128
return memoryAsInput ;
160
129
}
161
- return new SequenceInputStream ( memoryAsInput , Files .newInputStream ( outputFile . toPath () ) );
130
+ return new SequenceInputStream ( memoryAsInput , Files .newInputStream ( outputPath ) );
162
131
}
163
132
164
133
// --------------------------------------------------------- Public methods
@@ -196,20 +165,18 @@ public byte[] getData()
196
165
*/
197
166
public File getFile ()
198
167
{
199
- return outputFile ;
168
+ return outputPath != null ? outputPath . toFile () : null ;
200
169
}
201
170
202
171
/**
203
- * Closes underlying output stream, and mark this as closed
172
+ * Closes underlying output stream.
204
173
*
205
- * @exception java.io.IOException if an error occurs.
174
+ * @throws java.io.IOException if an error occurs.
206
175
*/
207
176
@ Override
208
177
public void close () throws IOException
209
178
{
210
179
super .close ();
211
- closed = true ;
212
180
currentOutputStream .close ();
213
181
}
214
-
215
182
}
0 commit comments