package org.apache.lucene.store;

/*
 * 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.
 */

import java.io.Closeable;
import java.io.IOException;

/** Abstract base class for input from a file in a {@link Directory}.  A
 * random-access input stream.  Used for all Lucene index input operations.
 *
 * <p>{@code IndexInput} may only be used from one thread, because it is not
 * thread safe (it keeps internal state like file position). To allow
 * multithreaded use, every {@code IndexInput} instance must be cloned before
 * used in another thread. Subclasses must therefore implement {@link #clone()},
 * returning a new {@code IndexInput} which operates on the same underlying
 * resource, but positioned independently. Lucene never closes cloned
 * {@code IndexInput}s, it will only do this on the original one.
 * The original instance must take care that cloned instances throw
 * {@link AlreadyClosedException} when the original one is closed.
 
 * @see Directory
 */
public abstract class IndexInput extends DataInput implements Cloneable,Closeable {

  private final String resourceDescription;

  /** resourceDescription should be a non-null, opaque string
   *  describing this resource; it's returned from
   *  {@link #toString}. */
  protected IndexInput(String resourceDescription) {
    if (resourceDescription == null) {
      throw new IllegalArgumentException("resourceDescription must not be null");
    }
    this.resourceDescription = resourceDescription;
  }

  /** Closes the stream to further operations. */
  @Override
  public abstract void close() throws IOException;

  /** Returns the current position in this file, where the next read will
   * occur.
   * @see #seek(long)
   */
  public abstract long getFilePointer();

  /** Sets current position in this file, where the next read will occur.
   * @see #getFilePointer()
   */
  public abstract void seek(long pos) throws IOException;

  /** The number of bytes in the file. */
  public abstract long length();

  @Override
  public String toString() {
    return resourceDescription;
  }
  
  /** {@inheritDoc}
   * <p><b>Warning:</b> Lucene never closes cloned
   * {@code IndexInput}s, it will only do this on the original one.
   * The original instance must take care that cloned instances throw
   * {@link AlreadyClosedException} when the original one is closed.
   */
  @Override
  public IndexInput clone() {
    return (IndexInput) super.clone();
  }
  
  /**
   * Creates a slice of this index input, with the given description, offset, and length. 
   * The slice is seeked to the beginning.
   */
  public abstract IndexInput slice(String sliceDescription, long offset, long length) throws IOException;

  /** Subclasses call this to get the String for resourceDescription of a slice of this {@code IndexInput}. */
  protected String getFullSliceDescription(String sliceDescription) {
    if (sliceDescription == null) {
      // Clones pass null sliceDescription:
      return toString();
    } else {
      return toString() + " [slice=" + sliceDescription + "]";
    }
  }

  /**
   * Creates a random-access slice of this index input, with the given offset and length. 
   * <p>
   * The default implementation calls {@link #slice}, and it doesn't support random access,
   * it implements absolute reads as seek+read.
   */
  public RandomAccessInput randomAccessSlice(long offset, long length) throws IOException {
    final IndexInput slice = slice("randomaccess", offset, length);
    if (slice instanceof RandomAccessInput) {
      // slice() already supports random access
      return (RandomAccessInput) slice;
    } else {
      // return default impl
      return new RandomAccessInput() {
        @Override
        public byte readByte(long pos) throws IOException {
          slice.seek(pos);
          return slice.readByte();
        }
        
        @Override
        public short readShort(long pos) throws IOException {
          slice.seek(pos);
          return slice.readShort();
        }
        
        @Override
        public int readInt(long pos) throws IOException {
          slice.seek(pos);
          return slice.readInt();
        }
        
        @Override
        public long readLong(long pos) throws IOException {
          slice.seek(pos);
          return slice.readLong();
        }
      };
    }
  }
}

@@ -889,16 +889,20 @@
 n;%0A%0A/** 
+%0A * 
 Abstract
@@ -1268,16 +1268,22 @@
 efore%0A *
+ it is
  used in
@@ -1472,16 +1472,43 @@
 dently. 
+%0A * %0A * %3Cp%3E%3Cb%3EWarning:%3C/b%3E 
 Lucene n
@@ -1567,107 +1567,181 @@
 nly 
-do this on the original one.%0A * The original instance must take care that cloned instances
+call %7B@link #close()%7D on the original object.%0A * %0A * %3Cp%3EIf you access the cloned IndexInput after closing the original object,%0A * any %3Ccode%3EreadXXX%3C/code%3E methods will
  throw
-%0A *
  %7B@l
@@ -1771,43 +1771,12 @@
 ion%7D
- when the original one is closed
 .%0A 
+*
 %0A * 
@@ -2915,16 +2915,22 @@
 ritDoc%7D%0A
+   * %0A
    * %3Cp%3E
@@ -3015,15 +3015,29 @@
 nly 
-do this
+call %7B@link #close()%7D
  on 
@@ -3054,149 +3054,179 @@
 al o
-ne
+bject
 .%0A   * 
-The original instance must take care that cloned instances throw%0A   * %7B@link AlreadyClosedException%7D when the original one is closed
+%0A   * %3Cp%3EIf you access the cloned IndexInput after closing the original object,%0A   * any %3Ccode%3EreadXXX%3C/code%3E methods will throw %7B@link AlreadyClosedException%7D
 .%0A  

Clarify documentation of clone/close in IndexInput.