Class FileChannelImageInputStream

  • All Implemented Interfaces:
    java.io.Closeable, java.io.DataInput, java.lang.AutoCloseable, javax.imageio.stream.ImageInputStream

    public class FileChannelImageInputStream
    extends javax.imageio.stream.ImageInputStreamImpl
    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 Buffers 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:
    ImageInputStream, java.nio, FileChannel
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private java.nio.channels.FileChannel channel
      The FileChannel data source.
      private java.nio.MappedByteBuffer mappedBuffer
      A memory mapping of all or part of the channel.
      private long mappedPos
      The stream position of the mapping.
      private long mappedUpperBound
      The stream position least upper bound of the mapping.
      • Fields inherited from class javax.imageio.stream.ImageInputStreamImpl

        bitOffset, byteOrder, flushedPos, streamPos
    • Constructor Summary

      Constructors 
      Constructor Description
      FileChannelImageInputStream​(java.nio.channels.FileChannel channel)
      Constructs a FileChannelImageInputStream from a FileChannel.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Invokes the superclass method and sets the internal reference to the source FileChannel to null.
      private java.nio.MappedByteBuffer getMappedBuffer​(int len)
      Returns a MappedByteBuffer which memory maps at least from the channel position corresponding to the current stream position to len bytes beyond.
      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 and sets the position within the memory mapped buffer.
      void setByteOrder​(java.nio.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
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • channel

        private java.nio.channels.FileChannel channel
        The FileChannel data source.
      • mappedBuffer

        private java.nio.MappedByteBuffer mappedBuffer
        A memory mapping of all or part of the channel.
      • mappedPos

        private long mappedPos
        The stream position of the mapping.
      • mappedUpperBound

        private long mappedUpperBound
        The stream position least upper bound of the mapping.
    • Constructor Detail

      • FileChannelImageInputStream

        public FileChannelImageInputStream​(java.nio.channels.FileChannel channel)
                                    throws java.io.IOException
        Constructs a FileChannelImageInputStream 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 source 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

      • getMappedBuffer

        private java.nio.MappedByteBuffer getMappedBuffer​(int len)
                                                   throws java.io.IOException
        Returns a MappedByteBuffer which memory maps at least from the channel position corresponding to the current stream position to len bytes beyond. A new buffer is created only if necessary.
        Parameters:
        len - The number of bytes required beyond the current stream position.
        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
      • close

        public void close()
                   throws java.io.IOException
        Invokes the superclass method 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.
      • 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
      • 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 and sets the position within the memory mapped buffer. A new region is mapped if necessary. The position of the source FileChannel is not changed, i.e., FileChannel.position(long) is not invoked.
        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