FileDocCategorySizeDatePackage
BufferedIndexOutput.javaAPI DocApache Lucene 2.2.04389Sat Jun 16 22:20:36 BST 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;
  
private 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

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

param
b the bytes to write
param
offset the offset in the byte array
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 offset, 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, offset, 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, offset, 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 + offset, 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;
          }
        }
      }
    }