31 package com.google.protobuf;
35 import java.nio.ByteBuffer;
42 abstract class AllocatedBuffer {
47 public abstract boolean hasNioBuffer();
53 public abstract boolean hasArray();
64 public abstract ByteBuffer nioBuffer();
76 public abstract byte[]
array();
92 public abstract int arrayOffset();
116 public abstract int limit();
124 public abstract int remaining();
131 public static AllocatedBuffer wrap(
byte[]
bytes) {
140 public static AllocatedBuffer wrap(
final byte[]
bytes,
final int offset,
final int length) {
142 throw new IndexOutOfBoundsException(
143 String.format(
"bytes.length=%d, offset=%d, length=%d",
bytes.length,
offset,
length));
153 public static AllocatedBuffer wrap(
final ByteBuffer
buffer) {
154 checkNotNull(
buffer,
"buffer");
156 return new AllocatedBuffer() {
159 public boolean hasNioBuffer() {
164 public ByteBuffer nioBuffer() {
169 public boolean hasArray() {
174 public byte[]
array() {
179 public int arrayOffset() {
180 return buffer.arrayOffset();
200 public int remaining() {
201 return buffer.remaining();
206 private static AllocatedBuffer wrapNoCheck(
208 return new AllocatedBuffer() {
213 public boolean hasNioBuffer() {
218 public ByteBuffer nioBuffer() {
219 throw new UnsupportedOperationException();
223 public boolean hasArray() {
228 public byte[]
array() {
233 public int arrayOffset() {
244 if (position < 0 || position >
length) {
245 throw new IllegalArgumentException(
"Invalid position: " +
position);
258 public int remaining() {