Class ByteBufferIndexInput

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, java.lang.Cloneable, RandomAccessInput
    Direct Known Subclasses:
    ByteBufferIndexInput.MultiBufferImpl, ByteBufferIndexInput.SingleBufferImpl

    public abstract class ByteBufferIndexInput
    extends IndexInput
    implements RandomAccessInput
    Base IndexInput implementation that uses an array of ByteBuffers to represent a file.

    Because Java's ByteBuffer uses an int to address the values, it's necessary to access a file greater Integer.MAX_VALUE in size using multiple byte buffers.

    For efficiency, this class requires that the buffers are a power-of-two (chunkSizePower).

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      (package private) static class  ByteBufferIndexInput.MultiBufferImpl
      This class adds offset support to ByteBufferIndexInput, which is needed for slices.
      (package private) static class  ByteBufferIndexInput.SingleBufferImpl
      Optimization of ByteBufferIndexInput for when there is only one buffer
    • Constructor Summary

      Constructors 
      Constructor Description
      ByteBufferIndexInput​(java.lang.String resourceDescription, java.nio.ByteBuffer[] buffers, long length, int chunkSizePower, ByteBufferGuard guard)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected ByteBufferIndexInput buildSlice​(java.lang.String sliceDescription, long offset, long length)
      Builds the actual sliced IndexInput (may apply extra offset in subclasses).
      private java.nio.ByteBuffer[] buildSlice​(java.nio.ByteBuffer[] buffers, long offset, long length)
      Returns a sliced view from a set of already-existing buffers: the last buffer's limit() will be correct, but you must deal with offset separately (the first buffer will not be adjusted)
      ByteBufferIndexInput clone()
      Returns a clone of this stream.
      void close()
      Closes the stream to further operations.
      long getFilePointer()
      Returns the current position in this file, where the next read will occur.
      long length()
      The number of bytes in the file.
      protected ByteBufferIndexInput newCloneInstance​(java.lang.String newResourceDescription, java.nio.ByteBuffer[] newBuffers, int offset, long length)
      Factory method that creates a suitable implementation of this class for the given ByteBuffers.
      static ByteBufferIndexInput newInstance​(java.lang.String resourceDescription, java.nio.ByteBuffer[] buffers, long length, int chunkSizePower, ByteBufferGuard guard)  
      byte readByte()
      Reads and returns a single byte.
      byte readByte​(long pos)
      Reads a byte at the given position in the file
      void readBytes​(byte[] b, int offset, int len)
      Reads a specified number of bytes into an array at the specified offset.
      int readInt()
      Reads four bytes and returns an int.
      int readInt​(long pos)
      Reads an integer at the given position in the file
      void readLELongs​(long[] dst, int offset, int length)
      Read a specified number of longs with the little endian byte order.
      long readLong()
      Reads eight bytes and returns a long.
      long readLong​(long pos)
      Reads a long at the given position in the file
      short readShort()
      Reads two bytes and returns a short.
      short readShort​(long pos)
      Reads a short at the given position in the file
      void seek​(long pos)
      Sets current position in this file, where the next read will occur.
      protected void setCurBuf​(java.nio.ByteBuffer curBuf)  
      private void setPos​(long pos, int bi)  
      ByteBufferIndexInput slice​(java.lang.String sliceDescription, long offset, long length)
      Creates a slice of this index input, with the given description, offset, and length.
      private void unsetBuffers()
      Called to remove all references to byte buffers, so we can throw AlreadyClosed on NPE.
      • Methods inherited from class java.lang.Object

        equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • EMPTY_LONGBUFFER

        private static final java.nio.LongBuffer EMPTY_LONGBUFFER
      • length

        protected final long length
      • chunkSizeMask

        protected final long chunkSizeMask
      • chunkSizePower

        protected final int chunkSizePower
      • buffers

        protected java.nio.ByteBuffer[] buffers
      • curBufIndex

        protected int curBufIndex
      • curBuf

        protected java.nio.ByteBuffer curBuf
      • curLongBufferViews

        private java.nio.LongBuffer[] curLongBufferViews
      • isClone

        protected boolean isClone
    • Constructor Detail

      • ByteBufferIndexInput

        ByteBufferIndexInput​(java.lang.String resourceDescription,
                             java.nio.ByteBuffer[] buffers,
                             long length,
                             int chunkSizePower,
                             ByteBufferGuard guard)
    • Method Detail

      • newInstance

        public static ByteBufferIndexInput newInstance​(java.lang.String resourceDescription,
                                                       java.nio.ByteBuffer[] buffers,
                                                       long length,
                                                       int chunkSizePower,
                                                       ByteBufferGuard guard)
      • setCurBuf

        protected void setCurBuf​(java.nio.ByteBuffer curBuf)
      • readBytes

        public final void readBytes​(byte[] b,
                                    int offset,
                                    int len)
                             throws java.io.IOException
        Description copied from class: DataInput
        Reads a specified number of bytes into an array at the specified offset.
        Specified by:
        readBytes in class DataInput
        Parameters:
        b - the array to read bytes into
        offset - the offset in the array to start storing bytes
        len - the number of bytes to read
        Throws:
        java.io.IOException
        See Also:
        DataOutput.writeBytes(byte[],int)
      • readLELongs

        public void readLELongs​(long[] dst,
                                int offset,
                                int length)
                         throws java.io.IOException
        Description copied from class: DataInput
        Read a specified number of longs with the little endian byte order.

        This method can be used to read longs whose bytes have been reversed at write time:

         for (long l : longs) {
           output.writeLong(Long.reverseBytes(l));
         }
         
        Overrides:
        readLELongs in class DataInput
        Throws:
        java.io.IOException
      • readInt

        public final int readInt()
                          throws java.io.IOException
        Description copied from class: DataInput
        Reads four bytes and returns an int.
        Overrides:
        readInt in class DataInput
        Throws:
        java.io.IOException
        See Also:
        DataOutput.writeInt(int)
      • seek

        public void seek​(long pos)
                  throws java.io.IOException
        Description copied from class: IndexInput
        Sets current position in this file, where the next read will occur. If this is beyond the end of the file then this will throw EOFException and then the stream is in an undetermined state.
        Specified by:
        seek in class IndexInput
        Throws:
        java.io.IOException
        See Also:
        IndexInput.getFilePointer()
      • setPos

        private void setPos​(long pos,
                            int bi)
                     throws java.io.IOException
        Throws:
        java.io.IOException
      • length

        public final long length()
        Description copied from class: IndexInput
        The number of bytes in the file.
        Specified by:
        length in class IndexInput
      • clone

        public final ByteBufferIndexInput clone()
        Description copied from class: IndexInput
        Returns a clone of this stream.

        Clones of a stream access the same data, and are positioned at the same point as the stream they were cloned from.

        Expert: Subclasses must ensure that clones may be positioned at different points in the input from each other and from the stream they were cloned from.

        Warning: Lucene never closes cloned IndexInputs, it will only call IndexInput.close() on the original object.

        If you access the cloned IndexInput after closing the original object, any readXXX methods will throw AlreadyClosedException.

        This method is NOT thread safe, so if the current IndexInput is being used by one thread while clone is called by another, disaster could strike.

        Overrides:
        clone in class IndexInput
      • slice

        public final ByteBufferIndexInput slice​(java.lang.String sliceDescription,
                                                long offset,
                                                long length)
        Creates a slice of this index input, with the given description, offset, and length. The slice is seeked to the beginning.
        Specified by:
        slice in class IndexInput
      • buildSlice

        protected ByteBufferIndexInput buildSlice​(java.lang.String sliceDescription,
                                                  long offset,
                                                  long length)
        Builds the actual sliced IndexInput (may apply extra offset in subclasses).
      • newCloneInstance

        protected ByteBufferIndexInput newCloneInstance​(java.lang.String newResourceDescription,
                                                        java.nio.ByteBuffer[] newBuffers,
                                                        int offset,
                                                        long length)
        Factory method that creates a suitable implementation of this class for the given ByteBuffers.
      • buildSlice

        private java.nio.ByteBuffer[] buildSlice​(java.nio.ByteBuffer[] buffers,
                                                 long offset,
                                                 long length)
        Returns a sliced view from a set of already-existing buffers: the last buffer's limit() will be correct, but you must deal with offset separately (the first buffer will not be adjusted)
      • close

        public final void close()
                         throws java.io.IOException
        Description copied from class: IndexInput
        Closes the stream to further operations.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
        Specified by:
        close in class IndexInput
        Throws:
        java.io.IOException
      • unsetBuffers

        private void unsetBuffers()
        Called to remove all references to byte buffers, so we can throw AlreadyClosed on NPE.