Package com.github.jaiimageio.stream
Class FileChannelImageOutputStream
- java.lang.Object
-
- javax.imageio.stream.ImageInputStreamImpl
-
- javax.imageio.stream.ImageOutputStreamImpl
-
- com.github.jaiimageio.stream.FileChannelImageOutputStream
-
- All Implemented Interfaces:
java.io.Closeable
,java.io.DataInput
,java.io.DataOutput
,java.lang.AutoCloseable
,javax.imageio.stream.ImageInputStream
,javax.imageio.stream.ImageOutputStream
public class FileChannelImageOutputStream extends javax.imageio.stream.ImageOutputStreamImpl
A class which implementsImageOutputStream
using aFileChannel
as the eventual data destination.Memory mapping is used for reading and direct buffers for writing. 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:
ImageOutputStream
,java.nio
,FileChannel
-
-
Field Summary
Fields Modifier and Type Field Description private java.nio.ByteBuffer
byteBuffer
AByteBuffer
used for writing to the channel.private java.nio.channels.FileChannel
channel
TheFileChannel
data destination.private static int
DEFAULT_WRITE_BUFFER_SIZE
The size of the write buffer.private javax.imageio.stream.ImageInputStream
readStream
AnImageInputStream
used for reading.
-
Constructor Summary
Constructors Constructor Description FileChannelImageOutputStream(java.nio.channels.FileChannel channel)
Constructs aFileChannelImageOutputStream
from aFileChannel
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Invokes the superclass method, writes any unwritten data, and sets the internal reference to the sourceFileChannel
tonull
.private void
flushBuffer()
Write to theFileChannel
any remaining bytes in the byte output buffer.private javax.imageio.stream.ImageInputStream
getImageInputStream()
Returns anImageInputStream
for reading.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, writes any unwritten data, and sets the channel position to the supplied parameter.void
setByteOrder(java.nio.ByteOrder networkByteOrder)
void
write(byte[] b, int off, int len)
void
write(int b)
void
writeChars(char[] c, int off, int len)
void
writeDoubles(double[] d, int off, int len)
void
writeFloats(float[] f, int off, int len)
void
writeInts(int[] i, int off, int len)
void
writeLongs(long[] l, int off, int len)
void
writeShorts(short[] s, int off, int len)
-
Methods inherited from class javax.imageio.stream.ImageOutputStreamImpl
flushBits, write, writeBit, writeBits, writeBoolean, writeByte, writeBytes, writeChar, writeChars, writeDouble, writeFloat, writeInt, writeLong, writeShort, writeUTF
-
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
-
Methods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface javax.imageio.stream.ImageInputStream
flush, 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 Detail
-
DEFAULT_WRITE_BUFFER_SIZE
private static final int DEFAULT_WRITE_BUFFER_SIZE
The size of the write buffer.- See Also:
- Constant Field Values
-
channel
private java.nio.channels.FileChannel channel
TheFileChannel
data destination.
-
byteBuffer
private java.nio.ByteBuffer byteBuffer
AByteBuffer
used for writing to the channel.
-
readStream
private javax.imageio.stream.ImageInputStream readStream
AnImageInputStream
used for reading.
-
-
Constructor Detail
-
FileChannelImageOutputStream
public FileChannelImageOutputStream(java.nio.channels.FileChannel channel) throws java.io.IOException
Constructs aFileChannelImageOutputStream
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 destinationFileChannel
.- Throws:
java.lang.IllegalArgumentException
- ifchannel
isnull
or is not open.java.io.IOException
- if a method invoked onchannel
throws anIOException
.
-
-
Method Detail
-
getImageInputStream
private javax.imageio.stream.ImageInputStream getImageInputStream() throws java.io.IOException
Returns anImageInputStream
for reading. The returned stream has byte order, stream and flushed positions, and bit offset set to the current values for this stream.- Throws:
java.io.IOException
-
flushBuffer
private void flushBuffer() throws java.io.IOException
Write to theFileChannel
any remaining bytes in the byte output buffer.- Throws:
java.io.IOException
-
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[] b, int off, int len) throws java.io.IOException
- Specified by:
read
in interfacejavax.imageio.stream.ImageInputStream
- Specified by:
read
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Specified by:
write
in interfacejavax.imageio.stream.ImageOutputStream
- Specified by:
write
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException
- Specified by:
write
in interfacejava.io.DataOutput
- Specified by:
write
in interfacejavax.imageio.stream.ImageOutputStream
- Specified by:
write
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
readFully
public void readFully(char[] c, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readFully
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readFully
public void readFully(short[] s, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readFully
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readFully
public void readFully(int[] i, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readFully
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readFully
public void readFully(long[] l, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readFully
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readFully
public void readFully(float[] f, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readFully
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
readFully
public void readFully(double[] d, int off, int len) throws java.io.IOException
- Specified by:
readFully
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
readFully
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
writeChars
public void writeChars(char[] c, int off, int len) throws java.io.IOException
- Specified by:
writeChars
in interfacejavax.imageio.stream.ImageOutputStream
- Overrides:
writeChars
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
writeShorts
public void writeShorts(short[] s, int off, int len) throws java.io.IOException
- Specified by:
writeShorts
in interfacejavax.imageio.stream.ImageOutputStream
- Overrides:
writeShorts
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
writeInts
public void writeInts(int[] i, int off, int len) throws java.io.IOException
- Specified by:
writeInts
in interfacejavax.imageio.stream.ImageOutputStream
- Overrides:
writeInts
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
writeLongs
public void writeLongs(long[] l, int off, int len) throws java.io.IOException
- Specified by:
writeLongs
in interfacejavax.imageio.stream.ImageOutputStream
- Overrides:
writeLongs
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
writeFloats
public void writeFloats(float[] f, int off, int len) throws java.io.IOException
- Specified by:
writeFloats
in interfacejavax.imageio.stream.ImageOutputStream
- Overrides:
writeFloats
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
writeDoubles
public void writeDoubles(double[] d, int off, int len) throws java.io.IOException
- Specified by:
writeDoubles
in interfacejavax.imageio.stream.ImageOutputStream
- Overrides:
writeDoubles
in classjavax.imageio.stream.ImageOutputStreamImpl
- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOException
Invokes the superclass method, writes any unwritten data, and sets the internal reference to the sourceFileChannel
tonull
. The sourceFileChannel
is not closed.- 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
- if an error occurs.
-
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 interfacejavax.imageio.stream.ImageInputStream
- Overrides:
length
in classjavax.imageio.stream.ImageInputStreamImpl
- Returns:
- The number of bytes in the channel -1L to indicate unknown length.
-
seek
public void seek(long pos) throws java.io.IOException
Invokes the superclass method, writes any unwritten data, and sets the channel position to the supplied parameter.- Specified by:
seek
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
seek
in classjavax.imageio.stream.ImageInputStreamImpl
- Throws:
java.io.IOException
-
setByteOrder
public void setByteOrder(java.nio.ByteOrder networkByteOrder)
- Specified by:
setByteOrder
in interfacejavax.imageio.stream.ImageInputStream
- Overrides:
setByteOrder
in classjavax.imageio.stream.ImageInputStreamImpl
-
-