Index: src/java/org/apache/lucene/store/BufferedIndexOutput.java =================================================================== --- src/java/org/apache/lucene/store/BufferedIndexOutput.java (revision 940113) +++ src/java/org/apache/lucene/store/BufferedIndexOutput.java (working copy) @@ -23,16 +23,34 @@ public abstract class BufferedIndexOutput extends IndexOutput { static final int BUFFER_SIZE = 16384; - private final byte[] buffer = new byte[BUFFER_SIZE]; + private int bufferSize; + + private final byte[] buffer; private long bufferStart = 0; // position in file of buffer private int bufferPosition = 0; // position in buffer + + public BufferedIndexOutput() { + this(BUFFER_SIZE); + } + + public BufferedIndexOutput(int bufferSize) { + if (bufferSize <= 0) + throw new IllegalArgumentException("bufferSize must be greater than 0 (got " + bufferSize + ")"); + this.bufferSize = bufferSize; + buffer = new byte[this.bufferSize]; + } + /** Returns buffer size. @see #setBufferSize */ + public int getBufferSize() { + return bufferSize; + } + /** Writes a single byte. * @see IndexInput#readByte() */ @Override public void writeByte(byte b) throws IOException { - if (bufferPosition >= BUFFER_SIZE) + if (bufferPosition >= bufferSize) flush(); buffer[bufferPosition++] = b; } @@ -44,18 +62,18 @@ */ @Override public void writeBytes(byte[] b, int offset, int length) throws IOException { - int bytesLeft = BUFFER_SIZE - bufferPosition; + int bytesLeft = bufferSize - 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) + if (bufferSize - bufferPosition == 0) flush(); } else { // is data larger then buffer? - if (length > BUFFER_SIZE) { + if (length > bufferSize) { // we flush the buffer if (bufferPosition > 0) flush(); @@ -72,10 +90,10 @@ pos += pieceLength; bufferPosition += pieceLength; // if the buffer is full, flush it - bytesLeft = BUFFER_SIZE - bufferPosition; + bytesLeft = bufferSize - bufferPosition; if (bytesLeft == 0) { flush(); - bytesLeft = BUFFER_SIZE; + bytesLeft = bufferSize; } } }