Package io.pravega.client.byteStream
Class ByteStreamWriter
- java.lang.Object
-
- java.io.OutputStream
-
- io.pravega.client.byteStream.ByteStreamWriter
-
- All Implemented Interfaces:
java.io.Closeable,java.io.Flushable,java.lang.AutoCloseable
public abstract class ByteStreamWriter extends java.io.OutputStreamAllows for writing raw bytes directly to a segment. This is intended as low level building block for creating higher level components. As such it can break things. This class does not frame, attach headers, or otherwise modify the bytes written to it in any way. So unlikeEventStreamWriterorRevisionedStreamClientthe data written cannot be split apart when read. As such, any bytes written by this API can ONLY be read usingByteStreamReader. Similarly, unless some sort of framing is added it is probably an error to have multiple ByteStreamWriters write to the same segment as this will result in interleaved data. The methods on this class are non-blocking unless otherwise specified. As such data passed to aOutputStream.write(byte[])call cannot be assumed to be persisted until a flush has been called. It is safe to invoke methods on this class from multiple threads but doing so will not result in an increase in performance.
-
-
Constructor Summary
Constructors Constructor Description ByteStreamWriter()
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description abstract voidclose()Flushes the buffer and closes the writer.abstract voidcloseAndSeal()Closes the writer similar toclose()but also seals it so that no future writes can ever be made.abstract longfetchHeadOffset()This makes a synchronous RPC call to the server to obtain the current head of the stream.abstract longfetchTailOffset()This makes a synchronous RPC call to the server to obtain the total number of bytes written to the segment in its history.abstract voidflush()Blocks until all data written has been durably persisted.abstract 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.abstract voidtruncateDataBefore(long offset)This makes a synchronous RPC call to the server to truncate the segment at the provided offset.abstract voidwrite(byte[] b, int off, int len)Writes the provided data to the segment.abstract voidwrite(java.nio.ByteBuffer src)Similar towrite(byte[], int, int)Writes the provided data to the segment.
-
-
-
Method Detail
-
write
public abstract void write(java.nio.ByteBuffer src) throws java.io.IOExceptionSimilar towrite(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.- 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 abstract void write(byte[] b, int off, int len) throws java.io.IOExceptionWrites 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.- Overrides:
writein classjava.io.OutputStream- Throws:
java.io.IOException- See Also:
OutputStream.write(byte[], int, int)
-
close
public abstract void close() throws java.io.IOExceptionFlushes the buffer and closes the writer. If there is data to flush, this is a blocking method.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Overrides:
closein classjava.io.OutputStream- Throws:
java.io.IOException- See Also:
OutputStream.close()
-
flush
public abstract void flush() throws java.io.IOExceptionBlocks until all data written has been durably persisted.- Specified by:
flushin interfacejava.io.Flushable- Overrides:
flushin classjava.io.OutputStream- Throws:
java.io.IOException- If for any reason the flush fails including if the stream is sealed.- See Also:
OutputStream.flush()
-
flushAsync
public abstract java.util.concurrent.CompletableFuture<java.lang.Void> flushAsync() throws java.io.IOExceptionThe 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.- 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 abstract void closeAndSeal() throws java.io.IOExceptionCloses the writer similar toclose()but also seals it so that no future writes can ever be made.- Throws:
java.io.IOException- If for any reason the flush fails including if the stream is sealed.
-
fetchHeadOffset
public abstract long fetchHeadOffset()
This makes a synchronous RPC call to the server to obtain the current head of the stream.- Returns:
- The current head offset
-
fetchTailOffset
public abstract 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. 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.- Returns:
- The tail offset
-
truncateDataBefore
public abstract void truncateDataBefore(long offset)
This makes a synchronous RPC call to the server to truncate the segment at the provided offset.- Parameters:
offset- The truncation offset.
-
-