public abstract class TemporaryBuffer
extends java.io.OutputStream
Subclasses determine the behavior when the in-memory buffer capacity has been exceeded and additional bytes are still being received for output.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
TemporaryBuffer.Block |
private class |
TemporaryBuffer.BlockInputStream |
static class |
TemporaryBuffer.Heap
A temporary buffer that will never exceed its in-memory limit.
|
static class |
TemporaryBuffer.LocalFile
A fully buffered output stream using local disk storage for large data.
|
Modifier and Type | Field and Description |
---|---|
(package private) java.util.ArrayList<TemporaryBuffer.Block> |
blocks
Chain of data, if we are still completely in-core; otherwise null.
|
protected static int |
DEFAULT_IN_CORE_LIMIT
Default limit for in-core storage.
|
private int |
inCoreLimit
Maximum number of bytes we will permit storing in memory.
|
private int |
initialBlocks
Initial size of block list.
|
private java.io.OutputStream |
overflow
If
inCoreLimit has been reached, remainder goes here. |
Modifier | Constructor and Description |
---|---|
protected |
TemporaryBuffer(int limit)
Create a new empty temporary buffer.
|
protected |
TemporaryBuffer(int estimatedSize,
int limit)
Create a new empty temporary buffer.
|
Modifier and Type | Method and Description |
---|---|
void |
close() |
void |
copy(java.io.InputStream in)
Copy all bytes remaining on the input stream into this buffer.
|
void |
destroy()
Clear this buffer so it has no data, and cannot be used again.
|
protected void |
doFlush()
Dumps the entire buffer into the overflow stream, and flushes it.
|
private long |
inCoreLength() |
private TemporaryBuffer.Block |
last() |
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
openInputStream() but handling destruction of any
associated resources automatically when closing the returned stream. |
protected abstract java.io.OutputStream |
overflow()
Open the overflow output stream, so the remaining output can be stored.
|
private boolean |
reachedInCoreLimit() |
void |
reset()
Reset this buffer for reuse, purging all buffered content.
|
private void |
switchToOverflow() |
byte[] |
toByteArray()
Convert this buffer's contents into a contiguous byte array.
|
byte[] |
toByteArray(int limit)
Convert this buffer's contents into a contiguous byte array.
|
void |
write(byte[] b,
int off,
int len) |
void |
write(int b) |
void |
writeTo(java.io.OutputStream os,
ProgressMonitor pm)
Send this buffer to an output stream.
|
protected static final int DEFAULT_IN_CORE_LIMIT
java.util.ArrayList<TemporaryBuffer.Block> blocks
private int inCoreLimit
When this limit is reached the data will be shifted to a file on disk, preventing the JVM heap from growing out of control.
private int initialBlocks
private java.io.OutputStream overflow
inCoreLimit
has been reached, remainder goes here.protected TemporaryBuffer(int limit)
limit
- maximum number of bytes to store in memory before entering the
overflow output path; also used as the estimated size.protected TemporaryBuffer(int estimatedSize, int limit)
estimatedSize
- estimated size of storage used, to size the initial list of
block pointers.limit
- maximum number of bytes to store in memory before entering the
overflow output path.public void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
public void write(byte[] b, int off, int len) throws java.io.IOException
write
in class java.io.OutputStream
java.io.IOException
protected void doFlush() throws java.io.IOException
java.io.IOException
- the overflow stream cannot be started, or the buffer contents
cannot be written to it, or it failed to flush.public void copy(java.io.InputStream in) throws java.io.IOException
in
- the stream to read from, until EOF is reached.java.io.IOException
- an error occurred reading from the input stream, or while
writing to a local temporary file.public long length()
The length is only accurate after close()
has been invoked.
private long inCoreLength()
public byte[] toByteArray() throws java.io.IOException
The buffer is only complete after close()
has been invoked.
length()
.java.io.IOException
- an error occurred reading from a local temporary filepublic byte[] toByteArray(int limit) throws java.io.IOException
limit
bytes
The buffer is only complete after close()
has been invoked.
limit
- the maximum number of bytes to be returnedlimit
bytes.java.io.IOException
- an error occurred reading from a local temporary filepublic void writeTo(java.io.OutputStream os, ProgressMonitor pm) throws java.io.IOException
This method may only be invoked after close()
has completed
normally, to ensure all data is completely transferred.
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
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
This method may only be invoked after close()
has completed
normally, to ensure all data is completely transferred.
java.io.IOException
- an error occurred opening the temporary file.public java.io.InputStream openInputStreamWithAutoDestroy() throws java.io.IOException
openInputStream()
but handling destruction of any
associated resources automatically when closing the returned stream.close()
java.io.IOException
- in case of an error.public void reset()
protected abstract java.io.OutputStream overflow() throws java.io.IOException
java.io.IOException
- the buffer cannot create the overflow stream.private TemporaryBuffer.Block last()
private boolean reachedInCoreLimit() throws java.io.IOException
java.io.IOException
private void switchToOverflow() throws java.io.IOException
java.io.IOException
public void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
public void destroy()