add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / facet / src / java / org / apache / lucene / util / encoding / FourFlagsIntDecoder.java
1 package org.apache.lucene.util.encoding;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5
6 /**
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
13  *
14  *     http://www.apache.org/licenses/LICENSE-2.0
15  *
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.
21  */
22
23 /**
24  * Decodes data which was encoded by {@link FourFlagsIntEncoder}. Scans
25  * the <code>indicator</code>, one flag (1-bits) at a time, and decodes extra
26  * data using {@link VInt8IntDecoder}.
27  * 
28  * @see FourFlagsIntEncoder
29  * @lucene.experimental
30  */
31 public class FourFlagsIntDecoder extends IntDecoder {
32
33   /**
34    * Holds all combinations of <i>indicator</i> for fast decoding (saves time
35    * on real-time bit manipulation)
36    */
37   private final static byte[][] decodeTable = new byte[256][4];
38
39   /** Generating all combinations of <i>indicator</i> into separate flags. */
40   static {
41     for (int i = 256; i != 0;) {
42       --i;
43       for (int j = 4; j != 0;) {
44         --j;
45         decodeTable[i][j] = (byte) ((i >>> (j << 1)) & 0x3);
46       }
47     }
48   }
49
50   private final IntDecoder decoder = new VInt8IntDecoder();
51
52   /** The indicator for decoding a chunk of 4 integers. */
53   private int indicator;
54
55   /** Used as an ordinal of 0 - 3, as the decoder decodes chunks of 4 integers. */
56   private int ordinal = 0;
57
58   @Override
59   public long decode() throws IOException {
60     // If we've decoded 8 integers, read the next indicator.
61     if ((ordinal & 0x3) == 0) {
62       indicator = in.read();
63       if (indicator < 0) {
64         return EOS;
65       }
66       ordinal = 0;
67     }
68
69     byte decodeVal = decodeTable[indicator][ordinal++];
70     if (decodeVal == 0) {
71       // decode the value from the stream.
72       long decode = decoder.decode();
73       return decode == EOS ? EOS : decode + 4;
74     }
75
76     return decodeVal;
77   }
78
79   @Override
80   public void reInit(InputStream in) {
81     super.reInit(in);
82     decoder.reInit(in);
83     ordinal = 0;
84     indicator = 0;
85   }
86
87   @Override
88   public String toString() {
89     return "FourFlags (VInt8)";
90   }
91
92 }