pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / store / IndexInput.java
1 package org.apache.lucene.store;
2
3 /**
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  */
19
20 import java.io.IOException;
21 import java.io.Closeable;
22
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.
25  * @see Directory
26  */
27 public abstract class IndexInput extends DataInput implements Cloneable,Closeable {
28
29   /**
30    * Expert
31    * 
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
38    *             strings
39    */
40   @Deprecated
41   public void skipChars(int length) throws IOException{
42     for (int i = 0; i < length; i++) {
43       byte b = readByte();
44       if ((b & 0x80) == 0){
45         //do nothing, we only need one byte
46       } else if ((b & 0xE0) != 0xE0) {
47         readByte();//read an additional byte
48       } else {      
49         //read two additional bytes.
50         readByte();
51         readByte();
52       }
53     }
54   }
55
56   private final String resourceDescription;
57
58   /** @deprecated please pass resourceDescription */
59   @Deprecated
60   protected IndexInput() {
61     this("anonymous IndexInput");
62   }
63
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");
70     }
71     this.resourceDescription = resourceDescription;
72   }
73
74   /** Closes the stream to further operations. */
75   public abstract void close() throws IOException;
76
77   /** Returns the current position in this file, where the next read will
78    * occur.
79    * @see #seek(long)
80    */
81   public abstract long getFilePointer();
82
83   /** Sets current position in this file, where the next read will occur.
84    * @see #getFilePointer()
85    */
86   public abstract void seek(long pos) throws IOException;
87
88   /** The number of bytes in the file. */
89   public abstract long length();
90
91   /**
92    * Copies <code>numBytes</code> bytes to the given {@link IndexOutput}.
93    * <p>
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,
96    * optimized copy.
97    * <p>
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
100    * implementation.
101    */
102   public void copyBytes(IndexOutput out, long numBytes) throws IOException {
103     assert numBytes >= 0: "numBytes=" + numBytes;
104
105     byte copyBuf[] = new byte[BufferedIndexInput.BUFFER_SIZE];
106
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);
111       numBytes -= toCopy;
112     }
113   }
114
115   @Override
116   public String toString() {
117     return resourceDescription;
118   }
119 }