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 import java.nio.ByteBuffer; 20 21 /** 22 * Abstract base class for {@link ReadableBuffer} implementations. 23 */ 24 public abstract class AbstractReadableBuffer implements ReadableBuffer { 25 @Override readInt()26 public final int readInt() { 27 checkReadable(4); 28 int b1 = readUnsignedByte(); 29 int b2 = readUnsignedByte(); 30 int b3 = readUnsignedByte(); 31 int b4 = readUnsignedByte(); 32 return (b1 << 24) | (b2 << 16) | (b3 << 8) | b4; 33 } 34 35 @Override hasArray()36 public boolean hasArray() { 37 return false; 38 } 39 40 @Override array()41 public byte[] array() { 42 throw new UnsupportedOperationException(); 43 } 44 45 @Override arrayOffset()46 public int arrayOffset() { 47 throw new UnsupportedOperationException(); 48 } 49 50 @Override markSupported()51 public boolean markSupported() { 52 return false; 53 } 54 55 @Override mark()56 public void mark() {} 57 58 @Override reset()59 public void reset() { 60 throw new UnsupportedOperationException(); 61 } 62 63 @Override byteBufferSupported()64 public boolean byteBufferSupported() { 65 return false; 66 } 67 68 @Override getByteBuffer()69 public ByteBuffer getByteBuffer() { 70 throw new UnsupportedOperationException(); 71 } 72 73 @Override close()74 public void close() {} 75 checkReadable(int length)76 protected final void checkReadable(int length) { 77 if (readableBytes() < length) { 78 throw new IndexOutOfBoundsException(); 79 } 80 } 81 } 82