Class BufferedByteStreamWriterImpl
- java.lang.Object
-
- java.io.OutputStream
-
- io.pravega.client.byteStream.ByteStreamWriter
-
- io.pravega.client.byteStream.impl.BufferedByteStreamWriterImpl
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.Flushable
,java.lang.AutoCloseable
public class BufferedByteStreamWriterImpl extends ByteStreamWriter
This class buffers individual calls towrite(int)
so that we don't make a separate RPC per byte. It attempts to do this in a Lazy way, not allocating buffer space unless it is needed.
-
-
Field Summary
Fields Modifier and Type Field Description static int
BUFFER_SIZE
-
Constructor Summary
Constructors Constructor Description BufferedByteStreamWriterImpl(@NonNull ByteStreamWriterImpl out)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Flushes the buffer and closes the writer.void
closeAndSeal()
Closes the writer similar toByteStreamWriter.close()
but also seals it so that no future writes can ever be made.long
fetchHeadOffset()
This makes a synchronous RPC call to the server to obtain the current head of the stream.long
fetchTailOffset()
This makes a synchronous RPC call to the server to obtain the total number of bytes written to the segment in its history.void
flush()
Blocks until all data written has been durably persisted.java.util.concurrent.CompletableFuture<java.lang.Void>
flushAsync()
The future will complete successfully when all data which was passed to the write prior to calling this method has been persisted, and will complete exceptionally if that is not possible such as for example if the segment is sealed.void
truncateDataBefore(long offset)
This makes a synchronous RPC call to the server to truncate the segment at the provided offset.void
write(byte[] b, int off, int len)
Writes the provided data to the segment.void
write(int b)
void
write(java.nio.ByteBuffer src)
Similar toByteStreamWriter.write(byte[], int, int)
Writes the provided data to the segment.
-
-
-
Field Detail
-
BUFFER_SIZE
public static final int BUFFER_SIZE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
BufferedByteStreamWriterImpl
@ConstructorProperties("out") public BufferedByteStreamWriterImpl(@NonNull @NonNull ByteStreamWriterImpl out)
-
-
Method Detail
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in classjava.io.OutputStream
- Throws:
java.io.IOException
-
write
public void write(java.nio.ByteBuffer src) throws java.io.IOException
Description copied from class:ByteStreamWriter
Similar toByteStreamWriter.write(byte[], int, int)
Writes the provided data to the segment. The data is buffered internally to avoid blocking. As such it cannot be assumed to be durably stored until a flush completes. It is intended that this method not block, but it may in the event that the server becomes disconnected for sufficiently long or is sufficiently slow that that backlog of data to be written becomes a memory issue.- Specified by:
write
in classByteStreamWriter
- Parameters:
src
- The bytes to write.- Throws:
java.io.IOException
- If for any reason an error occurs writing the data, including if the stream is sealed.
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
Description copied from class:ByteStreamWriter
Writes the provided data to the segment. The data is buffered internally to avoid blocking. As such it cannot be assumed to be durably stored until a flush completes. It is intended that this method not block, but it may in the event that the server becomes disconnected for sufficiently long or is sufficiently slow that that backlog of data to be written becomes a memory issue.- Specified by:
write
in classByteStreamWriter
- Throws:
java.io.IOException
- See Also:
OutputStream.write(byte[], int, int)
-
close
public void close() throws java.io.IOException
Description copied from class:ByteStreamWriter
Flushes the buffer and closes the writer. If there is data to flush, this is a blocking method.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in classByteStreamWriter
- Throws:
java.io.IOException
- See Also:
OutputStream.close()
-
flush
public void flush() throws java.io.IOException
Description copied from class:ByteStreamWriter
Blocks until all data written has been durably persisted.- Specified by:
flush
in interfacejava.io.Flushable
- Specified by:
flush
in classByteStreamWriter
- Throws:
java.io.IOException
- If for any reason the flush fails including if the stream is sealed.- See Also:
OutputStream.flush()
-
flushAsync
public java.util.concurrent.CompletableFuture<java.lang.Void> flushAsync() throws java.io.IOException
Description copied from class:ByteStreamWriter
The future will complete successfully when all data which was passed to the write prior to calling this method has been persisted, and will complete exceptionally if that is not possible such as for example if the segment is sealed.- Specified by:
flushAsync
in classByteStreamWriter
- Returns:
- The future related to last write
- Throws:
java.io.IOException
- If for any reason the flush fails including if the stream is sealed.- See Also:
OutputStream.flush()
-
closeAndSeal
public void closeAndSeal() throws java.io.IOException
Description copied from class:ByteStreamWriter
Closes the writer similar toByteStreamWriter.close()
but also seals it so that no future writes can ever be made.- Specified by:
closeAndSeal
in classByteStreamWriter
- Throws:
java.io.IOException
- If for any reason the flush fails including if the stream is sealed.
-
fetchHeadOffset
public long fetchHeadOffset()
Description copied from class:ByteStreamWriter
This makes a synchronous RPC call to the server to obtain the current head of the stream.- Specified by:
fetchHeadOffset
in classByteStreamWriter
- Returns:
- The current head offset
-
fetchTailOffset
public long fetchTailOffset()
Description copied from class:ByteStreamWriter
This makes a synchronous RPC call to the server to obtain the total number of bytes written to the segment in its history. This is the sum total of the bytes written in all calls toOutputStream.write(byte[])
that have been flushed. It does not include data that was passed toOutputStream.write(byte[])
but which has not yet been persisted.- Specified by:
fetchTailOffset
in classByteStreamWriter
- Returns:
- The tail offset
-
truncateDataBefore
public void truncateDataBefore(long offset)
Description copied from class:ByteStreamWriter
This makes a synchronous RPC call to the server to truncate the segment at the provided offset.- Specified by:
truncateDataBefore
in classByteStreamWriter
- Parameters:
offset
- The truncation offset.
-
-