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.ImageOutputStreamImplA class which implementsImageOutputStreamusing aFileChannelas 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.ByteBufferbyteBufferAByteBufferused for writing to the channel.private java.nio.channels.FileChannelchannelTheFileChanneldata destination.private static intDEFAULT_WRITE_BUFFER_SIZEThe size of the write buffer.private javax.imageio.stream.ImageInputStreamreadStreamAnImageInputStreamused for reading.
-
Constructor Summary
Constructors Constructor Description FileChannelImageOutputStream(java.nio.channels.FileChannel channel)Constructs aFileChannelImageOutputStreamfrom aFileChannel.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Invokes the superclass method, writes any unwritten data, and sets the internal reference to the sourceFileChanneltonull.private voidflushBuffer()Write to theFileChannelany remaining bytes in the byte output buffer.private javax.imageio.stream.ImageInputStreamgetImageInputStream()Returns anImageInputStreamfor reading.longlength()Returns the number of bytes currently in theFileChannel.intread()intread(byte[] b, int off, int len)voidreadFully(char[] c, int off, int len)voidreadFully(double[] d, int off, int len)voidreadFully(float[] f, int off, int len)voidreadFully(int[] i, int off, int len)voidreadFully(long[] l, int off, int len)voidreadFully(short[] s, int off, int len)voidseek(long pos)Invokes the superclass method, writes any unwritten data, and sets the channel position to the supplied parameter.voidsetByteOrder(java.nio.ByteOrder networkByteOrder)voidwrite(byte[] b, int off, int len)voidwrite(int b)voidwriteChars(char[] c, int off, int len)voidwriteDoubles(double[] d, int off, int len)voidwriteFloats(float[] f, int off, int len)voidwriteInts(int[] i, int off, int len)voidwriteLongs(long[] l, int off, int len)voidwriteShorts(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
TheFileChanneldata destination.
-
byteBuffer
private java.nio.ByteBuffer byteBuffer
AByteBufferused for writing to the channel.
-
readStream
private javax.imageio.stream.ImageInputStream readStream
AnImageInputStreamused for reading.
-
-
Constructor Detail
-
FileChannelImageOutputStream
public FileChannelImageOutputStream(java.nio.channels.FileChannel channel) throws java.io.IOExceptionConstructs aFileChannelImageOutputStreamfrom aFileChannel. The initial position of the stream stream is taken to be the position of theFileChannelparameter when this constructor is invoked. The stream and flushed positions are therefore both initialized tochannel.position().- Parameters:
channel- the destinationFileChannel.- Throws:
java.lang.IllegalArgumentException- ifchannelisnullor is not open.java.io.IOException- if a method invoked onchannelthrows anIOException.
-
-
Method Detail
-
getImageInputStream
private javax.imageio.stream.ImageInputStream getImageInputStream() throws java.io.IOExceptionReturns anImageInputStreamfor 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.IOExceptionWrite to theFileChannelany remaining bytes in the byte output buffer.- Throws:
java.io.IOException
-
read
public int read() throws java.io.IOException- Specified by:
readin interfacejavax.imageio.stream.ImageInputStream- Specified by:
readin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
read
public int read(byte[] b, int off, int len) throws java.io.IOException- Specified by:
readin interfacejavax.imageio.stream.ImageInputStream- Specified by:
readin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
write
public void write(int b) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Specified by:
writein interfacejavax.imageio.stream.ImageOutputStream- Specified by:
writein classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
write
public void write(byte[] b, int off, int len) throws java.io.IOException- Specified by:
writein interfacejava.io.DataOutput- Specified by:
writein interfacejavax.imageio.stream.ImageOutputStream- Specified by:
writein classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
readFully
public void readFully(char[] c, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejavax.imageio.stream.ImageInputStream- Overrides:
readFullyin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
readFully
public void readFully(short[] s, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejavax.imageio.stream.ImageInputStream- Overrides:
readFullyin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
readFully
public void readFully(int[] i, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejavax.imageio.stream.ImageInputStream- Overrides:
readFullyin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
readFully
public void readFully(long[] l, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejavax.imageio.stream.ImageInputStream- Overrides:
readFullyin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
readFully
public void readFully(float[] f, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejavax.imageio.stream.ImageInputStream- Overrides:
readFullyin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
readFully
public void readFully(double[] d, int off, int len) throws java.io.IOException- Specified by:
readFullyin interfacejavax.imageio.stream.ImageInputStream- Overrides:
readFullyin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
writeChars
public void writeChars(char[] c, int off, int len) throws java.io.IOException- Specified by:
writeCharsin interfacejavax.imageio.stream.ImageOutputStream- Overrides:
writeCharsin classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
writeShorts
public void writeShorts(short[] s, int off, int len) throws java.io.IOException- Specified by:
writeShortsin interfacejavax.imageio.stream.ImageOutputStream- Overrides:
writeShortsin classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
writeInts
public void writeInts(int[] i, int off, int len) throws java.io.IOException- Specified by:
writeIntsin interfacejavax.imageio.stream.ImageOutputStream- Overrides:
writeIntsin classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
writeLongs
public void writeLongs(long[] l, int off, int len) throws java.io.IOException- Specified by:
writeLongsin interfacejavax.imageio.stream.ImageOutputStream- Overrides:
writeLongsin classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
writeFloats
public void writeFloats(float[] f, int off, int len) throws java.io.IOException- Specified by:
writeFloatsin interfacejavax.imageio.stream.ImageOutputStream- Overrides:
writeFloatsin classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
writeDoubles
public void writeDoubles(double[] d, int off, int len) throws java.io.IOException- Specified by:
writeDoublesin interfacejavax.imageio.stream.ImageOutputStream- Overrides:
writeDoublesin classjavax.imageio.stream.ImageOutputStreamImpl- Throws:
java.io.IOException
-
close
public void close() throws java.io.IOExceptionInvokes the superclass method, writes any unwritten data, and sets the internal reference to the sourceFileChanneltonull. The sourceFileChannelis not closed.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Specified by:
closein interfacejavax.imageio.stream.ImageInputStream- Overrides:
closein 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 anIOExceptionis encountered when querying the channel's size, -1L will be returned.- Specified by:
lengthin interfacejavax.imageio.stream.ImageInputStream- Overrides:
lengthin 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.IOExceptionInvokes the superclass method, writes any unwritten data, and sets the channel position to the supplied parameter.- Specified by:
seekin interfacejavax.imageio.stream.ImageInputStream- Overrides:
seekin classjavax.imageio.stream.ImageInputStreamImpl- Throws:
java.io.IOException
-
setByteOrder
public void setByteOrder(java.nio.ByteOrder networkByteOrder)
- Specified by:
setByteOrderin interfacejavax.imageio.stream.ImageInputStream- Overrides:
setByteOrderin classjavax.imageio.stream.ImageInputStreamImpl
-
-