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

BufferedIndexOutput

public abstract class BufferedIndexOutput extends IndexOutput
Base implementation class for buffered {@link IndexOutput}.

Fields Summary
static final int
BUFFER_SIZE
private final byte[]
buffer
private long
bufferStart
private int
bufferPosition
Constructors Summary
Methods Summary
public voidclose()
Closes this stream to further operations.

    flush();
  
public voidflush()
Forces any buffered output to be written.

    flushBuffer(buffer, bufferPosition);
    bufferStart += bufferPosition;
    bufferPosition = 0;
  
protected abstract voidflushBuffer(byte[] b, int len)
Expert: implements buffer write. Writes bytes at the current position in the output.

param
b the bytes to write
param
len the number of bytes to write

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

see
#seek(long)

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

public voidseek(long pos)
Sets current position in this file, where the next write will occur.

see
#getFilePointer()

    flush();
    bufferStart = pos;
  
public voidwriteByte(byte b)
Writes a single byte.

see
IndexInput#readByte()

		  // position in buffer

           
        
    if (bufferPosition >= BUFFER_SIZE)
      flush();
    buffer[bufferPosition++] = b;
  
public voidwriteBytes(byte[] b, int length)
Writes an array of bytes.

param
b the bytes to write
param
length the number of bytes to write
see
IndexInput#readBytes(byte[],int,int)

    int bytesLeft = BUFFER_SIZE - bufferPosition;
    // is there enough space in the buffer?
    if (bytesLeft >= length) {
      // we add the data to the end of the buffer
      System.arraycopy(b, 0, buffer, bufferPosition, length);
      bufferPosition += length;
      // if the buffer is full, flush it
      if (BUFFER_SIZE - bufferPosition == 0)
        flush();
    } else {
      // is data larger then buffer?
      if (length > BUFFER_SIZE) {
        // we flush the buffer
        if (bufferPosition > 0)
          flush();
        // and write data at once
        flushBuffer(b, length);
        bufferStart += length;
      } else {
        // we fill/flush the buffer (until the input is written)
        int pos = 0; // position in the input data
        int pieceLength;
        while (pos < length) {
          pieceLength = (length - pos < bytesLeft) ? length - pos : bytesLeft;
          System.arraycopy(b, pos, buffer, bufferPosition, pieceLength);
          pos += pieceLength;
          bufferPosition += pieceLength;
          // if the buffer is full, flush it
          bytesLeft = BUFFER_SIZE - bufferPosition;
          if (bytesLeft == 0) {
            flush();
            bytesLeft = BUFFER_SIZE;
          }
        }
      }
    }