1 package org.apache.lucene.util.encoding;
3 import java.io.IOException;
4 import java.io.InputStream;
7 * Licensed to the Apache Software Foundation (ASF) under one or more
8 * contributor license agreements. See the NOTICE file distributed with
9 * this work for additional information regarding copyright ownership.
10 * The ASF licenses this file to You under the Apache License, Version 2.0
11 * (the "License"); you may not use this file except in compliance with
12 * the License. You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
24 * Decodes data which was encoded by {@link EightFlagsIntEncoder}. Scans
25 * the <code>indicator</code>, one flag (1-bits) at a time, and decodes extra
26 * data using {@link VInt8IntDecoder}.
28 * @see EightFlagsIntEncoder
29 * @lucene.experimental
31 public class EightFlagsIntDecoder extends IntDecoder {
34 * Holds all combinations of <i>indicator</i> for fast decoding (saves time
35 * on real-time bit manipulation)
37 private static final byte[][] decodeTable = new byte[256][8];
39 /** Generating all combinations of <i>indicator</i> into separate flags. */
41 for (int i = 256; i != 0;) {
43 for (int j = 8; j != 0;) {
45 decodeTable[i][j] = (byte) ((i >>> j) & 0x1);
50 private final IntDecoder decoder = new VInt8IntDecoder();
52 /** The indicator for decoding a chunk of 8 integers. */
53 private int indicator;
55 /** Used as an ordinal of 0 - 7, as the decoder decodes chunks of 8 integers. */
56 private int ordinal = 0;
59 public long decode() throws IOException {
60 // If we've decoded 8 integers, read the next indicator.
61 if ((ordinal & 0x7) == 0) {
62 indicator = in.read();
69 if (decodeTable[indicator][ordinal++] == 0) {
70 // decode the value from the stream.
71 long decode = decoder.decode();
72 return decode == EOS ? EOS : decode + 2;
79 public void reInit(InputStream in) {
87 public String toString() {
88 return "EightFlags (VInt8)";