add --shared
[pylucene.git] / lucene-java-3.4.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   protected byte[] copyBuf = null;
30
31   /**
32    * Expert
33    * 
34    * Similar to {@link #readChars(char[], int, int)} but does not do any conversion operations on the bytes it is reading in.  It still
35    * has to invoke {@link #readByte()} just as {@link #readChars(char[], int, int)} does, but it does not need a buffer to store anything
36    * 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
37    * how many more bytes to read
38    * @param length The number of chars to read
39    * @deprecated this method operates on old "modified utf8" encoded
40    *             strings
41    */
42   @Deprecated
43   public void skipChars(int length) throws IOException{
44     for (int i = 0; i < length; i++) {
45       byte b = readByte();
46       if ((b & 0x80) == 0){
47         //do nothing, we only need one byte
48       } else if ((b & 0xE0) != 0xE0) {
49         readByte();//read an additional byte
50       } else {      
51         //read two additional bytes.
52         readByte();
53         readByte();
54       }
55     }
56   }
57
58   /** Closes the stream to further operations. */
59   public abstract void close() throws IOException;
60
61   /** Returns the current position in this file, where the next read will
62    * occur.
63    * @see #seek(long)
64    */
65   public abstract long getFilePointer();
66
67   /** Sets current position in this file, where the next read will occur.
68    * @see #getFilePointer()
69    */
70   public abstract void seek(long pos) throws IOException;
71
72   /** The number of bytes in the file. */
73   public abstract long length();
74
75   /**
76    * Copies <code>numBytes</code> bytes to the given {@link IndexOutput}.
77    * <p>
78    * <b>NOTE:</b> this method uses an intermediate buffer to copy the bytes.
79    * Consider overriding it in your implementation, if you can make a better,
80    * optimized copy.
81    * <p>
82    * <b>NOTE</b> ensure that there are enough bytes in the input to copy to
83    * output. Otherwise, different exceptions may be thrown, depending on the
84    * implementation.
85    */
86   public void copyBytes(IndexOutput out, long numBytes) throws IOException {
87     assert numBytes >= 0: "numBytes=" + numBytes;
88
89     if (copyBuf == null) {
90       copyBuf = new byte[BufferedIndexInput.BUFFER_SIZE];
91     }
92
93     while (numBytes > 0) {
94       final int toCopy = (int) (numBytes > copyBuf.length ? copyBuf.length : numBytes);
95       readBytes(copyBuf, 0, toCopy);
96       out.writeBytes(copyBuf, 0, toCopy);
97       numBytes -= toCopy;
98     }
99   }
100   
101 }