1 package org.apache.lucene.store;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import java.io.IOException;
23 * A memory-resident {@link IndexOutput} implementation.
27 public class RAMOutputStream extends IndexOutput {
28 static final int BUFFER_SIZE = 1024;
32 private byte[] currentBuffer;
33 private int currentBufferIndex;
35 private int bufferPosition;
36 private long bufferStart;
37 private int bufferLength;
39 /** Construct an empty output buffer. */
40 public RAMOutputStream() {
44 public RAMOutputStream(RAMFile f) {
47 // make sure that we switch to the
48 // first needed buffer lazily
49 currentBufferIndex = -1;
53 /** Copy the current contents of this buffer to the named output. */
54 public void writeTo(IndexOutput out) throws IOException {
56 final long end = file.length;
60 int length = BUFFER_SIZE;
61 long nextPos = pos + length;
62 if (nextPos > end) { // at the last buffer
63 length = (int)(end - pos);
65 out.writeBytes(file.getBuffer(buffer++), length);
70 /** Resets this to an empty file. */
73 currentBufferIndex = -1;
81 public void close() throws IOException {
86 public void seek(long pos) throws IOException {
87 // set the file length in case we seek back
88 // and flush() has not been called yet
90 if (pos < bufferStart || pos >= bufferStart + bufferLength) {
91 currentBufferIndex = (int) (pos / BUFFER_SIZE);
92 switchCurrentBuffer();
95 bufferPosition = (int) (pos % BUFFER_SIZE);
99 public long length() {
104 public void writeByte(byte b) throws IOException {
105 if (bufferPosition == bufferLength) {
106 currentBufferIndex++;
107 switchCurrentBuffer();
109 currentBuffer[bufferPosition++] = b;
113 public void writeBytes(byte[] b, int offset, int len) throws IOException {
116 if (bufferPosition == bufferLength) {
117 currentBufferIndex++;
118 switchCurrentBuffer();
121 int remainInBuffer = currentBuffer.length - bufferPosition;
122 int bytesToCopy = len < remainInBuffer ? len : remainInBuffer;
123 System.arraycopy(b, offset, currentBuffer, bufferPosition, bytesToCopy);
124 offset += bytesToCopy;
126 bufferPosition += bytesToCopy;
130 private final void switchCurrentBuffer() throws IOException {
131 if (currentBufferIndex == file.numBuffers()) {
132 currentBuffer = file.addBuffer(BUFFER_SIZE);
134 currentBuffer = file.getBuffer(currentBufferIndex);
137 bufferStart = (long) BUFFER_SIZE * (long) currentBufferIndex;
138 bufferLength = currentBuffer.length;
141 private void setFileLength() {
142 long pointer = bufferStart + bufferPosition;
143 if (pointer > file.length) {
144 file.setLength(pointer);
149 public void flush() throws IOException {
150 file.setLastModified(System.currentTimeMillis());
155 public long getFilePointer() {
156 return currentBufferIndex < 0 ? 0 : bufferStart + bufferPosition;
159 /** Returns byte usage of all buffers. */
160 public long sizeInBytes() {
161 return file.numBuffers() * BUFFER_SIZE;
165 public void copyBytes(DataInput input, long numBytes) throws IOException {
166 assert numBytes >= 0: "numBytes=" + numBytes;
168 while (numBytes > 0) {
169 if (bufferPosition == bufferLength) {
170 currentBufferIndex++;
171 switchCurrentBuffer();
174 int toCopy = currentBuffer.length - bufferPosition;
175 if (numBytes < toCopy) {
176 toCopy = (int) numBytes;
178 input.readBytes(currentBuffer, bufferPosition, toCopy, false);
180 bufferPosition += toCopy;