Class MappedByteBufferInputStream
- java.lang.Object
-
- java.io.InputStream
-
- com.jogamp.common.nio.MappedByteBufferInputStream
-
- All Implemented Interfaces:
Closeable,AutoCloseable
public class MappedByteBufferInputStream extends InputStream
AnInputStreamimplementation based on an underlyingFileChannel's memory mappedByteBuffer,supportingmarkandreset().Implementation allows full memory mapped
ByteBuffercoverage viaFileChannelbeyond its size limitation ofInteger.MAX_VALUEutilizing an array ofByteBufferslices.
Implementation further allows full random access via
position()andposition(long)and accessing the memory mappedByteBufferslices directly viacurrentSlice()andnextSlice().- Since:
- 2.3.0
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classMappedByteBufferInputStream.CacheModestatic interfaceMappedByteBufferInputStream.FileResizeOpFile resize interface allowing a file to change its size, e.g.
-
Field Summary
Fields Modifier and Type Field Description static intDEFAULT_SLICE_SHIFTDefault slice shift, i.e.
-
Constructor Summary
Constructors Constructor Description MappedByteBufferInputStream(FileChannel fileChannel)Creates a new instance using the givenFileChannel,read-onlymapping mode,MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARDand theDEFAULT_SLICE_SHIFT.MappedByteBufferInputStream(FileChannel fileChannel, FileChannel.MapMode mmode, MappedByteBufferInputStream.CacheMode cmode)Creates a new instance using the givenFileChannel, given mapping-mode, given cache-mode and theDEFAULT_SLICE_SHIFT.MappedByteBufferInputStream(FileChannel fileChannel, FileChannel.MapMode mmode, MappedByteBufferInputStream.CacheMode cmode, int sliceShift)Creates a new instance using the givenFileChannel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intavailable()Seeremaining()for an accurate variant.voidclose()ByteBuffercurrentSlice()Return the mappedByteBufferslice at the currentposition().voidflush(boolean metaData)Similar toOutputStream.flush(), synchronizes all mapped buffers from local storage viaMappedByteBuffer.force()as well as theFileChannel.force(boolean)w/ometaData.voidflushSlices()Releases the mappedByteBufferslices.MappedByteBufferInputStream.CacheModegetCacheMode()Return the usedMappedByteBufferInputStream.CacheMode.MappedByteBufferOutputStreamgetOutputStream(MappedByteBufferInputStream.FileResizeOp fileResizeOp)Returns a new MappedByteBufferOutputStream instance sharing all resources of this input stream, including all buffer slices.booleangetSynchronous()Returnsynchronous mode.longlength()Returns the total size in bytes of theInputStreamvoidmark(int readlimit)booleanmarkSupported()ByteBuffernextSlice()Return the next mappedByteBufferslice from the currentposition(), implicitly settingposition(long)to the start of the returned next slice, seecurrentSlice().voidnotifyLengthChange(long newTotalSize)Notify this instance that the underlyingFileChannel's size has been changed and adjusting this instances buffer slices and states accordingly.longposition()Returns the absolute position of theInputStream.MappedByteBufferInputStreamposition(long newPosition)Sets the absolute position of theInputStreamtonewPosition.intread()intread(byte[] b, int off, int len)intread(ByteBuffer b, int len)Perform similar toread(byte[], int, int)withByteBufferinstead of byte array.longremaining()Returns the number of remaining available bytes of theInputStream, i.e.voidreset()voidsetFileResizeOp(MappedByteBufferInputStream.FileResizeOp fileResizeOp)voidsetLength(long newTotalSize)Resize the underlyingFileChannel's size and adjusting this instance viaaccordingly.voidsetSynchronous(boolean s)Enable or disable synchronous mode.longskip(long n)-
Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, transferTo
-
-
-
-
Field Detail
-
DEFAULT_SLICE_SHIFT
public static final int DEFAULT_SLICE_SHIFT
Default slice shift, i.e. 1L << shift, denoting slice size in MiB:64bit machines-> 30 = 1024 MiB32bit machines-> 29 = 512 MiB
In case the default is too much of-used up address-space, one may choose other values:
- 29 -> 512 MiB
- 28 -> 256 MiB
- 27 -> 128 MiB
- 26 -> 64 MiB
-
-
Constructor Detail
-
MappedByteBufferInputStream
public MappedByteBufferInputStream(FileChannel fileChannel, FileChannel.MapMode mmode, MappedByteBufferInputStream.CacheMode cmode, int sliceShift) throws IOException
Creates a new instance using the givenFileChannel.The
ByteBufferslices will be mapped lazily at first usage.- Parameters:
fileChannel- the file channel to be mapped lazily.mmode- the map mode, default isFileChannel.MapMode.READ_ONLY.cmode- the caching mode, default isMappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD.sliceShift- the pow2 slice size, default isDEFAULT_SLICE_SHIFT.- Throws:
IOException
-
MappedByteBufferInputStream
public MappedByteBufferInputStream(FileChannel fileChannel, FileChannel.MapMode mmode, MappedByteBufferInputStream.CacheMode cmode) throws IOException
Creates a new instance using the givenFileChannel, given mapping-mode, given cache-mode and theDEFAULT_SLICE_SHIFT.The
ByteBufferslices will be mapped lazily at first usage.- Parameters:
fileChannel- the file channel to be used.mmode- the map mode, default isFileChannel.MapMode.READ_ONLY.cmode- the caching mode, default isMappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD.- Throws:
IOException
-
MappedByteBufferInputStream
public MappedByteBufferInputStream(FileChannel fileChannel) throws IOException
Creates a new instance using the givenFileChannel,read-onlymapping mode,MappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARDand theDEFAULT_SLICE_SHIFT.The
ByteBufferslices will be mappedFileChannel.MapMode.READ_ONLYlazily at first usage.- Parameters:
fileChannel- the file channel to be used.- Throws:
IOException
-
-
Method Detail
-
setSynchronous
public final void setSynchronous(boolean s)
Enable or disable synchronous mode.If synchronous mode is enabled, mapped buffers will be
flushedifresized, written to orclosinginread-writemapping mode.If synchronous mode is enabled,
FileChannel.force(boolean)is issued ifresizingorclosingand not inread-onlymapping mode.- Parameters:
s-trueto enable synchronous mode
-
getSynchronous
public final boolean getSynchronous()
Returnsynchronous mode.
-
close
public final void close() throws IOException- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classInputStream- Throws:
IOException
-
setFileResizeOp
public final void setFileResizeOp(MappedByteBufferInputStream.FileResizeOp fileResizeOp) throws IllegalStateException
- Parameters:
fileResizeOp- the newMappedByteBufferInputStream.FileResizeOp.- Throws:
IllegalStateException- if attempting to set theMappedByteBufferInputStream.FileResizeOpto a different value than before
-
setLength
public final void setLength(long newTotalSize) throws IOExceptionResize the underlyingFileChannel's size and adjusting this instance viaaccordingly.User must have a
MappedByteBufferInputStream.FileResizeOpregisteredbefore.Implementation calls
notifyLengthChange(long)afterMappedByteBufferInputStream.FileResizeOp.setLength(long).- Parameters:
newTotalSize- the new total size- Throws:
IOException- if noMappedByteBufferInputStream.FileResizeOphas beenregisteredor if a buffer slice operation failed
-
notifyLengthChange
public final void notifyLengthChange(long newTotalSize) throws IOExceptionNotify this instance that the underlyingFileChannel's size has been changed and adjusting this instances buffer slices and states accordingly.Should be called by user API when aware of such event.
- Parameters:
newTotalSize- the new total size- Throws:
IOException- if a buffer slice operation failed
-
flush
public final void flush(boolean metaData) throws IOExceptionSimilar toOutputStream.flush(), synchronizes all mapped buffers from local storage viaMappedByteBuffer.force()as well as theFileChannel.force(boolean)w/ometaData.- Parameters:
metaData- TODO- Throws:
IOException- if this stream has beenclosed.
-
getOutputStream
public final MappedByteBufferOutputStream getOutputStream(MappedByteBufferInputStream.FileResizeOp fileResizeOp) throws IllegalStateException, IOException
Returns a new MappedByteBufferOutputStream instance sharing all resources of this input stream, including all buffer slices.- Throws:
IllegalStateException- if attempting to set theMappedByteBufferInputStream.FileResizeOpto a different value than beforeIOException- if this instance was opened w/FileChannel.MapMode.READ_ONLYor if this stream has beenclosed.
-
currentSlice
public final ByteBuffer currentSlice() throws IOException
Return the mappedByteBufferslice at the currentposition().Due to the nature of using sliced buffers mapping the whole region, user has to determine whether the returned buffer covers the desired region and may fetch the
nextSlice()until satisfied.
It is also possible to repeat this operation after reposition the stream viaposition(long)orskip(long)to a position within the next block, similar tonextSlice().- Throws:
IOException- if a buffer slice operation failed.
-
nextSlice
public final ByteBuffer nextSlice() throws IOException
Return the next mappedByteBufferslice from the currentposition(), implicitly settingposition(long)to the start of the returned next slice, seecurrentSlice().If no subsequent slice is available,
nullis being returned.- Throws:
IOException- if a buffer slice operation failed.
-
flushSlices
public final void flushSlices() throws IOExceptionReleases the mappedByteBufferslices.- Throws:
IOException- if a buffer slice operation failed.
-
getCacheMode
public final MappedByteBufferInputStream.CacheMode getCacheMode()
Return the usedMappedByteBufferInputStream.CacheMode.If a desired
MappedByteBufferInputStream.CacheModeis not available, it may fall back to an available one at runtime, seeMappedByteBufferInputStream.CacheMode.FLUSH_PRE_HARD.
This evaluation only happens if theMappedByteBufferInputStream.CacheMode!=MappedByteBufferInputStream.CacheMode.FLUSH_NONEand while attempting to flush an unused buffer slice.
-
length
public final long length()
Returns the total size in bytes of theInputStream0 <=position()<=length()
-
remaining
public final long remaining() throws IOExceptionReturns the number of remaining available bytes of theInputStream, i.e..length()-position()0 <=position()<=length()In contrast to
InputStream'savailable()method, this method returns the proper return typelong.- Throws:
IOException- if a buffer slice operation failed.
-
available
public final int available() throws IOExceptionSeeremaining()for an accurate variant.- Overrides:
availablein classInputStream- Throws:
IOException- if a buffer slice operation failed.
-
position
public final long position() throws IOExceptionReturns the absolute position of theInputStream.0 <=position()<=length()- Throws:
IOException- if a buffer slice operation failed.
-
position
public final MappedByteBufferInputStream position(long newPosition) throws IOException
- Parameters:
newPosition- The new position, which must be non-negative and ≤length().- Returns:
- this instance
- Throws:
IOException- if a buffer slice operation failed or stream isclosed.
-
markSupported
public final boolean markSupported()
- Overrides:
markSupportedin classInputStream
-
mark
public final void mark(int readlimit)
Parameter
readLimitis not used in this implementation, since the whole file is memory mapped and no read limitation occurs.- Overrides:
markin classInputStream
-
reset
public final void reset() throws IOException- Overrides:
resetin classInputStream- Throws:
IOException- if this stream has not been marked, a buffer slice operation failed or stream has beenclosed.
-
skip
public final long skip(long n) throws IOException- Overrides:
skipin classInputStream- Throws:
IOException- if a buffer slice operation failed or stream isclosed.
-
read
public final int read() throws IOException- Specified by:
readin classInputStream- Throws:
IOException
-
read
public final int read(byte[] b, int off, int len) throws IOException- Overrides:
readin classInputStream- Throws:
IOException
-
read
public final int read(ByteBuffer b, int len) throws IOException
Perform similar toread(byte[], int, int)withByteBufferinstead of byte array.- Parameters:
b- theByteBuffersink, data is written at currentBuffer.position()len- the number of bytes to read- Returns:
- the number of bytes read, -1 for EOS
- Throws:
IOException- if a buffer slice operation failed or stream has beenclosed.
-
-