View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.util.vint;
20  
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  
25  import org.apache.hadoop.classification.InterfaceAudience;
26  
27  /**
28   * Simple Variable Length Integer encoding.  Left bit of 0 means we are on the last byte.  If left
29   * bit of the current byte is 1, then there is at least one more byte.
30   */
31  @InterfaceAudience.Private
32  public class UVIntTool {
33  
34    public static final byte
35      BYTE_7_RIGHT_BITS_SET = 127,
36      BYTE_LEFT_BIT_SET = -128;
37  
38    public static final long
39      INT_7_RIGHT_BITS_SET = 127,
40      INT_8TH_BIT_SET = 128;
41  
42    public static final byte[]
43      MAX_VALUE_BYTES = new byte[] { -1, -1, -1, -1, 7 };
44  
45    /********************* int -> bytes **************************/
46  
47    public static int numBytes(int in) {
48      if (in == 0) {
49        // doesn't work with the formula below
50        return 1;
51      }
52      return (38 - Integer.numberOfLeadingZeros(in)) / 7;// 38 comes from 32+(7-1)
53    }
54  
55    public static byte[] getBytes(int value) {
56      int numBytes = numBytes(value);
57      byte[] bytes = new byte[numBytes];
58      int remainder = value;
59      for (int i = 0; i < numBytes - 1; ++i) {
60        // set the left bit
61        bytes[i] = (byte) ((remainder & INT_7_RIGHT_BITS_SET) | INT_8TH_BIT_SET);
62        remainder >>= 7;
63      }
64      // do not set the left bit
65      bytes[numBytes - 1] = (byte) (remainder & INT_7_RIGHT_BITS_SET);
66      return bytes;
67    }
68  
69    public static int writeBytes(int value, OutputStream os) throws IOException {
70      int numBytes = numBytes(value);
71      int remainder = value;
72      for (int i = 0; i < numBytes - 1; ++i) {
73        // set the left bit
74        os.write((byte) ((remainder & INT_7_RIGHT_BITS_SET) | INT_8TH_BIT_SET));
75        remainder >>= 7;
76      }
77      // do not set the left bit
78      os.write((byte) (remainder & INT_7_RIGHT_BITS_SET));
79      return numBytes;
80    }
81  
82    /******************** bytes -> int **************************/
83  
84    public static int getInt(byte[] bytes) {
85      return getInt(bytes, 0);
86    }
87  
88    public static int getInt(byte[] bytes, int offset) {
89      int value = 0;
90      for (int i = 0;; ++i) {
91        byte b = bytes[offset + i];
92        int shifted = BYTE_7_RIGHT_BITS_SET & b;// kill leftmost bit
93        shifted <<= 7 * i;
94        value |= shifted;
95        if (b >= 0) {
96          break;
97        }
98      }
99      return value;
100   }
101 
102   public static int getInt(InputStream is) throws IOException {
103     int value = 0;
104     int i = 0;
105     int b;
106     do{
107       b = is.read();
108       int shifted = BYTE_7_RIGHT_BITS_SET & b;// kill leftmost bit
109       shifted <<= 7 * i;
110       value |= shifted;
111       ++i;
112     }while(b > Byte.MAX_VALUE);
113     return value;
114   }
115 }