Package com.github.jaiimageio.stream
Class FileChannelImageInputStream
java.lang.Object
javax.imageio.stream.ImageInputStreamImpl
com.github.jaiimageio.stream.FileChannelImageInputStream
- All Implemented Interfaces:
Closeable
,DataInput
,AutoCloseable
,ImageInputStream
A class which implements
ImageInputStream
using a
FileChannel
as the eventual data source. The channel
contents are assumed to be stable during the lifetime of the object.
Memory mapping and new I/O view Buffer
s are used to
read the data. Only methods which provide significant performance
improvement with respect to the superclass implementation are overridden.
Overridden methods are not commented individually unless some noteworthy
aspect of the implementation must be described.
The methods of this class are not synchronized.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate FileChannel
TheFileChannel
data source.private MappedByteBuffer
A memory mapping of all or part of the channel.private long
The stream position of the mapping.private long
The stream position least upper bound of the mapping.Fields inherited from class javax.imageio.stream.ImageInputStreamImpl
bitOffset, byteOrder, flushedPos, streamPos
-
Constructor Summary
ConstructorsConstructorDescriptionFileChannelImageInputStream
(FileChannel channel) Constructs aFileChannelImageInputStream
from aFileChannel
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
Invokes the superclass method and sets the internal reference to the sourceFileChannel
tonull
.private MappedByteBuffer
getMappedBuffer
(int len) Returns aMappedByteBuffer
which memory maps at least from the channel position corresponding to the current stream position tolen
bytes beyond.long
length()
Returns the number of bytes currently in theFileChannel
.int
read()
int
read
(byte[] b, int off, int len) void
readFully
(char[] c, int off, int len) void
readFully
(double[] d, int off, int len) void
readFully
(float[] f, int off, int len) void
readFully
(int[] i, int off, int len) void
readFully
(long[] l, int off, int len) void
readFully
(short[] s, int off, int len) void
seek
(long pos) Invokes the superclass method and sets the position within the memory mapped buffer.void
setByteOrder
(ByteOrder networkByteOrder) Methods inherited from class javax.imageio.stream.ImageInputStreamImpl
checkClosed, finalize, flush, flushBefore, getBitOffset, getByteOrder, getFlushedPosition, getStreamPosition, isCached, isCachedFile, isCachedMemory, mark, read, readBit, readBits, readBoolean, readByte, readBytes, readChar, readDouble, readFloat, readFully, readFully, readInt, readLine, readLong, readShort, readUnsignedByte, readUnsignedInt, readUnsignedShort, readUTF, reset, setBitOffset, skipBytes, skipBytes
-
Field Details
-
channel
TheFileChannel
data source. -
mappedBuffer
A memory mapping of all or part of the channel. -
mappedPos
private long mappedPosThe stream position of the mapping. -
mappedUpperBound
private long mappedUpperBoundThe stream position least upper bound of the mapping.
-
-
Constructor Details
-
FileChannelImageInputStream
Constructs aFileChannelImageInputStream
from aFileChannel
. The initial position of the stream stream is taken to be the position of theFileChannel
parameter when this constructor is invoked. The stream and flushed positions are therefore both initialized tochannel.position()
.- Parameters:
channel
- the sourceFileChannel
.- Throws:
IllegalArgumentException
- ifchannel
isnull
or is not open.IOException
- if a method invoked onchannel
throws anIOException
.
-
-
Method Details
-
getMappedBuffer
Returns aMappedByteBuffer
which memory maps at least from the channel position corresponding to the current stream position tolen
bytes beyond. A new buffer is created only if necessary.- Parameters:
len
- The number of bytes required beyond the current stream position.- Throws:
IOException
-
read
- Specified by:
read
in interfaceImageInputStream
- Specified by:
read
in classImageInputStreamImpl
- Throws:
IOException
-
read
- Specified by:
read
in interfaceImageInputStream
- Specified by:
read
in classImageInputStreamImpl
- Throws:
IOException
-
close
Invokes the superclass method and sets the internal reference to the sourceFileChannel
tonull
. The sourceFileChannel
is not closed.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Specified by:
close
in interfaceImageInputStream
- Overrides:
close
in classImageInputStreamImpl
- Throws:
IOException
- if an error occurs.
-
readFully
- Specified by:
readFully
in interfaceImageInputStream
- Overrides:
readFully
in classImageInputStreamImpl
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceImageInputStream
- Overrides:
readFully
in classImageInputStreamImpl
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceImageInputStream
- Overrides:
readFully
in classImageInputStreamImpl
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceImageInputStream
- Overrides:
readFully
in classImageInputStreamImpl
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceImageInputStream
- Overrides:
readFully
in classImageInputStreamImpl
- Throws:
IOException
-
readFully
- Specified by:
readFully
in interfaceImageInputStream
- Overrides:
readFully
in classImageInputStreamImpl
- Throws:
IOException
-
length
public long length()Returns the number of bytes currently in theFileChannel
. If anIOException
is encountered when querying the channel's size, -1L will be returned.- Specified by:
length
in interfaceImageInputStream
- Overrides:
length
in classImageInputStreamImpl
- Returns:
- The number of bytes in the channel -1L to indicate unknown length.
-
seek
Invokes the superclass method and sets the position within the memory mapped buffer. A new region is mapped if necessary. The position of the sourceFileChannel
is not changed, i.e.,FileChannel.position(long)
is not invoked.- Specified by:
seek
in interfaceImageInputStream
- Overrides:
seek
in classImageInputStreamImpl
- Throws:
IOException
-
setByteOrder
- Specified by:
setByteOrder
in interfaceImageInputStream
- Overrides:
setByteOrder
in classImageInputStreamImpl
-