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:
MeasurableStream,RepositionableStream,Closeable,AutoCloseable
public class ByteBufferInputStream extends MeasurableInputStream implements RepositionableStream
A bridge between byte buffers and input streams.Java's memory-mapping facilities have the severe limitation of mapping at most
Integer.MAX_VALUEbytes, as they expose the content of a file using aMappedByteBuffer. This class can expose a file of arbitrary length as a repositionableMeasurableInputStreamthat is actually based on an array ofMappedByteBuffers, each mapping a chunk ofCHUNK_SIZEbytes.- Since:
- 1.2
- Author:
- Sebastiano Vigna
-
-
Field Summary
Fields Modifier and Type Field Description static longCHUNK_SIZEThe size of a chunk created bymap(FileChannel, FileChannel.MapMode).
-
Constructor Summary
Constructors Modifier Constructor Description ByteBufferInputStream(ByteBuffer byteBuffer)Creates a new byte-buffer input stream from a singleByteBuffer.protectedByteBufferInputStream(ByteBuffer[] byteBuffer, long size, int curr, boolean[] readyToUse)Creates a new byte-buffer input stream.
-
Method Summary
Modifier and Type Method Description intavailable()ByteBufferInputStreamcopy()longlength()static ByteBufferInputStreammap(FileChannel fileChannel)Creates a new read-only byte-buffer input stream by mapping a given file channel.static ByteBufferInputStreammap(FileChannel fileChannel, FileChannel.MapMode mapMode)Creates a new byte-buffer input stream by mapping a given file channel.voidmark(int unused)booleanmarkSupported()longposition()voidposition(long newPosition)intread()intread(byte[] b, int offset, int length)voidreset()longskip(long n)-
Methods inherited from class java.io.InputStream
close, nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
-
-
-
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(ByteBuffer byteBuffer)
Creates a new byte-buffer input stream from a singleByteBuffer.- 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 tobyteBufferspecifying 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(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 beFileChannel.MapMode.READ_ONLY.- Returns:
- a new byte-buffer input stream over the contents of
fileChannel. - Throws:
IOException
-
available
public int available()
- Overrides:
availablein classInputStream
-
markSupported
public boolean markSupported()
- Overrides:
markSupportedin classInputStream
-
mark
public void mark(int unused)
- Overrides:
markin classInputStream
-
reset
public void reset() throws IOException- Overrides:
resetin classInputStream- Throws:
IOException
-
skip
public long skip(long n) throws IOException- Overrides:
skipin classInputStream- Throws:
IOException
-
read
public int read()
- Specified by:
readin classInputStream
-
read
public int read(byte[] b, int offset, int length)- Overrides:
readin classInputStream
-
length
public long length()
- Specified by:
lengthin interfaceMeasurableStream
-
position
public long position()
- Specified by:
positionin interfaceMeasurableStream- Specified by:
positionin interfaceRepositionableStream
-
position
public void position(long newPosition)
- Specified by:
positionin interfaceRepositionableStream
-
copy
public ByteBufferInputStream copy()
-
-