2 * Copyright 2004 The Apache Software Foundation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 namespace Lucene
.Net
.Store
20 /// <summary>Abstract class for output to a file in a Directory. A random-access output
21 /// stream. Used for all Lucene index output operations.
23 /// <seealso cref="Directory">
25 /// <seealso cref="InputStream">
27 public abstract class OutputStream
29 internal const int BUFFER_SIZE
= 1024;
31 private byte[] buffer
= new byte[BUFFER_SIZE
];
32 private long bufferStart
= 0; // position in file of buffer
33 private int bufferPosition
= 0; // position in buffer
35 /// <summary>Writes a single byte.</summary>
36 /// <seealso cref="InputStream#ReadByte()">
38 public void WriteByte(byte b
)
40 if (bufferPosition
>= BUFFER_SIZE
)
42 buffer
[bufferPosition
++] = b
;
45 /// <summary>Writes an array of bytes.</summary>
46 /// <param name="b">the bytes to write
48 /// <param name="length">the number of bytes to write
50 /// <seealso cref="InputStream#ReadBytes(byte[],int,int)">
52 public void WriteBytes(byte[] b
, int length
)
54 for (int i
= 0; i
< length
; i
++)
58 /// <summary>Writes an int as four bytes.</summary>
59 /// <seealso cref="InputStream#ReadInt()">
61 public void WriteInt(int i
)
63 WriteByte((byte) (i
>> 24));
64 WriteByte((byte) (i
>> 16));
65 WriteByte((byte) (i
>> 8));
69 /// <summary>Writes an int in a variable-length format. Writes between one and
70 /// five bytes. Smaller values take fewer bytes. Negative numbers are not
73 /// <seealso cref="InputStream#ReadVInt()">
75 public void WriteVInt(int i
)
77 while ((i
& ~
0x7F) != 0)
79 WriteByte((byte) ((i
& 0x7f) | 0x80));
80 i
= (int) (((uint) i
) >> 7);
85 /// <summary>Writes a long as eight bytes.</summary>
86 /// <seealso cref="InputStream#ReadLong()">
88 public void WriteLong(long i
)
90 WriteInt((int) (i
>> 32));
94 /// <summary>Writes an long in a variable-length format. Writes between one and five
95 /// bytes. Smaller values take fewer bytes. Negative numbers are not
98 /// <seealso cref="InputStream#ReadVLong()">
100 public void WriteVLong(long i
)
102 while ((i
& ~
0x7F) != 0)
104 WriteByte((byte) ((i
& 0x7f) | 0x80));
105 i
= (long) (((long) i
) >> 7);
110 /// <summary>Writes a string.</summary>
111 /// <seealso cref="InputStream#ReadString()">
113 public void WriteString(System
.String s
)
115 int length
= s
.Length
;
117 WriteChars(s
, 0, length
);
120 /// <summary>Writes a sequence of UTF-8 encoded characters from a string.</summary>
121 /// <param name="s">the source of the characters
123 /// <param name="start">the first character in the sequence
125 /// <param name="length">the number of characters in the sequence
127 /// <seealso cref="InputStream#ReadChars(char[],int,int)">
129 public void WriteChars(System
.String s
, int start
, int length
)
131 int end
= start
+ length
;
132 for (int i
= start
; i
< end
; i
++)
134 int code
= (int) s
[i
];
135 if (code
>= 0x01 && code
<= 0x7F)
136 WriteByte((byte) code
);
137 else if (((code
>= 0x80) && (code
<= 0x7FF)) || code
== 0)
139 WriteByte((byte) (0xC0 | (code
>> 6)));
140 WriteByte((byte) (0x80 | (code
& 0x3F)));
144 WriteByte((byte) (0xE0 | (((uint) code
) >> 12)));
145 WriteByte((byte) (0x80 | ((code
>> 6) & 0x3F)));
146 WriteByte((byte) (0x80 | (code
& 0x3F)));
151 /// <summary>Forces any buffered output to be written. </summary>
152 protected internal void Flush()
154 FlushBuffer(buffer
, bufferPosition
);
155 bufferStart
+= bufferPosition
;
159 /// <summary>Expert: implements buffer write. Writes bytes at the current position in
162 /// <param name="b">the bytes to write
164 /// <param name="len">the number of bytes to write
166 public abstract void FlushBuffer(byte[] b
, int len
);
168 /// <summary>Closes this stream to further operations. </summary>
169 public virtual void Close()
174 /// <summary>Returns the current position in this file, where the next write will
177 /// <seealso cref="#Seek(long)">
179 public long GetFilePointer()
181 return bufferStart
+ bufferPosition
;
184 /// <summary>Sets current position in this file, where the next write will occur.</summary>
185 /// <seealso cref="#GetFilePointer()">
187 public virtual void Seek(long pos
)
193 /// <summary>The number of bytes in the file. </summary>
194 public abstract long Length();