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 * The portion of this file denoted by 'Copied from com.google.protobuf.CodedInputStream'
19 * is from Protocol Buffers v2.4.1 under the following license
20 *
21 * Copyright 2008 Google Inc. All rights reserved.
22 * http://code.google.com/p/protobuf/
23 *
24 * Redistribution and use in source and binary forms, with or without
25 * modification, are permitted provided that the following conditions are
26 * met:
27 *
28 * * Redistributions of source code must retain the above copyright
29 * notice, this list of conditions and the following disclaimer.
30 * * Redistributions in binary form must reproduce the above
31 * copyright notice, this list of conditions and the following disclaimer
32 * in the documentation and/or other materials provided with the
33 * distribution.
34 * * Neither the name of Google Inc. nor the names of its
35 * contributors may be used to endorse or promote products derived from
36 * this software without specific prior written permission.
37 *
38 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
39 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
40 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
41 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
42 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
44 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
45 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
46 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
47 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
48 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
49 *
50 */
51
52 package org.apache.hadoop.hbase.util;
53
54 import java.io.DataInput;
55 import java.io.IOException;
56
57 import org.apache.hadoop.hbase.classification.InterfaceAudience;
58
59 @InterfaceAudience.Private
60 public abstract class ProtoUtil {
61
62 /**
63 * Read a variable length integer in the same format that ProtoBufs encodes.
64 * @param in the input stream to read from
65 * @return the integer
66 * @throws IOException if it is malformed or EOF.
67 */
68 public static int readRawVarint32(DataInput in) throws IOException {
69 // Copied from com.google.protobuf.CodedInputStream v2.4.1 readRawVarint32
70 byte tmp = in.readByte();
71 if (tmp >= 0) {
72 return tmp;
73 }
74 int result = tmp & 0x7f;
75 if ((tmp = in.readByte()) >= 0) {
76 result |= tmp << 7;
77 } else {
78 result |= (tmp & 0x7f) << 7;
79 if ((tmp = in.readByte()) >= 0) {
80 result |= tmp << 14;
81 } else {
82 result |= (tmp & 0x7f) << 14;
83 if ((tmp = in.readByte()) >= 0) {
84 result |= tmp << 21;
85 } else {
86 result |= (tmp & 0x7f) << 21;
87 result |= (tmp = in.readByte()) << 28;
88 if (tmp < 0) {
89 // Discard upper 32 bits.
90 for (int i = 0; i < 5; i++) {
91 if (in.readByte() >= 0) {
92 return result;
93 }
94 }
95 throw new IOException("Malformed varint");
96 }
97 }
98 }
99 }
100 return result;
101 }
102 // end of copied from protobuf
103
104 }