Class MappedByteBufferInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable
InputStream implementation based on an underlying FileChannel's memory mapped ByteBuffer,
supporting mark and reset().
Implementation allows full memory mapped ByteBuffer coverage via FileChannel
beyond its size limitation of Integer.MAX_VALUE utilizing an array of ByteBuffer slices.
Implementation further allows full random access via position() and position(long)
and accessing the memory mapped ByteBuffer slices directly via currentSlice() and nextSlice().
- Since:
- 2.3.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumstatic interfaceFile resize interface allowing a file to change its size, e.g. -
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionMappedByteBufferInputStream(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
Modifier and TypeMethodDescriptionfinal intSeeremaining()for an accurate variant.final voidclose()final ByteBufferReturn the mappedByteBufferslice at the currentposition().final voidflush(boolean metaData) Similar toOutputStream.flush(), synchronizes all mapped buffers from local storage viaMappedByteBuffer.force()as well as theFileChannel.force(boolean)w/ometaData.Return the usedMappedByteBufferInputStream.CacheMode.getOutputStream(MappedByteBufferInputStream.FileResizeOp fileResizeOp) Returns a new MappedByteBufferOutputStream instance sharing all resources of this input stream, including all buffer slices.final booleanReturnsynchronous mode.final longlength()Returns the total size in bytes of theInputStreamfinal voidmark(int readlimit) final booleanfinal ByteBufferReturn the next mappedByteBufferslice from the currentposition(), implicitly settingposition(long)to the start of the returned next slice, seecurrentSlice().final voidnotifyLengthChange(long newTotalSize) Notify this instance that the underlyingFileChannel's size has been changed and adjusting this instances buffer slices and states accordingly.final longposition()Returns the absolute position of theInputStream.position(long newPosition) Sets the absolute position of theInputStreamtonewPosition.final intread()final intread(byte[] b, int off, int len) final intread(ByteBuffer b, int len) Perform similar toread(byte[], int, int)withByteBufferinstead of byte array.final longReturns the number of remaining available bytes of theInputStream, i.e.final voidreset()final voidsetFileResizeOp(MappedByteBufferInputStream.FileResizeOp fileResizeOp) final voidsetLength(long newTotalSize) Resize the underlyingFileChannel's size and adjusting this instance viaaccordingly.final voidsetSynchronous(boolean s) Enable or disable synchronous mode.final longskip(long n) Methods inherited from class java.io.InputStream
nullInputStream, read, readAllBytes, readNBytes, readNBytes, skipNBytes, transferTo
-
Field Details
-
DEFAULT_SLICE_SHIFT
public static final int DEFAULT_SLICE_SHIFTDefault 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 Details
-
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
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 Details
-
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
- 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
Resize 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
Notify 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
Similar 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
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
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.
-
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
Returns 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
Seeremaining()for an accurate variant.- Overrides:
availablein classInputStream- Throws:
IOException- if a buffer slice operation failed.
-
position
Returns the absolute position of theInputStream.0 <=position()<=length()- Throws:
IOException- if a buffer slice operation failed.
-
position
- 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
- Overrides:
resetin classInputStream- Throws:
IOException- if this stream has not been marked, a buffer slice operation failed or stream has beenclosed.
-
skip
- Overrides:
skipin classInputStream- Throws:
IOException- if a buffer slice operation failed or stream isclosed.
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
- Overrides:
readin classInputStream- Throws:
IOException
-
read
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.
-