1 /* 2 * Copyright 2014 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc.internal; 18 19 /** 20 * Abstract base class for {@link ReadableBuffer} implementations. 21 */ 22 public abstract class AbstractReadableBuffer implements ReadableBuffer { 23 @Override readInt()24 public final int readInt() { 25 checkReadable(4); 26 int b1 = readUnsignedByte(); 27 int b2 = readUnsignedByte(); 28 int b3 = readUnsignedByte(); 29 int b4 = readUnsignedByte(); 30 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; 31 } 32 33 @Override hasArray()34 public boolean hasArray() { 35 return false; 36 } 37 38 @Override array()39 public byte[] array() { 40 throw new UnsupportedOperationException(); 41 } 42 43 @Override arrayOffset()44 public int arrayOffset() { 45 throw new UnsupportedOperationException(); 46 } 47 48 @Override close()49 public void close() {} 50 checkReadable(int length)51 protected final void checkReadable(int length) { 52 if (readableBytes() < length) { 53 throw new IndexOutOfBoundsException(); 54 } 55 } 56 } 57