@ThreadSafe
public class StreamBuffer
extends java.lang.Object
InputStream
for reading, reading blocks until data becomes available or the buffer is closed.
To avoid in-memory data growing excessively, this buffer stores a single "chunk" at the time. Only after a chunk is consumed, a new one can be appended.
To support range requests in a memory-efficient way, StreamBufferInputStream
also allows skipping the first
skipOffset - 1
bytes without allocating memory (or worse: downloading the actual bytes only to have them
thrown away to get to the actual range)
Constructor and Description |
---|
StreamBuffer(long skipOffset)
Constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
closeForCompleted()
Close this buffer because all expected data has been written
Reading will return the end of stream marker after all data has been consumed.
|
void |
closeForError(java.lang.Throwable t)
Close this buffer before all data is written due to an error.
|
java.io.InputStream |
getInputStream()
Obtain the input stream to read this data.
|
boolean |
tryWrite(com.google.protobuf.ByteString data)
Try to append a chunk of data for consumption.
|
void |
write(com.google.protobuf.ByteString data)
Append a chunk of data for consumption.
|
public StreamBuffer(long skipOffset)
skipOffset
- index of the first actual byte to return (public void closeForError(java.lang.Throwable t)
t
- the cause for the buffer to be closed.public void closeForCompleted()
public void write(com.google.protobuf.ByteString data)
data
- the data to write into the bufferjava.lang.IllegalStateException
- if writing is attempted after the buffer has been closedpublic boolean tryWrite(com.google.protobuf.ByteString data)
data
- the data to write into the bufferjava.lang.IllegalStateException
- if writing is attempted after the buffer has been closedpublic java.io.InputStream getInputStream()
java.lang.IllegalStateException
- if invoked multiple times