/* * Copyright 2004 The Apache Software Foundation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ using System; namespace Lucene.Net.Store { /// Base implementation class for buffered {@link IndexOutput}. public abstract class BufferedIndexOutput:IndexOutput { internal const int BUFFER_SIZE = 1024; private byte[] buffer = new byte[BUFFER_SIZE]; private long bufferStart = 0; // position in file of buffer private int bufferPosition = 0; // position in buffer /// Writes a single byte. /// /// public override void WriteByte(byte b) { if (bufferPosition >= BUFFER_SIZE) Flush(); buffer[bufferPosition++] = b; } /// Writes an array of bytes. /// the bytes to write /// /// the number of bytes to write /// /// /// public override void WriteBytes(byte[] b, int length) { for (int i = 0; i < length; i++) WriteByte(b[i]); } /// Forces any buffered output to be written. public override void Flush() { FlushBuffer(buffer, bufferPosition); bufferStart += bufferPosition; bufferPosition = 0; } /// Expert: implements buffer write. Writes bytes at the current position in /// the output. /// /// the bytes to write /// /// the number of bytes to write /// public abstract void FlushBuffer(byte[] b, int len); /// Closes this stream to further operations. public override void Close() { Flush(); } /// Returns the current position in this file, where the next write will /// occur. /// /// /// public override long GetFilePointer() { return bufferStart + bufferPosition; } /// Sets current position in this file, where the next write will occur. /// /// public override void Seek(long pos) { Flush(); bufferStart = pos; } /// The number of bytes in the file. public abstract override long Length(); } }