• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.EMPTY_BYTE_BUFFER;
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 {
41   /** The {@link Iterator} with type {@link ByteBuffer} of {@code input} */
42   private Iterator<ByteBuffer> iterator;
43   /** The current ByteBuffer; */
44   private ByteBuffer currentByteBuffer;
45   /** The number of ByteBuffers in the input data. */
46   private int dataSize;
47   /**
48    * Current {@code ByteBuffer}'s index
49    *
50    * <p>If index equals dataSize, then all the data in the InputStream has been consumed
51    */
52   private int currentIndex;
53   /** The current position for current ByteBuffer */
54   private int currentByteBufferPos;
55   /** Whether current ByteBuffer has an array */
56   private boolean hasArray;
57   /**
58    * If the current ByteBuffer is unsafe-direct based, currentArray is null; otherwise should be the
59    * array inside ByteBuffer.
60    */
61   private byte[] currentArray;
62   /** Current ByteBuffer's array offset */
63   private int currentArrayOffset;
64   /**
65    * If the current ByteBuffer is unsafe-direct based, currentAddress is the start address of this
66    * ByteBuffer; otherwise should be zero.
67    */
68   private long currentAddress;
69 
IterableByteBufferInputStream(Iterable<ByteBuffer> data)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 
getNextByteBuffer()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 
updateCurrentByteBufferPos(int numberOfBytesRead)105   private void updateCurrentByteBufferPos(int numberOfBytesRead) {
106     currentByteBufferPos += numberOfBytesRead;
107     if (currentByteBufferPos == currentByteBuffer.limit()) {
108       getNextByteBuffer();
109     }
110   }
111 
112   @Override
read()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
read(byte[] output, int offset, int length)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 }
151