001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 *
018 * The portion of this file denoted by 'Copied from com.google.protobuf.CodedInputStream'
019 * is from Protocol Buffers v2.4.1 under the following license
020 *
021 * Copyright 2008 Google Inc.  All rights reserved.
022 * http://code.google.com/p/protobuf/
023 *
024 * Redistribution and use in source and binary forms, with or without
025 * modification, are permitted provided that the following conditions are
026 * met:
027 *
028 *     * Redistributions of source code must retain the above copyright
029 * notice, this list of conditions and the following disclaimer.
030 *     * Redistributions in binary form must reproduce the above
031 * copyright notice, this list of conditions and the following disclaimer
032 * in the documentation and/or other materials provided with the
033 * distribution.
034 *     * Neither the name of Google Inc. nor the names of its
035 * contributors may be used to endorse or promote products derived from
036 * this software without specific prior written permission.
037 *
038 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
039 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
040 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
041 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
042 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
043 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
044 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
045 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
046 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
047 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
048 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
049 *
050 */
051
052package org.apache.hadoop.hbase.util;
053
054import java.io.DataInput;
055import java.io.IOException;
056
057import org.apache.yetus.audience.InterfaceAudience;
058
059@InterfaceAudience.Private
060public abstract class ProtoUtil {
061
062  /**
063   * Read a variable length integer in the same format that ProtoBufs encodes.
064   * @param in the input stream to read from
065   * @return the integer
066   * @throws IOException if it is malformed or EOF.
067   */
068  public static int readRawVarint32(DataInput in) throws IOException {
069  // Copied from com.google.protobuf.CodedInputStream v2.4.1 readRawVarint32
070    byte tmp = in.readByte();
071    if (tmp >= 0) {
072      return tmp;
073    }
074    int result = tmp & 0x7f;
075    if ((tmp = in.readByte()) >= 0) {
076      result |= tmp << 7;
077    } else {
078      result |= (tmp & 0x7f) << 7;
079      if ((tmp = in.readByte()) >= 0) {
080        result |= tmp << 14;
081      } else {
082        result |= (tmp & 0x7f) << 14;
083        if ((tmp = in.readByte()) >= 0) {
084          result |= tmp << 21;
085        } else {
086          result |= (tmp & 0x7f) << 21;
087          result |= (tmp = in.readByte()) << 28;
088          if (tmp < 0) {
089            // Discard upper 32 bits.
090            for (int i = 0; i < 5; i++) {
091              if (in.readByte() >= 0) {
092                return result;
093              }
094            }
095            throw new IOException("Malformed varint");
096          }
097        }
098      }
099    }
100    return result;
101  }
102  // end of copied from protobuf
103
104}