pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test-framework / java / org / apache / lucene / store / MockIndexInputWrapper.java
1 package org.apache.lucene.store;
2
3 import java.io.IOException;
4 import java.util.Map;
5
6 /**
7  * Licensed to the Apache Software Foundation (ASF) under one or more
8  * contributor license agreements.  See the NOTICE file distributed with
9  * this work for additional information regarding copyright ownership.
10  * The ASF licenses this file to You under the Apache License, Version 2.0
11  * (the "License"); you may not use this file except in compliance with
12  * the License.  You may obtain a copy of the License at
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  */
22
23 /**
24  * Used by MockDirectoryWrapper to create an input stream that
25  * keeps track of when it's been closed.
26  */
27
28 public class MockIndexInputWrapper extends IndexInput {
29   private MockDirectoryWrapper dir;
30   final String name;
31   private IndexInput delegate;
32   private boolean isClone;
33   private boolean closed;
34
35   /** Construct an empty output buffer. */
36   public MockIndexInputWrapper(MockDirectoryWrapper dir, String name, IndexInput delegate) {
37     super("MockIndexInputWrapper(name=" + name + " delegate=" + delegate + ")");
38     this.name = name;
39     this.dir = dir;
40     this.delegate = delegate;
41   }
42
43   @Override
44   public void close() throws IOException {
45     try {
46       // turn on the following to look for leaks closing inputs,
47       // after fixing TestTransactions
48       // dir.maybeThrowDeterministicException();
49     } finally {
50       closed = true;
51       delegate.close();
52       // Pending resolution on LUCENE-686 we may want to
53       // remove the conditional check so we also track that
54       // all clones get closed:
55       if (!isClone) {
56         dir.removeIndexInput(this, name);
57       }
58     }
59   }
60   
61   private void ensureOpen() {
62     if (closed) {
63       throw new RuntimeException("Abusing closed IndexInput!");
64     }
65   }
66
67   @Override
68   public Object clone() {
69     ensureOpen();
70     dir.inputCloneCount.incrementAndGet();
71     IndexInput iiclone = (IndexInput) delegate.clone();
72     MockIndexInputWrapper clone = new MockIndexInputWrapper(dir, name, iiclone);
73     clone.isClone = true;
74     // Pending resolution on LUCENE-686 we may want to
75     // uncomment this code so that we also track that all
76     // clones get closed:
77     /*
78     synchronized(dir.openFiles) {
79       if (dir.openFiles.containsKey(name)) {
80         Integer v = (Integer) dir.openFiles.get(name);
81         v = Integer.valueOf(v.intValue()+1);
82         dir.openFiles.put(name, v);
83       } else {
84         throw new RuntimeException("BUG: cloned file was not open?");
85       }
86     }
87     */
88     return clone;
89   }
90
91   @Override
92   public long getFilePointer() {
93     ensureOpen();
94     return delegate.getFilePointer();
95   }
96
97   @Override
98   public void seek(long pos) throws IOException {
99     ensureOpen();
100     delegate.seek(pos);
101   }
102
103   @Override
104   public long length() {
105     ensureOpen();
106     return delegate.length();
107   }
108
109   @Override
110   public byte readByte() throws IOException {
111     ensureOpen();
112     return delegate.readByte();
113   }
114
115   @Override
116   public void readBytes(byte[] b, int offset, int len) throws IOException {
117     ensureOpen();
118     delegate.readBytes(b, offset, len);
119   }
120
121   @Override
122   public void copyBytes(IndexOutput out, long numBytes) throws IOException {
123     ensureOpen();
124     delegate.copyBytes(out, numBytes);
125   }
126
127   @Override
128   public void readBytes(byte[] b, int offset, int len, boolean useBuffer)
129       throws IOException {
130     ensureOpen();
131     delegate.readBytes(b, offset, len, useBuffer);
132   }
133
134   @Override
135   public int readInt() throws IOException {
136     ensureOpen();
137     return delegate.readInt();
138   }
139
140   @Override
141   public int readVInt() throws IOException {
142     ensureOpen();
143     return delegate.readVInt();
144   }
145
146   @Override
147   public long readLong() throws IOException {
148     ensureOpen();
149     return delegate.readLong();
150   }
151
152   @Override
153   public long readVLong() throws IOException {
154     ensureOpen();
155     return delegate.readVLong();
156   }
157
158   @Override
159   public String readString() throws IOException {
160     ensureOpen();
161     return delegate.readString();
162   }
163
164   @Override
165   public Map<String,String> readStringStringMap() throws IOException {
166     ensureOpen();
167     return delegate.readStringStringMap();
168   }
169
170   @Override
171   public void setModifiedUTF8StringsMode() {
172     ensureOpen();
173     delegate.setModifiedUTF8StringsMode();
174   }
175
176   @Override
177   public void readChars(char[] buffer, int start, int length)
178       throws IOException {
179     ensureOpen();
180     delegate.readChars(buffer, start, length);
181   }
182
183   @Override
184   public void skipChars(int length) throws IOException {
185     ensureOpen();
186     delegate.skipChars(length);
187   }
188 }