AllocatedBuffer.java
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 
31 package com.google.protobuf;
32 
33 import static com.google.protobuf.Internal.checkNotNull;
34 
35 import java.nio.ByteBuffer;
36 
41 @ExperimentalApi
42 abstract class AllocatedBuffer {
47  public abstract boolean hasNioBuffer();
48 
53  public abstract boolean hasArray();
54 
64  public abstract ByteBuffer nioBuffer();
65 
76  public abstract byte[] array();
77 
92  public abstract int arrayOffset();
93 
99  public abstract int position();
100 
109  public abstract AllocatedBuffer position(int position);
110 
116  public abstract int limit();
117 
124  public abstract int remaining();
125 
131  public static AllocatedBuffer wrap(byte[] bytes) {
132  return wrapNoCheck(bytes, 0, bytes.length);
133  }
134 
140  public static AllocatedBuffer wrap(final byte[] bytes, final int offset, final int length) {
141  if (offset < 0 || length < 0 || (offset + length) > bytes.length) {
142  throw new IndexOutOfBoundsException(
143  String.format("bytes.length=%d, offset=%d, length=%d", bytes.length, offset, length));
144  }
145 
146  return wrapNoCheck(bytes, offset, length);
147  }
148 
153  public static AllocatedBuffer wrap(final ByteBuffer buffer) {
154  checkNotNull(buffer, "buffer");
155 
156  return new AllocatedBuffer() {
157 
158  @Override
159  public boolean hasNioBuffer() {
160  return true;
161  }
162 
163  @Override
164  public ByteBuffer nioBuffer() {
165  return buffer;
166  }
167 
168  @Override
169  public boolean hasArray() {
170  return buffer.hasArray();
171  }
172 
173  @Override
174  public byte[] array() {
175  return buffer.array();
176  }
177 
178  @Override
179  public int arrayOffset() {
180  return buffer.arrayOffset();
181  }
182 
183  @Override
184  public int position() {
185  return buffer.position();
186  }
187 
188  @Override
189  public AllocatedBuffer position(int position) {
190  buffer.position(position);
191  return this;
192  }
193 
194  @Override
195  public int limit() {
196  return buffer.limit();
197  }
198 
199  @Override
200  public int remaining() {
201  return buffer.remaining();
202  }
203  };
204  }
205 
206  private static AllocatedBuffer wrapNoCheck(
207  final byte[] bytes, final int offset, final int length) {
208  return new AllocatedBuffer() {
209  // Relative to offset.
210  private int position;
211 
212  @Override
213  public boolean hasNioBuffer() {
214  return false;
215  }
216 
217  @Override
218  public ByteBuffer nioBuffer() {
219  throw new UnsupportedOperationException();
220  }
221 
222  @Override
223  public boolean hasArray() {
224  return true;
225  }
226 
227  @Override
228  public byte[] array() {
229  return bytes;
230  }
231 
232  @Override
233  public int arrayOffset() {
234  return offset;
235  }
236 
237  @Override
238  public int position() {
239  return position;
240  }
241 
242  @Override
243  public AllocatedBuffer position(int position) {
244  if (position < 0 || position > length) {
245  throw new IllegalArgumentException("Invalid position: " + position);
246  }
247  this.position = position;
248  return this;
249  }
250 
251  @Override
252  public int limit() {
253  // Relative to offset.
254  return length;
255  }
256 
257  @Override
258  public int remaining() {
259  return length - position;
260  }
261  };
262  }
263 }
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
bytes
uint8 bytes[10]
Definition: coded_stream_unittest.cc:153
offset
GLintptr offset
Definition: glcorearb.h:2944
buffer
GLuint buffer
Definition: glcorearb.h:2939
buffer
Definition: buffer_processor.h:43
position
intern position
Definition: array.c:487
java
com.google
com
com.google.protobuf.Internal
Definition: Internal.java:54
array
PHP_PROTO_OBJECT_FREE_END PHP_PROTO_OBJECT_DTOR_END intern array
Definition: array.c:111


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:47