Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
ByteSliceWriter.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using Lucene.Net.Support;
19 
20 namespace Lucene.Net.Index
21 {
22  /// <summary> Class to write byte streams into slices of shared
23  /// byte[]. This is used by DocumentsWriter to hold the
24  /// posting list for many terms in RAM.
25  /// </summary>
26  public sealed class ByteSliceWriter
27  {
28  private byte[] slice;
29  private int upto;
30  private readonly ByteBlockPool pool;
31 
32  internal int offset0;
33 
35  {
36  this.pool = pool;
37  }
38 
39  /// <summary> Set up the writer to write at address.</summary>
40  public void Init(int address)
41  {
42  slice = pool.buffers[address >> DocumentsWriter.BYTE_BLOCK_SHIFT];
43  System.Diagnostics.Debug.Assert(slice != null);
44  upto = address & DocumentsWriter.BYTE_BLOCK_MASK;
45  offset0 = address;
46  System.Diagnostics.Debug.Assert(upto < slice.Length);
47  }
48 
49  /// <summary>Write byte into byte slice stream </summary>
50  public void WriteByte(byte b)
51  {
52  System.Diagnostics.Debug.Assert(slice != null);
53  if (slice[upto] != 0)
54  {
55  upto = pool.AllocSlice(slice, upto);
56  slice = pool.buffer;
57  offset0 = pool.byteOffset;
58  System.Diagnostics.Debug.Assert(slice != null);
59  }
60  slice[upto++] = b;
61  System.Diagnostics.Debug.Assert(upto != slice.Length);
62  }
63 
64  public void WriteBytes(byte[] b, int offset, int len)
65  {
66  int offsetEnd = offset + len;
67  while (offset < offsetEnd)
68  {
69  if (slice[upto] != 0)
70  {
71  // End marker
72  upto = pool.AllocSlice(slice, upto);
73  slice = pool.buffer;
74  offset0 = pool.byteOffset;
75  }
76 
77  slice[upto++] = b[offset++];
78  System.Diagnostics.Debug.Assert(upto != slice.Length);
79  }
80  }
81 
82  public int Address
83  {
84  get { return upto + (offset0 & DocumentsWriter.BYTE_BLOCK_NOT_MASK); }
85  }
86 
87  public void WriteVInt(int i)
88  {
89  while ((i & ~ 0x7F) != 0)
90  {
91  WriteByte((byte) ((i & 0x7f) | 0x80));
92  i = Number.URShift(i, 7);
93  }
94  WriteByte((byte) i);
95  }
96  }
97 }