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.io.IOException;
21 import java.io.Closeable;
23 /** Abstract base class for input from a file in a {@link Directory}. A
24 * random-access input stream. Used for all Lucene index input operations.
27 public abstract class IndexInput extends DataInput implements Cloneable,Closeable {
32 * Similar to {@link #readChars(char[], int, int)} but does not do any conversion operations on the bytes it is reading in. It still
33 * has to invoke {@link #readByte()} just as {@link #readChars(char[], int, int)} does, but it does not need a buffer to store anything
34 * and it does not have to do any of the bitwise operations, since we don't actually care what is in the byte except to determine
35 * how many more bytes to read
36 * @param length The number of chars to read
37 * @deprecated this method operates on old "modified utf8" encoded
41 public void skipChars(int length) throws IOException{
42 for (int i = 0; i < length; i++) {
45 //do nothing, we only need one byte
46 } else if ((b & 0xE0) != 0xE0) {
47 readByte();//read an additional byte
49 //read two additional bytes.
56 private final String resourceDescription;
58 /** @deprecated please pass resourceDescription */
60 protected IndexInput() {
61 this("anonymous IndexInput");
64 /** resourceDescription should be a non-null, opaque string
65 * describing this resource; it's returned from
66 * {@link #toString}. */
67 protected IndexInput(String resourceDescription) {
68 if (resourceDescription == null) {
69 throw new IllegalArgumentException("resourceDescription must not be null");
71 this.resourceDescription = resourceDescription;
74 /** Closes the stream to further operations. */
75 public abstract void close() throws IOException;
77 /** Returns the current position in this file, where the next read will
81 public abstract long getFilePointer();
83 /** Sets current position in this file, where the next read will occur.
84 * @see #getFilePointer()
86 public abstract void seek(long pos) throws IOException;
88 /** The number of bytes in the file. */
89 public abstract long length();
92 * Copies <code>numBytes</code> bytes to the given {@link IndexOutput}.
94 * <b>NOTE:</b> this method uses an intermediate buffer to copy the bytes.
95 * Consider overriding it in your implementation, if you can make a better,
98 * <b>NOTE</b> ensure that there are enough bytes in the input to copy to
99 * output. Otherwise, different exceptions may be thrown, depending on the
102 public void copyBytes(IndexOutput out, long numBytes) throws IOException {
103 assert numBytes >= 0: "numBytes=" + numBytes;
105 byte copyBuf[] = new byte[BufferedIndexInput.BUFFER_SIZE];
107 while (numBytes > 0) {
108 final int toCopy = (int) (numBytes > copyBuf.length ? copyBuf.length : numBytes);
109 readBytes(copyBuf, 0, toCopy);
110 out.writeBytes(copyBuf, 0, toCopy);
116 public String toString() {
117 return resourceDescription;