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.util.ArrayList;
21 import java.io.Serializable;
23 /** @lucene.internal */
24 public class RAMFile implements Serializable {
26 private static final long serialVersionUID = 1l;
28 protected ArrayList<byte[]> buffers = new ArrayList<byte[]>();
30 RAMDirectory directory;
31 protected long sizeInBytes;
33 // This is publicly modifiable via Directory.touchFile(), so direct access not supported
34 private long lastModified = System.currentTimeMillis();
36 // File used as buffer, in no RAMDirectory
39 RAMFile(RAMDirectory directory) {
40 this.directory = directory;
43 // For non-stream access from thread that might be concurrent with writing
44 public synchronized long getLength() {
48 protected synchronized void setLength(long length) {
52 // For non-stream access from thread that might be concurrent with writing
53 public synchronized long getLastModified() {
57 protected synchronized void setLastModified(long lastModified) {
58 this.lastModified = lastModified;
61 protected final byte[] addBuffer(int size) {
62 byte[] buffer = newBuffer(size);
68 if (directory != null) {
69 directory.sizeInBytes.getAndAdd(size);
74 protected final synchronized byte[] getBuffer(int index) {
75 return buffers.get(index);
78 protected final synchronized int numBuffers() {
79 return buffers.size();
83 * Expert: allocate a new buffer.
84 * Subclasses can allocate differently.
85 * @param size size of allocated buffer.
86 * @return allocated buffer.
88 protected byte[] newBuffer(int size) {
89 return new byte[size];
92 public synchronized long getSizeInBytes() {