FileDocCategorySizeDatePackage
InputStream.javaAPI DocApache Lucene 1.4.37396Tue Mar 30 00:48:06 BST 2004org.apache.lucene.store

InputStream

public abstract class InputStream 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
see
OutputStream

Fields Summary
static final int
BUFFER_SIZE
private byte[]
buffer
private char[]
chars
private long
bufferStart
private int
bufferLength
private int
bufferPosition
protected long
length
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.

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

    if (buffer != null) {
      clone.buffer = new byte[BUFFER_SIZE];
      System.arraycopy(buffer, 0, clone.buffer, 0, bufferLength);
    }

    clone.chars = null;

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

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

see
#seek(long)

    return bufferStart + bufferPosition;
  
public final longlength()
The number of bytes in the file.

    return length;
  
public final bytereadByte()
Reads and returns a single byte.

see
OutputStream#writeByte(byte)

			  // set by subclasses

             
        
    if (bufferPosition >= bufferLength)
      refill();
    return buffer[bufferPosition++];
  
public final 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
OutputStream#writeBytes(byte[],int)

    if (len < BUFFER_SIZE) {
      for (int i = 0; i < len; i++)		  // read byte-by-byte
	b[i + offset] = (byte)readByte();
    } else {					  // read all-at-once
      long start = getFilePointer();
      seekInternal(start);
      readInternal(b, offset, len);

      bufferStart = start + len;		  // adjust stream variables
      bufferPosition = 0;
      bufferLength = 0;				  // trigger refill() on read
    }
  
public final 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
OutputStream#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 final intreadInt()
Reads four bytes and returns an int.

see
OutputStream#writeInt(int)

    return ((readByte() & 0xFF) << 24) | ((readByte() & 0xFF) << 16)
         | ((readByte() & 0xFF) <<  8) |  (readByte() & 0xFF);
  
protected abstract voidreadInternal(byte[] b, int offset, int length)
Expert: implements buffer refill. Reads bytes from the current position in the input.

param
b the array to read bytes into
param
offset the offset in the array to start storing bytes
param
length the number of bytes to read

public final longreadLong()
Reads eight bytes and returns a long.

see
OutputStream#writeLong(long)

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

see
OutputStream#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 final 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
OutputStream#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 final 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;
  
private voidrefill()

    long start = bufferStart + bufferPosition;
    long end = start + BUFFER_SIZE;
    if (end > length)				  // don't read past EOF
      end = length;
    bufferLength = (int)(end - start);
    if (bufferLength == 0)
      throw new IOException("read past EOF");

    if (buffer == null)
      buffer = new byte[BUFFER_SIZE];		  // allocate buffer lazily
    readInternal(buffer, 0, bufferLength);

    bufferStart = start;
    bufferPosition = 0;
  
public final voidseek(long pos)
Sets current position in this file, where the next read will occur.

see
#getFilePointer()

    if (pos >= bufferStart && pos < (bufferStart + bufferLength))
      bufferPosition = (int)(pos - bufferStart);  // seek within buffer
    else {
      bufferStart = pos;
      bufferPosition = 0;
      bufferLength = 0;				  // trigger refill() on read()
      seekInternal(pos);
    }
  
protected abstract voidseekInternal(long pos)
Expert: implements seek. Sets current position in this file, where the next {@link #readInternal(byte[],int,int)} will occur.

see
#readInternal(byte[],int,int)