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.hbase.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 UVLongTool{
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      LONG_7_RIGHT_BITS_SET = 127,
40      LONG_8TH_BIT_SET = 128;
41  
42    public static final byte[]
43      MAX_VALUE_BYTES = new byte[] { -1, -1, -1, -1, -1, -1, -1, -1, 127 };
44  
45  
46    /********************* long -> bytes **************************/
47  
48    public static int numBytes(long in) {// do a check for illegal arguments if not protected
49      if (in == 0) {
50        return 1;
51      }// doesn't work with the formula below
52      return (70 - Long.numberOfLeadingZeros(in)) / 7;// 70 comes from 64+(7-1)
53    }
54  
55    public static byte[] getBytes(long value) {
56      int numBytes = numBytes(value);
57      byte[] bytes = new byte[numBytes];
58      long remainder = value;
59      for (int i = 0; i < numBytes - 1; ++i) {
60        bytes[i] = (byte) ((remainder & LONG_7_RIGHT_BITS_SET) | LONG_8TH_BIT_SET);// set the left bit
61        remainder >>= 7;
62      }
63      bytes[numBytes - 1] = (byte) (remainder & LONG_7_RIGHT_BITS_SET);// do not set the left bit
64      return bytes;
65    }
66  
67    public static int writeBytes(long value, OutputStream os) throws IOException {
68      int numBytes = numBytes(value);
69      long remainder = value;
70      for (int i = 0; i < numBytes - 1; ++i) {
71        // set the left bit
72        os.write((byte) ((remainder & LONG_7_RIGHT_BITS_SET) | LONG_8TH_BIT_SET));
73        remainder >>= 7;
74      }
75      // do not set the left bit
76      os.write((byte) (remainder & LONG_7_RIGHT_BITS_SET));
77      return numBytes;
78    }
79  
80    /******************** bytes -&gt; long **************************/
81  
82    public static long getLong(byte[] bytes) {
83      return getLong(bytes, 0);
84    }
85  
86    public static long getLong(byte[] bytes, int offset) {
87      long value = 0;
88      for (int i = 0;; ++i) {
89        byte b = bytes[offset + i];
90        long shifted = BYTE_7_RIGHT_BITS_SET & b;// kill leftmost bit
91        shifted <<= 7 * i;
92        value |= shifted;
93        if (b >= 0) {
94          break;
95        }// first bit was 0, so that's the last byte in the VarLong
96      }
97      return value;
98    }
99  
100   public static long getLong(InputStream is) throws IOException {
101     long value = 0;
102     int i = 0;
103     int b;
104     do {
105       b = is.read();
106       long shifted = BYTE_7_RIGHT_BITS_SET & b;// kill leftmost bit
107       shifted <<= 7 * i;
108       value |= shifted;
109       ++i;
110     } while (b > Byte.MAX_VALUE);
111     return value;
112   }
113 }