KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > store > IndexInput


1 package org.apache.lucene.store;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20
21 /** Abstract base class for input from a file in a {@link Directory}. A
22  * random-access input stream. Used for all Lucene index input operations.
23  * @see Directory
24  */

25 public abstract class IndexInput implements Cloneable JavaDoc {
26   private char[] chars; // used by readString()
27

28   /** Reads and returns a single byte.
29    * @see IndexOutput#writeByte(byte)
30    */

31   public abstract byte readByte() throws IOException JavaDoc;
32
33   /** Reads a specified number of bytes into an array at the specified offset.
34    * @param b the array to read bytes into
35    * @param offset the offset in the array to start storing bytes
36    * @param len the number of bytes to read
37    * @see IndexOutput#writeBytes(byte[],int)
38    */

39   public abstract void readBytes(byte[] b, int offset, int len)
40     throws IOException JavaDoc;
41
42   /** Reads four bytes and returns an int.
43    * @see IndexOutput#writeInt(int)
44    */

45   public int readInt() throws IOException JavaDoc {
46     return ((readByte() & 0xFF) << 24) | ((readByte() & 0xFF) << 16)
47          | ((readByte() & 0xFF) << 8) | (readByte() & 0xFF);
48   }
49
50   /** Reads an int stored in variable-length format. Reads between one and
51    * five bytes. Smaller values take fewer bytes. Negative numbers are not
52    * supported.
53    * @see IndexOutput#writeVInt(int)
54    */

55   public int readVInt() throws IOException JavaDoc {
56     byte b = readByte();
57     int i = b & 0x7F;
58     for (int shift = 7; (b & 0x80) != 0; shift += 7) {
59       b = readByte();
60       i |= (b & 0x7F) << shift;
61     }
62     return i;
63   }
64
65   /** Reads eight bytes and returns a long.
66    * @see IndexOutput#writeLong(long)
67    */

68   public long readLong() throws IOException JavaDoc {
69     return (((long)readInt()) << 32) | (readInt() & 0xFFFFFFFFL);
70   }
71
72   /** Reads a long stored in variable-length format. Reads between one and
73    * nine bytes. Smaller values take fewer bytes. Negative numbers are not
74    * supported. */

75   public long readVLong() throws IOException JavaDoc {
76     byte b = readByte();
77     long i = b & 0x7F;
78     for (int shift = 7; (b & 0x80) != 0; shift += 7) {
79       b = readByte();
80       i |= (b & 0x7FL) << shift;
81     }
82     return i;
83   }
84
85   /** Reads a string.
86    * @see IndexOutput#writeString(String)
87    */

88   public String JavaDoc readString() throws IOException JavaDoc {
89     int length = readVInt();
90     if (chars == null || length > chars.length)
91       chars = new char[length];
92     readChars(chars, 0, length);
93     return new String JavaDoc(chars, 0, length);
94   }
95
96   /** Reads UTF-8 encoded characters into an array.
97    * @param buffer the array to read characters into
98    * @param start the offset in the array to start storing characters
99    * @param length the number of characters to read
100    * @see IndexOutput#writeChars(String,int,int)
101    */

102   public void readChars(char[] buffer, int start, int length)
103        throws IOException JavaDoc {
104     final int end = start + length;
105     for (int i = start; i < end; i++) {
106       byte b = readByte();
107       if ((b & 0x80) == 0)
108     buffer[i] = (char)(b & 0x7F);
109       else if ((b & 0xE0) != 0xE0) {
110     buffer[i] = (char)(((b & 0x1F) << 6)
111          | (readByte() & 0x3F));
112       } else
113     buffer[i] = (char)(((b & 0x0F) << 12)
114         | ((readByte() & 0x3F) << 6)
115             | (readByte() & 0x3F));
116     }
117   }
118
119   /** Closes the stream to futher operations. */
120   public abstract void close() throws IOException JavaDoc;
121
122   /** Returns the current position in this file, where the next read will
123    * occur.
124    * @see #seek(long)
125    */

126   public abstract long getFilePointer();
127
128   /** Sets current position in this file, where the next read will occur.
129    * @see #getFilePointer()
130    */

131   public abstract void seek(long pos) throws IOException JavaDoc;
132
133   /** The number of bytes in the file. */
134   public abstract long length();
135
136   /** Returns a clone of this stream.
137    *
138    * <p>Clones of a stream access the same data, and are positioned at the same
139    * point as the stream they were cloned from.
140    *
141    * <p>Expert: Subclasses must ensure that clones may be positioned at
142    * different points in the input from each other and from the stream they
143    * were cloned from.
144    */

145   public Object JavaDoc clone() {
146     IndexInput clone = null;
147     try {
148       clone = (IndexInput)super.clone();
149     } catch (CloneNotSupportedException JavaDoc e) {}
150
151     clone.chars = null;
152
153     return clone;
154   }
155
156 }
157
Popular Tags