Class InMemoryOutputFile

java.lang.Object
dev.hardwood.AbstractOutputFile
dev.hardwood.InMemoryOutputFile
All Implemented Interfaces:
OutputFile, Closeable, AutoCloseable

@Experimental public final class InMemoryOutputFile extends AbstractOutputFile

An OutputFile that keeps the file in memory and hands it back.

Use it to produce Parquet bytes without a filesystem: a file to upload to an object store, a payload to send over a network, or one written and read back in the same process. It is created with OutputFile.inMemory(), and the buffer buffer() returns can be passed straight to InputFile.of(ByteBuffer):

InMemoryOutputFile out = OutputFile.inMemory();
try (ParquetFileWriter writer = ParquetFileWriter.create(out, schema)) {
    // write the file
}
try (ParquetFileReader reader = ParquetFileReader.open(InputFile.of(out.buffer()))) {
    // read it back
}

The whole file is held on the heap, which costs the size of the finished file in addition to the memory the writer uses for the row group it has open. A file can be at most Integer.MAX_VALUE - 8 bytes; a write past that fails with an IOException.

This API is Experimental: the shape may change in future releases.

  • Method Details

    • doWrite

      protected void doWrite(ByteBuffer data) throws IOException
      Description copied from class: AbstractOutputFile
      Appends the remaining bytes of data, consuming the buffer. Returning with bytes left in data makes write throw IllegalStateException, since position() would no longer match the file.
      Specified by:
      doWrite in class AbstractOutputFile
      Parameters:
      data - the bytes to append
      Throws:
      IOException - if the write fails
    • doDiscard

      protected void doDiscard()
      Description copied from class: AbstractOutputFile
      Releases the resources and throws away what was written, leaving the destination as if nothing was written. Releases what it can before it throws, since it is not called again. Does nothing by default.
      Overrides:
      doDiscard in class AbstractOutputFile
    • buffer

      public ByteBuffer buffer()

      The file that was written.

      The returned buffer contains the whole file: its position is 0 and its limit is the number of bytes written, which is the range InputFile.of(ByteBuffer) reads, so it can be passed there unchanged. Each call returns a buffer with its own position and limit, so reading one buffer does not affect another. Do not modify the buffer's contents: whether a change shows in the buffers other calls return is unspecified.

      Returns:
      the bytes of the finished file
      Throws:
      IllegalStateException - if the file has not been closed, or was discarded