KickJava   Java API By Example, From Geeks To Geeks.

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


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 output to a file in a Directory. A random-access
22  * output stream. Used for all Lucene index output operations.
23  * @see Directory
24  * @see IndexInput
25  */

26 public abstract class IndexOutput {
27
28   /** Writes a single byte.
29    * @see IndexInput#readByte()
30    */

31   public abstract void writeByte(byte b) throws IOException JavaDoc;
32
33   /** Writes an array of bytes.
34    * @param b the bytes to write
35    * @param length the number of bytes to write
36    * @see IndexInput#readBytes(byte[],int,int)
37    */

38   public abstract void writeBytes(byte[] b, int length) throws IOException JavaDoc;
39
40   /** Writes an int as four bytes.
41    * @see IndexInput#readInt()
42    */

43   public void writeInt(int i) throws IOException JavaDoc {
44     writeByte((byte)(i >> 24));
45     writeByte((byte)(i >> 16));
46     writeByte((byte)(i >> 8));
47     writeByte((byte) i);
48   }
49
50   /** Writes an int in a variable-length format. Writes between one and
51    * five bytes. Smaller values take fewer bytes. Negative numbers are not
52    * supported.
53    * @see IndexInput#readVInt()
54    */

55   public void writeVInt(int i) throws IOException JavaDoc {
56     while ((i & ~0x7F) != 0) {
57       writeByte((byte)((i & 0x7f) | 0x80));
58       i >>>= 7;
59     }
60     writeByte((byte)i);
61   }
62
63   /** Writes a long as eight bytes.
64    * @see IndexInput#readLong()
65    */

66   public void writeLong(long i) throws IOException JavaDoc {
67     writeInt((int) (i >> 32));
68     writeInt((int) i);
69   }
70
71   /** Writes an long in a variable-length format. Writes between one and five
72    * bytes. Smaller values take fewer bytes. Negative numbers are not
73    * supported.
74    * @see IndexInput#readVLong()
75    */

76   public void writeVLong(long i) throws IOException JavaDoc {
77     while ((i & ~0x7F) != 0) {
78       writeByte((byte)((i & 0x7f) | 0x80));
79       i >>>= 7;
80     }
81     writeByte((byte)i);
82   }
83
84   /** Writes a string.
85    * @see IndexInput#readString()
86    */

87   public void writeString(String JavaDoc s) throws IOException JavaDoc {
88     int length = s.length();
89     writeVInt(length);
90     writeChars(s, 0, length);
91   }
92
93   /** Writes a sequence of UTF-8 encoded characters from a string.
94    * @param s the source of the characters
95    * @param start the first character in the sequence
96    * @param length the number of characters in the sequence
97    * @see IndexInput#readChars(char[],int,int)
98    */

99   public void writeChars(String JavaDoc s, int start, int length)
100        throws IOException JavaDoc {
101     final int end = start + length;
102     for (int i = start; i < end; i++) {
103       final int code = (int)s.charAt(i);
104       if (code >= 0x01 && code <= 0x7F)
105     writeByte((byte)code);
106       else if (((code >= 0x80) && (code <= 0x7FF)) || code == 0) {
107     writeByte((byte)(0xC0 | (code >> 6)));
108     writeByte((byte)(0x80 | (code & 0x3F)));
109       } else {
110     writeByte((byte)(0xE0 | (code >>> 12)));
111     writeByte((byte)(0x80 | ((code >> 6) & 0x3F)));
112     writeByte((byte)(0x80 | (code & 0x3F)));
113       }
114     }
115   }
116
117   /** Forces any buffered output to be written. */
118   public abstract void flush() throws IOException JavaDoc;
119
120   /** Closes this stream to further operations. */
121   public abstract void close() throws IOException JavaDoc;
122
123   /** Returns the current position in this file, where the next write will
124    * occur.
125    * @see #seek(long)
126    */

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

132   public abstract void seek(long pos) throws IOException JavaDoc;
133
134   /** The number of bytes in the file. */
135   public abstract long length() throws IOException JavaDoc;
136
137
138 }
139
Popular Tags