FileDocCategorySizeDatePackage
IndexInput.javaAPI DocApache Lucene 2.1.06036Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.store

IndexInput

public abstract class IndexInput extends Object implements Cloneable
Abstract base class for input from a file in a {@link Directory}. A random-access input stream. Used for all Lucene index input operations.
see
Directory

Fields Summary
private char[]
chars
Constructors Summary
Methods Summary
public java.lang.Objectclone()
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.

    IndexInput clone = null;
    try {
      clone = (IndexInput)super.clone();
    } catch (CloneNotSupportedException e) {}

    clone.chars = null;

    return clone;
  
public abstract voidclose()
Closes the stream to futher operations.

public abstract longgetFilePointer()
Returns the current position in this file, where the next read will occur.

see
#seek(long)

public abstract longlength()
The number of bytes in the file.

public abstract bytereadByte()
Reads and returns a single byte.

see
IndexOutput#writeByte(byte)

public abstract voidreadBytes(byte[] b, int offset, int len)
Reads a specified number of bytes into an array at the specified offset.

param
b the array to read bytes into
param
offset the offset in the array to start storing bytes
param
len the number of bytes to read
see
IndexOutput#writeBytes(byte[],int)

public voidreadChars(char[] buffer, int start, int length)
Reads UTF-8 encoded characters into an array.

param
buffer the array to read characters into
param
start the offset in the array to start storing characters
param
length the number of characters to read
see
IndexOutput#writeChars(String,int,int)

    final int end = start + length;
    for (int i = start; i < end; i++) {
      byte b = readByte();
      if ((b & 0x80) == 0)
	buffer[i] = (char)(b & 0x7F);
      else if ((b & 0xE0) != 0xE0) {
	buffer[i] = (char)(((b & 0x1F) << 6)
		 | (readByte() & 0x3F));
      } else
	buffer[i] = (char)(((b & 0x0F) << 12)
		| ((readByte() & 0x3F) << 6)
	        |  (readByte() & 0x3F));
    }
  
public intreadInt()
Reads four bytes and returns an int.

see
IndexOutput#writeInt(int)

    return ((readByte() & 0xFF) << 24) | ((readByte() & 0xFF) << 16)
         | ((readByte() & 0xFF) <<  8) |  (readByte() & 0xFF);
  
public longreadLong()
Reads eight bytes and returns a long.

see
IndexOutput#writeLong(long)

    return (((long)readInt()) << 32) | (readInt() & 0xFFFFFFFFL);
  
public java.lang.StringreadString()
Reads a string.

see
IndexOutput#writeString(String)

    int length = readVInt();
    if (chars == null || length > chars.length)
      chars = new char[length];
    readChars(chars, 0, length);
    return new String(chars, 0, length);
  
public intreadVInt()
Reads an int stored in variable-length format. Reads between one and five bytes. Smaller values take fewer bytes. Negative numbers are not supported.

see
IndexOutput#writeVInt(int)

    byte b = readByte();
    int i = b & 0x7F;
    for (int shift = 7; (b & 0x80) != 0; shift += 7) {
      b = readByte();
      i |= (b & 0x7F) << shift;
    }
    return i;
  
public longreadVLong()
Reads a long stored in variable-length format. Reads between one and nine bytes. Smaller values take fewer bytes. Negative numbers are not supported.

    byte b = readByte();
    long i = b & 0x7F;
    for (int shift = 7; (b & 0x80) != 0; shift += 7) {
      b = readByte();
      i |= (b & 0x7FL) << shift;
    }
    return i;
  
public abstract voidseek(long pos)
Sets current position in this file, where the next read will occur.

see
#getFilePointer()

public voidskipChars(int length)
Expert Similar to {@link #readChars(char[], int, int)} but does not do any conversion operations on the bytes it is reading in. It still has to invoke {@link #readByte()} just as {@link #readChars(char[], int, int)} does, but it does not need a buffer to store anything and it does not have to do any of the bitwise operations, since we don't actually care what is in the byte except to determine how many more bytes to read

param
length The number of chars to read

    for (int i = 0; i < length; i++) {
      byte b = readByte();
      if ((b & 0x80) == 0){
        //do nothing, we only need one byte
      }
      else if ((b & 0xE0) != 0xE0) {
        readByte();//read an additional byte
      } else{      
        //read two additional bytes.
        readByte();
        readByte();
      }
    }