public static class TemporaryBuffer.LocalFile extends TemporaryBuffer
Initially this output stream buffers to memory and is therefore similar to ByteArrayOutputStream, but it shifts to using an on disk temporary file if the output gets too large.
The content of this buffered stream may be sent to another OutputStream
only after this stream has been properly closed by TemporaryBuffer.close()
.
TemporaryBuffer.Block, TemporaryBuffer.Heap, TemporaryBuffer.LocalFile
Modifier and Type | Field and Description |
---|---|
private java.io.File |
directory
Directory to store the temporary file under.
|
private java.io.File |
onDiskFile
Location of our temporary file if we are on disk; otherwise null.
|
blocks, DEFAULT_IN_CORE_LIMIT
Constructor and Description |
---|
LocalFile(java.io.File directory)
Create a new temporary buffer, limiting memory usage.
|
LocalFile(java.io.File directory,
int inCoreLimit)
Create a new temporary buffer, limiting memory usage.
|
Modifier and Type | Method and Description |
---|---|
void |
destroy()
Clear this buffer so it has no data, and cannot be used again.
|
long |
length()
Obtain the length (in bytes) of the buffer.
|
java.io.InputStream |
openInputStream()
Open an input stream to read from the buffered data.
|
java.io.InputStream |
openInputStreamWithAutoDestroy()
Same as
TemporaryBuffer.openInputStream() but handling destruction of any
associated resources automatically when closing the returned stream. |
protected java.io.OutputStream |
overflow()
Open the overflow output stream, so the remaining output can be stored.
|
byte[] |
toByteArray()
Convert this buffer's contents into a contiguous byte array.
|
void |
writeTo(java.io.OutputStream os,
ProgressMonitor pm)
Send this buffer to an output stream.
|
close, copy, doFlush, reset, toByteArray, write, write
private final java.io.File directory
private java.io.File onDiskFile
If we exceeded the TemporaryBuffer.inCoreLimit
we nulled out TemporaryBuffer.blocks
and created this file instead. All output goes here through
overflow()
.
public LocalFile(java.io.File directory)
directory
- if the buffer has to spill over into a temporary file, the
directory where the file should be saved. If null the
system default temporary directory (for example /tmp) will
be used instead.public LocalFile(java.io.File directory, int inCoreLimit)
directory
- if the buffer has to spill over into a temporary file, the
directory where the file should be saved. If null the
system default temporary directory (for example /tmp) will
be used instead.inCoreLimit
- maximum number of bytes to store in memory. Storage beyond
this limit will use the local file.protected java.io.OutputStream overflow() throws java.io.IOException
TemporaryBuffer
overflow
in class TemporaryBuffer
java.io.IOException
- the buffer cannot create the overflow stream.public long length()
TemporaryBuffer
The length is only accurate after TemporaryBuffer.close()
has been invoked.
length
in class TemporaryBuffer
public byte[] toByteArray() throws java.io.IOException
TemporaryBuffer
The buffer is only complete after TemporaryBuffer.close()
has been invoked.
toByteArray
in class TemporaryBuffer
TemporaryBuffer.length()
.java.io.IOException
- an error occurred reading from a local temporary filepublic void writeTo(java.io.OutputStream os, ProgressMonitor pm) throws java.io.IOException
TemporaryBuffer
This method may only be invoked after TemporaryBuffer.close()
has completed
normally, to ensure all data is completely transferred.
writeTo
in class TemporaryBuffer
os
- stream to send this buffer's complete content to.pm
- if not null progress updates are sent here. Caller should
initialize the task and the number of work units to
TemporaryBuffer.length()
/1024
.java.io.IOException
- an error occurred reading from a temporary file on the local
system, or writing to the output stream.public java.io.InputStream openInputStream() throws java.io.IOException
TemporaryBuffer
This method may only be invoked after TemporaryBuffer.close()
has completed
normally, to ensure all data is completely transferred.
openInputStream
in class TemporaryBuffer
java.io.IOException
- an error occurred opening the temporary file.public java.io.InputStream openInputStreamWithAutoDestroy() throws java.io.IOException
TemporaryBuffer
TemporaryBuffer.openInputStream()
but handling destruction of any
associated resources automatically when closing the returned stream.openInputStreamWithAutoDestroy
in class TemporaryBuffer
TemporaryBuffer.close()
java.io.IOException
- in case of an error.public void destroy()
TemporaryBuffer
destroy
in class TemporaryBuffer