BufferedIndexOutputpublic 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 |
Methods Summary |
---|
public void | close()Closes this stream to further operations.
flush();
| public void | flush()Forces any buffered output to be written.
flushBuffer(buffer, bufferPosition);
bufferStart += bufferPosition;
bufferPosition = 0;
| private void | flushBuffer(byte[] b, int len)Expert: implements buffer write. Writes bytes at the current position in
the output.
flushBuffer(b, 0, len);
| protected abstract void | flushBuffer(byte[] b, int offset, int len)Expert: implements buffer write. Writes bytes at the current position in
the output.
| public long | getFilePointer()Returns the current position in this file, where the next write will
occur.
return bufferStart + bufferPosition;
| public abstract long | length()The number of bytes in the file.
| public void | seek(long pos)Sets current position in this file, where the next write will occur.
flush();
bufferStart = pos;
| public void | writeByte(byte b)Writes a single byte. // position in buffer
if (bufferPosition >= BUFFER_SIZE)
flush();
buffer[bufferPosition++] = b;
| public void | writeBytes(byte[] b, int offset, int length)Writes an array of bytes.
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;
}
}
}
}
|
|