001 package gate.creole.annic.apache.lucene.store;
002
003 /**
004 * Copyright 2004 The Apache Software Foundation
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019 import java.io.IOException;
020
021 /**
022 * A memory-resident {@link OutputStream} implementation.
023 *
024 * @version $Id: RAMOutputStream.java 529 2004-10-05 11:55:26Z niraj $
025 */
026
027 public class RAMOutputStream extends OutputStream {
028 private RAMFile file;
029 private int pointer = 0;
030
031 /** Construct an empty output buffer. */
032 public RAMOutputStream() {
033 this(new RAMFile());
034 }
035
036 RAMOutputStream(RAMFile f) {
037 file = f;
038 }
039
040 /** Copy the current contents of this buffer to the named output. */
041 public void writeTo(OutputStream out) throws IOException {
042 flush();
043 final long end = file.length;
044 long pos = 0;
045 int buffer = 0;
046 while (pos < end) {
047 int length = BUFFER_SIZE;
048 long nextPos = pos + length;
049 if (nextPos > end) { // at the last buffer
050 length = (int)(end - pos);
051 }
052 out.writeBytes((byte[])file.buffers.elementAt(buffer++), length);
053 pos = nextPos;
054 }
055 }
056
057 /** Resets this to an empty buffer. */
058 public void reset() {
059 try {
060 seek(0);
061 } catch (IOException e) { // should never happen
062 throw new RuntimeException(e.toString());
063 }
064
065 file.length = 0;
066 }
067
068 public void flushBuffer(byte[] src, int len) {
069 int bufferNumber = pointer/BUFFER_SIZE;
070 int bufferOffset = pointer%BUFFER_SIZE;
071 int bytesInBuffer = BUFFER_SIZE - bufferOffset;
072 int bytesToCopy = bytesInBuffer >= len ? len : bytesInBuffer;
073
074 if (bufferNumber == file.buffers.size())
075 file.buffers.addElement(new byte[BUFFER_SIZE]);
076
077 byte[] buffer = (byte[])file.buffers.elementAt(bufferNumber);
078 System.arraycopy(src, 0, buffer, bufferOffset, bytesToCopy);
079
080 if (bytesToCopy < len) { // not all in one buffer
081 int srcOffset = bytesToCopy;
082 bytesToCopy = len - bytesToCopy; // remaining bytes
083 bufferNumber++;
084 if (bufferNumber == file.buffers.size())
085 file.buffers.addElement(new byte[BUFFER_SIZE]);
086 buffer = (byte[])file.buffers.elementAt(bufferNumber);
087 System.arraycopy(src, srcOffset, buffer, 0, bytesToCopy);
088 }
089 pointer += len;
090 if (pointer > file.length)
091 file.length = pointer;
092
093 file.lastModified = System.currentTimeMillis();
094 }
095
096 public void close() throws IOException {
097 super.close();
098 }
099
100 public void seek(long pos) throws IOException {
101 super.seek(pos);
102 pointer = (int)pos;
103 }
104 public long length() {
105 return file.length;
106 }
107 }
|