1 package org.apache.lucene.index;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
22 * 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.
27 final class ByteSliceWriter {
31 private final ByteBlockPool pool;
35 public ByteSliceWriter(ByteBlockPool pool) {
40 * Set up the writer to write at address.
42 public void init(int address) {
43 slice = pool.buffers[address >> DocumentsWriter.BYTE_BLOCK_SHIFT];
45 upto = address & DocumentsWriter.BYTE_BLOCK_MASK;
47 assert upto < slice.length;
50 /** Write byte into byte slice stream */
51 public void writeByte(byte b) {
53 if (slice[upto] != 0) {
54 upto = pool.allocSlice(slice, upto);
56 offset0 = pool.byteOffset;
60 assert upto != slice.length;
63 public void writeBytes(final byte[] b, int offset, final int len) {
64 final int offsetEnd = offset + len;
65 while(offset < offsetEnd) {
66 if (slice[upto] != 0) {
68 upto = pool.allocSlice(slice, upto);
70 offset0 = pool.byteOffset;
73 slice[upto++] = b[offset++];
74 assert upto != slice.length;
78 public int getAddress() {
79 return upto + (offset0 & DocumentsWriter.BYTE_BLOCK_NOT_MASK);
82 public void writeVInt(int i) {
83 while ((i & ~0x7F) != 0) {
84 writeByte((byte)((i & 0x7f) | 0x80));