01: package org.apache.lucene.store;
02:
03: import java.io.IOException;
04:
05: /**
06: * Licensed to the Apache Software Foundation (ASF) under one or more
07: * contributor license agreements. See the NOTICE file distributed with
08: * this work for additional information regarding copyright ownership.
09: * The ASF licenses this file to You under the Apache License, Version 2.0
10: * (the "License"); you may not use this file except in compliance with
11: * the License. You may obtain a copy of the License at
12: *
13: * http://www.apache.org/licenses/LICENSE-2.0
14: *
15: * Unless required by applicable law or agreed to in writing, software
16: * distributed under the License is distributed on an "AS IS" BASIS,
17: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18: * See the License for the specific language governing permissions and
19: * limitations under the License.
20: */
21:
22: /**
23: * Used by MockRAMDirectory to create an input stream that
24: * keeps track of when it's been closed.
25: */
26:
27: public class MockRAMInputStream extends RAMInputStream {
28: private MockRAMDirectory dir;
29: private String name;
30: private boolean isClone;
31:
32: /** Construct an empty output buffer.
33: * @throws IOException */
34: public MockRAMInputStream(MockRAMDirectory dir, String name,
35: RAMFile f) throws IOException {
36: super (f);
37: this .name = name;
38: this .dir = dir;
39: }
40:
41: public void close() {
42: super .close();
43: // Pending resolution on LUCENE-686 we may want to
44: // remove the conditional check so we also track that
45: // all clones get closed:
46: if (!isClone) {
47: synchronized (dir.openFiles) {
48: Integer v = (Integer) dir.openFiles.get(name);
49: if (v.intValue() == 1) {
50: dir.openFiles.remove(name);
51: } else {
52: v = new Integer(v.intValue() - 1);
53: dir.openFiles.put(name, v);
54: }
55: }
56: }
57: }
58:
59: public Object clone() {
60: MockRAMInputStream clone = (MockRAMInputStream) super .clone();
61: clone.isClone = true;
62: // Pending resolution on LUCENE-686 we may want to
63: // uncomment this code so that we also track that all
64: // clones get closed:
65: /*
66: synchronized(dir.openFiles) {
67: if (dir.openFiles.containsKey(name)) {
68: Integer v = (Integer) dir.openFiles.get(name);
69: v = new Integer(v.intValue()+1);
70: dir.openFiles.put(name, v);
71: } else {
72: throw new RuntimeException("BUG: cloned file was not open?");
73: }
74: }
75: */
76: return clone;
77: }
78: }
|