MemoryMappableByteBufferOutputStream
Class name: com.datashyft.core.model.util. MemoryMappableByteBufferOutputStream
This class implements an OutputStream in which the data is written either into memory or into a temporary file, depending on the specified size of the data. The data written to this OutputStream can be retrieved using the toByteBuffer() method. This method will return a ByteBuffer containing all the data written to the Output Stream. The returned buffer will be positioned at the beginning.
The Output Stream supports a maximum size of Integer.MAX_VALUE, or approximately 2GB. Specifying a larger size on the constructor will cause it to throw an IOException. Furthermore, attempting to write a total of more than 2GB of data to the output stream will result in an exception on the offending write operation.
public MemoryMappableByteBufferOutputStream(long size)
Creates a new MemoryMappableByteBufferOutputStream with the estimated total size. This size is used to configure the operational mode of the output stream. If the given size is 512KB or less, the data will be stored in memory and converted to a ByteBuffer when needed. If the size is greater than 512KB, the data will be written to a temporary file and converted to a Memory Mapped ByteBuffer when needed. In either case, the size is merely an estimate, and it is acceptable to write more or less data to this output stream as needed.
The Output Stream will not switch between memory and temp file modes once it has been created. If you specify a size of 512KB or less but write more than that amount to the output stream, all of the data will be stored in memory. If you specify a size greater than 512KB, but write less, it will be written to a temporary file and memory mapped.
Specifying a size less than 0 will cause the object to store the data on disk. Specifying a size of 0 will cause the object to store the data in memory (under the assumption that there is no data).
Parameters:
size - the estimated amount of data this Output Stream will have written to it.
Throws:
IOException – If the size is larger than Integer.MAX_VALUE.
public ByteBuffer toByteBuffer()
Returns the data written into this OutputStream as a ByteBuffer. ByteBuffers are limited to Integer.MAX_VALUE bytes in size (about 2GB)
Returns:
A ByteBuffer containing the data written to this OutputStream
Throws:
IllegalStateException - If the data written into this OutputStream is too large to fit in a ByteBuffer