package org.apache.lucene.store;

import java.io.IOException;
import java.util.Map;

/**
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Used by MockDirectoryWrapper to create an input stream that
 * keeps track of when it's been closed.
 */

public class MockIndexInputWrapper extends IndexInput {
  private MockDirectoryWrapper dir;
  final String name;
  private IndexInput delegate;
  private boolean isClone;

  /** Construct an empty output buffer. */
  public MockIndexInputWrapper(MockDirectoryWrapper dir, String name, IndexInput delegate) {
    this.name = name;
    this.dir = dir;
    this.delegate = delegate;
  }

  @Override
  public void close() throws IOException {
    try {
      // turn on the following to look for leaks closing inputs,
      // after fixing TestTransactions
      // dir.maybeThrowDeterministicException();
    } finally {
      delegate.close();
      // Pending resolution on LUCENE-686 we may want to
      // remove the conditional check so we also track that
      // all clones get closed:
      if (!isClone) {
        dir.removeIndexInput(this, name);
      }
    }
  }

  @Override
  public Object clone() {
    dir.inputCloneCount.incrementAndGet();
    IndexInput iiclone = (IndexInput) delegate.clone();
    MockIndexInputWrapper clone = new MockIndexInputWrapper(dir, name, iiclone);
    clone.isClone = true;
    // Pending resolution on LUCENE-686 we may want to
    // uncomment this code so that we also track that all
    // clones get closed:
    /*
    synchronized(dir.openFiles) {
      if (dir.openFiles.containsKey(name)) {
        Integer v = (Integer) dir.openFiles.get(name);
        v = Integer.valueOf(v.intValue()+1);
        dir.openFiles.put(name, v);
      } else {
        throw new RuntimeException("BUG: cloned file was not open?");
      }
    }
    */
    return clone;
  }

  @Override
  public long getFilePointer() {
    return delegate.getFilePointer();
  }

  @Override
  public void seek(long pos) throws IOException {
    delegate.seek(pos);
  }

  @Override
  public long length() {
    return delegate.length();
  }

  @Override
  public byte readByte() throws IOException {
    return delegate.readByte();
  }

  @Override
  public void readBytes(byte[] b, int offset, int len) throws IOException {
    delegate.readBytes(b, offset, len);
  }

  @Override
  public void copyBytes(IndexOutput out, long numBytes) throws IOException {
    delegate.copyBytes(out, numBytes);
  }

  @Override
  public void readBytes(byte[] b, int offset, int len, boolean useBuffer)
      throws IOException {
    delegate.readBytes(b, offset, len, useBuffer);
  }

  @Override
  public short readShort() throws IOException {
    return delegate.readShort();
  }

  @Override
  public int readInt() throws IOException {
    return delegate.readInt();
  }

  @Override
  public long readLong() throws IOException {
    return delegate.readLong();
  }

  @Override
  public String readString() throws IOException {
    return delegate.readString();
  }

  @Override
  public Map<String,String> readStringStringMap() throws IOException {
    return delegate.readStringStringMap();
  }

  @Override
  public int readVInt() throws IOException {
    return delegate.readVInt();
  }

  @Override
  public long readVLong() throws IOException {
    return delegate.readVLong();
  }

  @Override
  public String toString() {
    return "MockIndexInputWrapper(" + delegate + ")";
  }
}


@@ -1165,16 +1165,42 @@
 isClone;
+%0A  private boolean closed;
 %0A%0A  /** 
@@ -1636,24 +1636,45 @@
 %7D finally %7B%0A
+      closed = true;%0A
       delega
@@ -1911,24 +1911,149 @@
  %7D%0A    %7D%0A  %7D
+%0A  %0A  private void ensureOpen() %7B%0A    if (closed) %7B%0A      throw new RuntimeException(%22Abusing closed IndexInput!%22);%0A    %7D%0A  %7D
 %0A%0A  @Overrid
@@ -2076,24 +2076,42 @@
 t clone() %7B%0A
+    ensureOpen();%0A
     dir.inpu
@@ -2835,24 +2835,42 @@
 Pointer() %7B%0A
+    ensureOpen();%0A
     return d
@@ -2954,32 +2954,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     delegate.see
@@ -3042,24 +3042,42 @@
  length() %7B%0A
+    ensureOpen();%0A
     return d
@@ -3149,32 +3149,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -3292,32 +3292,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     delegate.rea
@@ -3444,32 +3444,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     delegate.cop
@@ -3623,24 +3623,42 @@
 Exception %7B%0A
+    ensureOpen();%0A
     delegate
@@ -3753,32 +3753,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -3865,32 +3865,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -3977,32 +3977,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -4094,32 +4094,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -4234,32 +4234,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -4357,32 +4357,50 @@
 s IOException %7B%0A
+    ensureOpen();%0A
     return deleg
@@ -4424,32 +4424,32 @@
  %7D%0A%0A  @Override%0A
-
   public long re
@@ -4475,24 +4475,42 @@
 Exception %7B%0A
+    ensureOpen();%0A
     return d

throw exception if you muck with a closed indexinput