KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > store > BufferedIndexOutput


1 package org.apache.lucene.store;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 /** Base implementation class for buffered {@link IndexOutput}. */
22 public abstract class BufferedIndexOutput extends IndexOutput {
23   static final int BUFFER_SIZE = 1024;
24
25   private final byte[] buffer = new byte[BUFFER_SIZE];
26   private long bufferStart = 0; // position in file of buffer
27
private int bufferPosition = 0; // position in buffer
28

29   /** Writes a single byte.
30    * @see IndexInput#readByte()
31    */

32   public void writeByte(byte b) throws IOException JavaDoc {
33     if (bufferPosition >= BUFFER_SIZE)
34       flush();
35     buffer[bufferPosition++] = b;
36   }
37
38   /** Writes an array of bytes.
39    * @param b the bytes to write
40    * @param length the number of bytes to write
41    * @see IndexInput#readBytes(byte[],int,int)
42    */

43   public void writeBytes(byte[] b, int length) throws IOException JavaDoc {
44     int bytesLeft = BUFFER_SIZE - bufferPosition;
45     // is there enough space in the buffer?
46
if (bytesLeft >= length) {
47       // we add the data to the end of the buffer
48
System.arraycopy(b, 0, buffer, bufferPosition, length);
49       bufferPosition += length;
50       // if the buffer is full, flush it
51
if (BUFFER_SIZE - bufferPosition == 0)
52         flush();
53     } else {
54       // is data larger then buffer?
55
if (length > BUFFER_SIZE) {
56         // we flush the buffer
57
if (bufferPosition > 0)
58           flush();
59         // and write data at once
60
flushBuffer(b, length);
61         bufferStart += length;
62       } else {
63         // we fill/flush the buffer (until the input is written)
64
int pos = 0; // position in the input data
65
int pieceLength;
66         while (pos < length) {
67           pieceLength = (length - pos < bytesLeft) ? length - pos : bytesLeft;
68           System.arraycopy(b, pos, buffer, bufferPosition, pieceLength);
69           pos += pieceLength;
70           bufferPosition += pieceLength;
71           // if the buffer is full, flush it
72
bytesLeft = BUFFER_SIZE - bufferPosition;
73           if (bytesLeft == 0) {
74             flush();
75             bytesLeft = BUFFER_SIZE;
76           }
77         }
78       }
79     }
80   }
81
82   /** Forces any buffered output to be written. */
83   public void flush() throws IOException JavaDoc {
84     flushBuffer(buffer, bufferPosition);
85     bufferStart += bufferPosition;
86     bufferPosition = 0;
87   }
88
89   /** Expert: implements buffer write. Writes bytes at the current position in
90    * the output.
91    * @param b the bytes to write
92    * @param len the number of bytes to write
93    */

94   protected abstract void flushBuffer(byte[] b, int len) throws IOException JavaDoc;
95
96   /** Closes this stream to further operations. */
97   public void close() throws IOException JavaDoc {
98     flush();
99   }
100
101   /** Returns the current position in this file, where the next write will
102    * occur.
103    * @see #seek(long)
104    */

105   public long getFilePointer() {
106     return bufferStart + bufferPosition;
107   }
108
109   /** Sets current position in this file, where the next write will occur.
110    * @see #getFilePointer()
111    */

112   public void seek(long pos) throws IOException JavaDoc {
113     flush();
114     bufferStart = pos;
115   }
116
117   /** The number of bytes in the file. */
118   public abstract long length() throws IOException JavaDoc;
119
120
121 }
122
Popular Tags