Package it.unimi.dsi.io
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
,java.io.Closeable
,java.lang.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 aMappedByteBuffer
. This class can expose a file of arbitrary length as a repositionableMeasurableInputStream
that is actually based on an array ofMappedByteBuffer
s, each mapping a chunk ofCHUNK_SIZE
bytes.- Since:
- 1.2
- Author:
- Sebastiano Vigna
-
-
Field Summary
Fields Modifier and Type Field Description static long
CHUNK_SIZE
The size of a chunk created bymap(FileChannel, FileChannel.MapMode)
.
-
Constructor Summary
Constructors Modifier Constructor Description ByteBufferInputStream(java.nio.ByteBuffer byteBuffer)
Creates a new byte-buffer input stream from a singleByteBuffer
.protected
ByteBufferInputStream(java.nio.ByteBuffer[] byteBuffer, long size, int curr, boolean[] readyToUse)
Creates a new byte-buffer input stream.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description int
available()
ByteBufferInputStream
copy()
long
length()
static ByteBufferInputStream
map(java.nio.channels.FileChannel fileChannel)
Creates a new read-only byte-buffer input stream by mapping a given file channel.static ByteBufferInputStream
map(java.nio.channels.FileChannel fileChannel, java.nio.channels.FileChannel.MapMode mapMode)
Creates a new byte-buffer input stream by mapping a given file channel.void
mark(int unused)
boolean
markSupported()
long
position()
void
position(long newPosition)
int
read()
int
read(byte[] b, int offset, int length)
void
reset()
long
skip(long n)
-
-
-
Field Detail
-
CHUNK_SIZE
public static final long CHUNK_SIZE
The size of a chunk created bymap(FileChannel, FileChannel.MapMode)
.
-
-
Constructor Detail
-
ByteBufferInputStream
public ByteBufferInputStream(java.nio.ByteBuffer byteBuffer)
Creates a new byte-buffer input stream from a singleByteBuffer
.- Parameters:
byteBuffer
- the underlying byte buffer.
-
ByteBufferInputStream
protected ByteBufferInputStream(java.nio.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 tobyteBuffer
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 Detail
-
map
public static ByteBufferInputStream map(java.nio.channels.FileChannel fileChannel) throws java.io.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:
java.io.IOException
-
map
public static ByteBufferInputStream map(java.nio.channels.FileChannel fileChannel, java.nio.channels.FileChannel.MapMode mapMode) throws java.io.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 beFileChannel.MapMode.READ_ONLY
.- Returns:
- a new byte-buffer input stream over the contents of
fileChannel
. - Throws:
java.io.IOException
-
available
public int available()
- Overrides:
available
in classjava.io.InputStream
-
markSupported
public boolean markSupported()
- Overrides:
markSupported
in classjava.io.InputStream
-
mark
public void mark(int unused)
- Overrides:
mark
in classjava.io.InputStream
-
reset
public void reset() throws java.io.IOException
- Overrides:
reset
in classjava.io.InputStream
- Throws:
java.io.IOException
-
skip
public long skip(long n) throws java.io.IOException
- Overrides:
skip
in classjava.io.InputStream
- Throws:
java.io.IOException
-
read
public int read()
- Specified by:
read
in classjava.io.InputStream
-
read
public int read(byte[] b, int offset, int length)
- Overrides:
read
in classjava.io.InputStream
-
length
public long length()
- Specified by:
length
in interfaceit.unimi.dsi.fastutil.io.MeasurableStream
-
position
public long position()
- Specified by:
position
in interfaceit.unimi.dsi.fastutil.io.MeasurableStream
- Specified by:
position
in interfaceit.unimi.dsi.fastutil.io.RepositionableStream
-
position
public void position(long newPosition)
- Specified by:
position
in interfaceit.unimi.dsi.fastutil.io.RepositionableStream
-
copy
public ByteBufferInputStream copy()
-
-