Class ByteBufferInputStream

java.lang.Object
java.io.InputStream
it.unimi.dsi.fastutil.io.MeasurableInputStream
it.unimi.dsi.io.ByteBufferInputStream
All Implemented Interfaces:
it.unimi.dsi.fastutil.io.MeasurableStream, it.unimi.dsi.fastutil.io.RepositionableStream, Closeable, AutoCloseable

public class ByteBufferInputStream extends it.unimi.dsi.fastutil.io.MeasurableInputStream implements it.unimi.dsi.fastutil.io.RepositionableStream
A bridge between byte buffers and input streams.

Java's memory-mapping facilities have the severe limitation of mapping at most Integer.MAX_VALUE bytes, as they expose the content of a file using a MappedByteBuffer. This class can expose a file of arbitrary length as a repositionable MeasurableInputStream that is actually based on an array of MappedByteBuffers, each mapping a chunk of CHUNK_SIZE bytes.

Since:
1.2
Author:
Sebastiano Vigna
  • Field Details

  • Constructor Details

    • ByteBufferInputStream

      public ByteBufferInputStream(ByteBuffer byteBuffer)
      Creates a new byte-buffer input stream from a single ByteBuffer.
      Parameters:
      byteBuffer - the underlying byte buffer.
    • ByteBufferInputStream

      protected ByteBufferInputStream(ByteBuffer[] byteBuffer, long size, int curr, boolean[] readyToUse)
      Creates a new byte-buffer input stream.
      Parameters:
      byteBuffer - the underlying byte buffers.
      size - the sum of the capacities of the byte buffers.
      curr - the current buffer (reading will start at this buffer from its current position).
      readyToUse - an array parallel to byteBuffer specifying which buffers do not need to be duplicated before being used (the process will happen lazily); the array will be used internally by the newly created byte-buffer input stream.
  • Method Details

    • map

      public static ByteBufferInputStream map(FileChannel fileChannel) throws IOException
      Creates a new read-only byte-buffer input stream by mapping a given file channel.
      Parameters:
      fileChannel - the file channel that will be mapped.
      Returns:
      a new read-only byte-buffer input stream over the contents of fileChannel.
      Throws:
      IOException
    • map

      public static ByteBufferInputStream map(FileChannel fileChannel, FileChannel.MapMode mapMode) throws IOException
      Creates a new byte-buffer input stream by mapping a given file channel.
      Parameters:
      fileChannel - the file channel that will be mapped.
      mapMode - this must be FileChannel.MapMode.READ_ONLY.
      Returns:
      a new byte-buffer input stream over the contents of fileChannel.
      Throws:
      IOException
    • available

      public int available()
      Overrides:
      available in class InputStream
    • markSupported

      public boolean markSupported()
      Overrides:
      markSupported in class InputStream
    • mark

      public void mark(int unused)
      Overrides:
      mark in class InputStream
    • reset

      public void reset() throws IOException
      Overrides:
      reset in class InputStream
      Throws:
      IOException
    • skip

      public long skip(long n) throws IOException
      Overrides:
      skip in class InputStream
      Throws:
      IOException
    • read

      public int read()
      Specified by:
      read in class InputStream
    • read

      public int read(byte[] b, int offset, int length)
      Overrides:
      read in class InputStream
    • length

      public long length()
      Specified by:
      length in interface it.unimi.dsi.fastutil.io.MeasurableStream
    • position

      public long position()
      Specified by:
      position in interface it.unimi.dsi.fastutil.io.MeasurableStream
      Specified by:
      position in interface it.unimi.dsi.fastutil.io.RepositionableStream
    • position

      public void position(long newPosition)
      Specified by:
      position in interface it.unimi.dsi.fastutil.io.RepositionableStream
    • copy

      public ByteBufferInputStream copy()