Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Store / OutputStream.cs
blobbe0c32021d31c59c2baa05b52f34b31e751be39b
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
16 using System;
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.
22 /// </summary>
23 /// <seealso cref="Directory">
24 /// </seealso>
25 /// <seealso cref="InputStream">
26 /// </seealso>
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()">
37 /// </seealso>
38 public void WriteByte(byte b)
40 if (bufferPosition >= BUFFER_SIZE)
41 Flush();
42 buffer[bufferPosition++] = b;
45 /// <summary>Writes an array of bytes.</summary>
46 /// <param name="b">the bytes to write
47 /// </param>
48 /// <param name="length">the number of bytes to write
49 /// </param>
50 /// <seealso cref="InputStream#ReadBytes(byte[],int,int)">
51 /// </seealso>
52 public void WriteBytes(byte[] b, int length)
54 for (int i = 0; i < length; i++)
55 WriteByte(b[i]);
58 /// <summary>Writes an int as four bytes.</summary>
59 /// <seealso cref="InputStream#ReadInt()">
60 /// </seealso>
61 public void WriteInt(int i)
63 WriteByte((byte) (i >> 24));
64 WriteByte((byte) (i >> 16));
65 WriteByte((byte) (i >> 8));
66 WriteByte((byte) i);
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
71 /// supported.
72 /// </summary>
73 /// <seealso cref="InputStream#ReadVInt()">
74 /// </seealso>
75 public void WriteVInt(int i)
77 while ((i & ~ 0x7F) != 0)
79 WriteByte((byte) ((i & 0x7f) | 0x80));
80 i = (int) (((uint) i) >> 7);
82 WriteByte((byte) i);
85 /// <summary>Writes a long as eight bytes.</summary>
86 /// <seealso cref="InputStream#ReadLong()">
87 /// </seealso>
88 public void WriteLong(long i)
90 WriteInt((int) (i >> 32));
91 WriteInt((int) i);
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
96 /// supported.
97 /// </summary>
98 /// <seealso cref="InputStream#ReadVLong()">
99 /// </seealso>
100 public void WriteVLong(long i)
102 while ((i & ~ 0x7F) != 0)
104 WriteByte((byte) ((i & 0x7f) | 0x80));
105 i = (long) (((long) i) >> 7);
107 WriteByte((byte) i);
110 /// <summary>Writes a string.</summary>
111 /// <seealso cref="InputStream#ReadString()">
112 /// </seealso>
113 public void WriteString(System.String s)
115 int length = s.Length;
116 WriteVInt(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
122 /// </param>
123 /// <param name="start">the first character in the sequence
124 /// </param>
125 /// <param name="length">the number of characters in the sequence
126 /// </param>
127 /// <seealso cref="InputStream#ReadChars(char[],int,int)">
128 /// </seealso>
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)));
142 else
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;
156 bufferPosition = 0;
159 /// <summary>Expert: implements buffer write. Writes bytes at the current position in
160 /// the output.
161 /// </summary>
162 /// <param name="b">the bytes to write
163 /// </param>
164 /// <param name="len">the number of bytes to write
165 /// </param>
166 public abstract void FlushBuffer(byte[] b, int len);
168 /// <summary>Closes this stream to further operations. </summary>
169 public virtual void Close()
171 Flush();
174 /// <summary>Returns the current position in this file, where the next write will
175 /// occur.
176 /// </summary>
177 /// <seealso cref="#Seek(long)">
178 /// </seealso>
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()">
186 /// </seealso>
187 public virtual void Seek(long pos)
189 Flush();
190 bufferStart = pos;
193 /// <summary>The number of bytes in the file. </summary>
194 public abstract long Length();