Class DataWriter
- All Implemented Interfaces:
Closeable
,Flushable
,AutoCloseable
,Closeable
,Committable
The data is provided in the form of the InputStream
, byte array or java object.
It can allocate all the blocks or start writing to some predefined block and allocate the rest,
the later is handy for the headers of the Media
, or for storages with the predefined layout.
The operations are written for the input streams that have no predefined length, the data is written to the storage as it comes.
In case of any error (e.g. if space is not enough to store the data), the allocated data will safely be released and an error will be thrown.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Closes this entity and all related blocksvoid
commit()
Synchronizes the written data with the storagestatic <T extends InputStream>
longfromInputStream
(BlockAllocator blockAllocator, T inputStream) Writes the data from the input streamlong
Provides the first block of the stored dataProvides a status of this entitystatic DataWriter
open
(BlockAllocator blockAllocator, long startIndex) Opens the data writer at the specific starting block indexstatic long
safeWriter
(BlockAllocator blockAllocator, long startIndex, Consumer<DataWriter> dataWriterConsumer) Write the data from the unknown source to the storage in a safe mode.static long
safeWriter
(BlockAllocator blockAllocator, Consumer<DataWriter> dataWriterConsumer) Write the data from the unknown source to the storage in a safe mode.void
unwind()
This should be called if operation has been aborted and must be set back.void
write
(byte[] b) void
write
(int data) Writes one byte of the datastatic <T> long
writeObject
(BlockAllocator blockAllocator, T object) Writes the serialized java object to the store in the safe manner.<T> void
writeObject
(T object) Writes a java object to the storageMethods inherited from class java.io.OutputStream
flush, nullOutputStream, write
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.rostore.v2.media.Closeable
checkOpened
-
Method Details
-
safeWriter
public static long safeWriter(BlockAllocator blockAllocator, Consumer<DataWriter> dataWriterConsumer) Write the data from the unknown source to the storage in a safe mode.The operation creates a data writer object and executes the write operations as a consumer, if any error during write operation happens, it get reverted and only after it the exception is thrown.
There is no need for any additional clean up in the case of error.
- Parameters:
blockAllocator
- the block allocator be useddataWriterConsumer
- the consumer that receive the data writer object and should provide the data to it- Returns:
- the first block index
-
writeObject
Writes the serialized java object to the store in the safe manner.It is similar to
safeWriter(BlockAllocator, Consumer)
, only the data is provided in the for of java object.The java object is serialized with
BinaryMapper
.- Type Parameters:
T
- the type of the java object- Parameters:
blockAllocator
- the block allocator to get the blocksobject
- the java object to store- Returns:
- the index of the first allocated block
-
write
public void write(byte[] b) - Overrides:
write
in classOutputStream
-
safeWriter
public static long safeWriter(BlockAllocator blockAllocator, long startIndex, Consumer<DataWriter> dataWriterConsumer) Write the data from the unknown source to the storage in a safe mode.This one is similar to
safeWriter(BlockAllocator, Consumer)
, only the first block index is provided explicitly, all other blocks are allocated by the block allocator.- Parameters:
blockAllocator
- the block allocator to be usedstartIndex
- the index of the first blockdataWriterConsumer
- the secured consumer that is writing to this data writer- Returns:
- the start index
-
open
Opens the data writer at the specific starting block index- Parameters:
blockAllocator
- allocator to be usedstartIndex
- the block index to start writing to- Returns:
- the data writer object
-
fromInputStream
public static <T extends InputStream> long fromInputStream(BlockAllocator blockAllocator, T inputStream) Writes the data from the input streamOperation will be reverted if any error happens.
- Type Parameters:
T
- the subtype of the input stream- Parameters:
blockAllocator
- the block allocator to be usedinputStream
- the input stream with the data- Returns:
- the first block of the data
-
getStartIndex
public long getStartIndex()Provides the first block of the stored data- Returns:
- the first block index
-
writeObject
public <T> void writeObject(T object) Writes a java object to the storageThe object is serialized by
BinaryMapper
.- Type Parameters:
T
- the type of the object- Parameters:
object
- the object to store
-
write
public void write(int data) Writes one byte of the data- Specified by:
write
in classOutputStream
- Parameters:
data
- thebyte
.
-
close
public void close()Description copied from interface:Closeable
Closes this entity and all related blocks- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classOutputStream
-
getStatus
Description copied from interface:Closeable
Provides a status of this entity -
unwind
public void unwind()This should be called if operation has been aborted and must be set back. It is done automatically if safe mode operations are used, like those started withor #safeWriter(BlockAllocator, long, Consumer)
-
commit
public void commit()Synchronizes the written data with the storage- Specified by:
commit
in interfaceCommittable
-