Class 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 implements ImageOutputStream using a FileChannel 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
      A ByteBuffer used for writing to the channel.
      private java.nio.channels.FileChannel channel
      The FileChannel data destination.
      private static int DEFAULT_WRITE_BUFFER_SIZE
      The size of the write buffer.
      private javax.imageio.stream.ImageInputStream readStream
      An ImageInputStream used for reading.
      • Fields inherited from class javax.imageio.stream.ImageInputStreamImpl

        bitOffset, byteOrder, flushedPos, streamPos
    • Constructor Summary

      Constructors 
      Constructor Description
      FileChannelImageOutputStream​(java.nio.channels.FileChannel channel)
      Constructs a FileChannelImageOutputStream from a FileChannel.
    • 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 source FileChannel to null.
      private void flushBuffer()
      Write to the FileChannel any remaining bytes in the byte output buffer.
      private javax.imageio.stream.ImageInputStream getImageInputStream()
      Returns an ImageInputStream for reading.
      long length()
      Returns the number of bytes currently in the FileChannel.
      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
      • Methods inherited from interface javax.imageio.stream.ImageOutputStream

        flushBefore
    • 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
        The FileChannel data destination.
      • byteBuffer

        private java.nio.ByteBuffer byteBuffer
        A ByteBuffer used for writing to the channel.
      • readStream

        private javax.imageio.stream.ImageInputStream readStream
        An ImageInputStream used for reading.
    • Constructor Detail

      • FileChannelImageOutputStream

        public FileChannelImageOutputStream​(java.nio.channels.FileChannel channel)
                                     throws java.io.IOException
        Constructs a FileChannelImageOutputStream from a FileChannel. The initial position of the stream stream is taken to be the position of the FileChannel parameter when this constructor is invoked. The stream and flushed positions are therefore both initialized to channel.position().
        Parameters:
        channel - the destination FileChannel.
        Throws:
        java.lang.IllegalArgumentException - if channel is null or is not open.
        java.io.IOException - if a method invoked on channel throws an IOException.
    • Method Detail

      • getImageInputStream

        private javax.imageio.stream.ImageInputStream getImageInputStream()
                                                                   throws java.io.IOException
        Returns an ImageInputStream 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 the FileChannel any remaining bytes in the byte output buffer.
        Throws:
        java.io.IOException
      • read

        public int read()
                 throws java.io.IOException
        Specified by:
        read in interface javax.imageio.stream.ImageInputStream
        Specified by:
        read in class javax.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 interface javax.imageio.stream.ImageInputStream
        Specified by:
        read in class javax.imageio.stream.ImageInputStreamImpl
        Throws:
        java.io.IOException
      • write

        public void write​(int b)
                   throws java.io.IOException
        Specified by:
        write in interface java.io.DataOutput
        Specified by:
        write in interface javax.imageio.stream.ImageOutputStream
        Specified by:
        write in class javax.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 interface java.io.DataOutput
        Specified by:
        write in interface javax.imageio.stream.ImageOutputStream
        Specified by:
        write in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        readFully in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        readFully in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        readFully in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        readFully in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        readFully in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        readFully in class javax.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 interface javax.imageio.stream.ImageOutputStream
        Overrides:
        writeChars in class javax.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 interface javax.imageio.stream.ImageOutputStream
        Overrides:
        writeShorts in class javax.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 interface javax.imageio.stream.ImageOutputStream
        Overrides:
        writeInts in class javax.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 interface javax.imageio.stream.ImageOutputStream
        Overrides:
        writeLongs in class javax.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 interface javax.imageio.stream.ImageOutputStream
        Overrides:
        writeFloats in class javax.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 interface javax.imageio.stream.ImageOutputStream
        Overrides:
        writeDoubles in class javax.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 source FileChannel to null. The source FileChannel is not closed.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in interface javax.imageio.stream.ImageInputStream
        Overrides:
        close in class javax.imageio.stream.ImageInputStreamImpl
        Throws:
        java.io.IOException - if an error occurs.
      • length

        public long length()
        Returns the number of bytes currently in the FileChannel. If an IOException is encountered when querying the channel's size, -1L will be returned.
        Specified by:
        length in interface javax.imageio.stream.ImageInputStream
        Overrides:
        length in class javax.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 interface javax.imageio.stream.ImageInputStream
        Overrides:
        seek in class javax.imageio.stream.ImageInputStreamImpl
        Throws:
        java.io.IOException
      • setByteOrder

        public void setByteOrder​(java.nio.ByteOrder networkByteOrder)
        Specified by:
        setByteOrder in interface javax.imageio.stream.ImageInputStream
        Overrides:
        setByteOrder in class javax.imageio.stream.ImageInputStreamImpl