Package com.twelvemonkeys.imageio.stream
Class BufferedChannelImageInputStream
- java.lang.Object
-
- javax.imageio.stream.ImageInputStreamImpl
-
- com.twelvemonkeys.imageio.stream.BufferedChannelImageInputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataInput
,java.lang.AutoCloseable
,javax.imageio.stream.ImageInputStream
final class BufferedChannelImageInputStream extends javax.imageio.stream.ImageInputStreamImpl
A bufferedImageInputStream
that is backed by aSeekableByteChannel
and provides greatly improved performance compared toFileCacheImageInputStream
orMemoryCacheImageInputStream
for shorter reads, like single byte or bit reads.
-
-
Field Summary
Fields Modifier and Type Field Description private byte[]
buffer
private int
bufferLimit
private int
bufferPos
private java.nio.ByteBuffer
byteBuffer
private java.nio.channels.SeekableByteChannel
channel
private java.io.Closeable
closeable
private static java.io.Closeable
CLOSEABLE_STUB
(package private) static int
DEFAULT_BUFFER_SIZE
private java.nio.ByteBuffer
integralCache
private byte[]
integralCacheArray
-
Constructor Summary
Constructors Modifier Constructor Description (package private)
BufferedChannelImageInputStream(Cache cache)
Constructs aBufferedChannelImageInputStream
that will read from a givenCache
.BufferedChannelImageInputStream(java.io.File file)
Constructs aBufferedChannelImageInputStream
that will read from a givenFile
.BufferedChannelImageInputStream(java.io.FileInputStream inputStream)
Constructs aBufferedChannelImageInputStream
that will read from a givenFileInputStream
.BufferedChannelImageInputStream(java.io.RandomAccessFile file)
Constructs aBufferedChannelImageInputStream
that will read from a givenRandomAccessFile
.BufferedChannelImageInputStream(java.nio.channels.SeekableByteChannel channel)
Constructs aBufferedChannelImageInputStream
that will read from a givenSeekableByteChannel
.private
BufferedChannelImageInputStream(java.nio.channels.SeekableByteChannel channel, boolean closeChannelOnClose)
BufferedChannelImageInputStream(java.nio.file.Path file)
Constructs aBufferedChannelImageInputStream
that will read from a givenPath
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private boolean
bufferEmpty()
void
close()
private boolean
fillBuffer()
void
flushBefore(long pos)
long
length()
int
read()
int
read(byte[] bytes, int offset, int length)
private int
readBuffered(byte[] bytes, int offset, int length)
private int
readDirect(byte[] bytes, int offset, int length)
int
readInt()
long
readLong()
short
readShort()
void
seek(long position)
void
setByteOrder(java.nio.ByteOrder byteOrder)
-
Methods inherited from class javax.imageio.stream.ImageInputStreamImpl
checkClosed, finalize, flush, getBitOffset, getByteOrder, getFlushedPosition, getStreamPosition, isCached, isCachedFile, isCachedMemory, mark, read, readBit, readBits, readBoolean, readByte, readBytes, readChar, readDouble, readFloat, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readFully, readLine, readUnsignedByte, readUnsignedInt, readUnsignedShort, readUTF, reset, setBitOffset, skipBytes, skipBytes
-
-
-
-
Field Detail
-
CLOSEABLE_STUB
private static final java.io.Closeable CLOSEABLE_STUB
-
DEFAULT_BUFFER_SIZE
static final int DEFAULT_BUFFER_SIZE
- See Also:
- Constant Field Values
-
byteBuffer
private java.nio.ByteBuffer byteBuffer
-
buffer
private byte[] buffer
-
bufferPos
private int bufferPos
-
bufferLimit
private int bufferLimit
-
integralCache
private final java.nio.ByteBuffer integralCache
-
integralCacheArray
private final byte[] integralCacheArray
-
channel
private java.nio.channels.SeekableByteChannel channel
-
closeable
private java.io.Closeable closeable
-
-
Constructor Detail
-
BufferedChannelImageInputStream
public BufferedChannelImageInputStream(java.io.File file) throws java.io.IOException
Constructs aBufferedChannelImageInputStream
that will read from a givenFile
.- Parameters:
file
- aFile
to read from.- Throws:
java.lang.IllegalArgumentException
- iffile
isnull
.java.lang.SecurityException
- if a security manager is installed, and it denies read access to the file.java.io.IOException
- if an I/O error occurs while opening the file.
-
BufferedChannelImageInputStream
public BufferedChannelImageInputStream(java.nio.file.Path file) throws java.io.IOException
Constructs aBufferedChannelImageInputStream
that will read from a givenPath
.- Parameters:
file
- aPath
to read from.- Throws:
java.lang.IllegalArgumentException
- iffile
isnull
.java.lang.UnsupportedOperationException
- if thefile
is associated with a provider that does not support creating file channels.java.io.IOException
- if an I/O error occurs while opening the file.java.lang.SecurityException
- if a security manager is installed, and it denies read access to the file.
-
BufferedChannelImageInputStream
public BufferedChannelImageInputStream(java.io.RandomAccessFile file)
Constructs aBufferedChannelImageInputStream
that will read from a givenRandomAccessFile
.- Parameters:
file
- aRandomAccessFile
to read from.- Throws:
java.lang.IllegalArgumentException
- iffile
isnull
.
-
BufferedChannelImageInputStream
public BufferedChannelImageInputStream(java.io.FileInputStream inputStream)
Constructs aBufferedChannelImageInputStream
that will read from a givenFileInputStream
.Closing this stream will not close the
FileInputStream
.- Parameters:
inputStream
- aFileInputStream
to read from.- Throws:
java.lang.IllegalArgumentException
- ifinputStream
isnull
.
-
BufferedChannelImageInputStream
public BufferedChannelImageInputStream(java.nio.channels.SeekableByteChannel channel)
Constructs aBufferedChannelImageInputStream
that will read from a givenSeekableByteChannel
.Closing this stream will not close the
SeekableByteChannel
.- Parameters:
channel
- aSeekableByteChannel
to read from.- Throws:
java.lang.IllegalArgumentException
- ifchannel
isnull
.
-
BufferedChannelImageInputStream
BufferedChannelImageInputStream(Cache cache)
Constructs aBufferedChannelImageInputStream
that will read from a givenCache
.Closing this stream will close the
Cache
.- Parameters:
cache
- aSeekableByteChannel
to read from.- Throws:
java.lang.IllegalArgumentException
- ifchannel
isnull
.
-
BufferedChannelImageInputStream
private BufferedChannelImageInputStream(java.nio.channels.SeekableByteChannel channel, boolean closeChannelOnClose)
-
-
Method Detail
-
fillBuffer
private boolean fillBuffer() throws java.io.IOException
- Throws:
java.io.IOException
-
bufferEmpty
private boolean bufferEmpty()
-
setByteOrder
public void setByteOrder(java.nio.ByteOrder byteOrder)
- Specified by:
setByteOrder
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
setByteOrder
in classjavax.imageio.stream.ImageInputStreamImpl
-
read
public int read() throws java.io.IOException
- Specified by:
read
in interfacejavax.imageio.stream.ImageInputStream
- Specified by:
read
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
read
public int read(byte[] bytes, int offset, int length) throws java.io.IOException
- Specified by:
read
in interfacejavax.imageio.stream.ImageInputStream
- Specified by:
read
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readDirect
private int readDirect(byte[] bytes, int offset, int length) throws java.io.IOException
- Throws:
java.io.IOException
-
readBuffered
private int readBuffered(byte[] bytes, int offset, int length)
-
length
public long length()
- Specified by:
length
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
length
in classjavax.imageio.stream.ImageInputStreamImpl
-
close
public void close() throws java.io.IOException
- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfacejava.io.Closeable
- Specified by:
close
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
close
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readShort
public short readShort() throws java.io.IOException
- Specified by:
readShort
in interfacejava.io.DataInput
- Specified by:
readShort
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readShort
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readInt
public int readInt() throws java.io.IOException
- Specified by:
readInt
in interfacejava.io.DataInput
- Specified by:
readInt
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readInt
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readLong
public long readLong() throws java.io.IOException
- Specified by:
readLong
in interfacejava.io.DataInput
- Specified by:
readLong
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readLong
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
seek
public void seek(long position) throws java.io.IOException
- Specified by:
seek
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
seek
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
flushBefore
public void flushBefore(long pos) throws java.io.IOException
- Specified by:
flushBefore
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
flushBefore
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
-