IterableByteBufferInputStream.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 
34 
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.nio.ByteBuffer;
38 import java.util.Iterator;
39 
40 class IterableByteBufferInputStream extends InputStream {
42  private Iterator<ByteBuffer> iterator;
44  private ByteBuffer currentByteBuffer;
46  private int dataSize;
52  private int currentIndex;
54  private int currentByteBufferPos;
56  private boolean hasArray;
61  private byte[] currentArray;
63  private int currentArrayOffset;
68  private long currentAddress;
69 
70  IterableByteBufferInputStream(Iterable<ByteBuffer> data) {
71  iterator = data.iterator();
72  dataSize = 0;
73  for (ByteBuffer unused : data) {
74  dataSize++;
75  }
76  currentIndex = -1;
77 
78  if (!getNextByteBuffer()) {
79  currentByteBuffer = EMPTY_BYTE_BUFFER;
80  currentIndex = 0;
81  currentByteBufferPos = 0;
82  currentAddress = 0;
83  }
84  }
85 
86  private boolean getNextByteBuffer() {
87  currentIndex++;
88  if (!iterator.hasNext()) {
89  return false;
90  }
91  currentByteBuffer = iterator.next();
92  currentByteBufferPos = currentByteBuffer.position();
93  if (currentByteBuffer.hasArray()) {
94  hasArray = true;
95  currentArray = currentByteBuffer.array();
96  currentArrayOffset = currentByteBuffer.arrayOffset();
97  } else {
98  hasArray = false;
99  currentAddress = UnsafeUtil.addressOffset(currentByteBuffer);
100  currentArray = null;
101  }
102  return true;
103  }
104 
105  private void updateCurrentByteBufferPos(int numberOfBytesRead) {
106  currentByteBufferPos += numberOfBytesRead;
107  if (currentByteBufferPos == currentByteBuffer.limit()) {
108  getNextByteBuffer();
109  }
110  }
111 
112  @Override
113  public int read() throws IOException {
114  if (currentIndex == dataSize) {
115  return -1;
116  }
117  if (hasArray) {
118  int result = currentArray[currentByteBufferPos + currentArrayOffset] & 0xFF;
119  updateCurrentByteBufferPos(1);
120  return result;
121  } else {
122  int result = UnsafeUtil.getByte(currentByteBufferPos + currentAddress) & 0xFF;
123  updateCurrentByteBufferPos(1);
124  return result;
125  }
126  }
127 
128  @Override
129  public int read(byte[] output, int offset, int length) throws IOException {
130  if (currentIndex == dataSize) {
131  return -1;
132  }
133  int remaining = currentByteBuffer.limit() - currentByteBufferPos;
134  if (length > remaining) {
135  length = remaining;
136  }
137  if (hasArray) {
138  System.arraycopy(
139  currentArray, currentByteBufferPos + currentArrayOffset, output, offset, length);
140  updateCurrentByteBufferPos(length);
141  } else {
142  int prevPos = currentByteBuffer.position();
143  currentByteBuffer.position(currentByteBufferPos);
144  currentByteBuffer.get(output, offset, length);
145  currentByteBuffer.position(prevPos);
146  updateCurrentByteBufferPos(length);
147  }
148  return length;
149  }
150 }
length
GLenum GLuint GLenum GLsizei length
Definition: glcorearb.h:2695
com.google.protobuf
Definition: ProtoCaliperBenchmark.java:2
com.google.protobuf.Internal.EMPTY_BYTE_BUFFER
static final ByteBuffer EMPTY_BYTE_BUFFER
Definition: Internal.java:378
offset
GLintptr offset
Definition: glcorearb.h:2944
java
com.google
com
data
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: glcorearb.h:2879
com.google.protobuf.Internal
Definition: Internal.java:54
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503


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