/
RebufferingInputStream.java
290 lines (251 loc) · 7.96 KB
/
RebufferingInputStream.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.cassandra.io.util;
import java.io.Closeable;
import java.io.DataInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import net.nicoulaj.compilecommand.annotations.DontInline;
import org.apache.cassandra.utils.FastByteOperations;
import org.apache.cassandra.utils.vint.VIntCoding;
import com.google.common.base.Preconditions;
/**
* Rough equivalent of BufferedInputStream and DataInputStream wrapping a ByteBuffer that can be refilled
* via rebuffer. Implementations provide this buffer from various channels (socket, file, memory, etc).
*
* RebufferingInputStream is not thread safe.
*/
public abstract class RebufferingInputStream extends InputStream implements DataInputPlus, Closeable
{
protected ByteBuffer buffer;
protected RebufferingInputStream(ByteBuffer buffer)
{
Preconditions.checkArgument(buffer == null || buffer.order() == ByteOrder.BIG_ENDIAN, "Buffer must have BIG ENDIAN byte ordering");
this.buffer = buffer;
}
/**
* Implementations must implement this method to refill the buffer.
* They can expect the buffer to be empty when this method is invoked.
* @throws IOException
*/
protected abstract void reBuffer() throws IOException;
@Override
public void readFully(byte[] b) throws IOException
{
readFully(b, 0, b.length);
}
@Override
public void readFully(byte[] b, int off, int len) throws IOException
{
int read = read(b, off, len);
if (read < len)
throw new EOFException("EOF after " + read + " bytes out of " + len);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
// avoid int overflow
if (off < 0 || off > b.length || len < 0 || len > b.length - off)
throw new IndexOutOfBoundsException();
if (len == 0)
return 0;
int copied = 0;
while (copied < len)
{
int position = buffer.position();
int remaining = buffer.limit() - position;
if (remaining == 0)
{
reBuffer();
position = buffer.position();
remaining = buffer.limit() - position;
if (remaining == 0)
return copied == 0 ? -1 : copied;
}
int toCopy = Math.min(len - copied, remaining);
FastByteOperations.copy(buffer, position, b, off + copied, toCopy);
buffer.position(position + toCopy);
copied += toCopy;
}
return copied;
}
@DontInline
protected long readPrimitiveSlowly(int bytes) throws IOException
{
long result = 0;
for (int i = 0; i < bytes; i++)
result = (result << 8) | (readByte() & 0xFFL);
return result;
}
@Override
public int skipBytes(int n) throws IOException
{
if (n < 0)
return 0;
int requested = n;
int position = buffer.position(), limit = buffer.limit(), remaining;
while ((remaining = limit - position) < n)
{
n -= remaining;
buffer.position(limit);
reBuffer();
position = buffer.position();
limit = buffer.limit();
if (position == limit)
return requested - n;
}
buffer.position(position + n);
return requested;
}
@Override
public boolean readBoolean() throws IOException
{
return readByte() != 0;
}
@Override
public byte readByte() throws IOException
{
if (!buffer.hasRemaining())
{
reBuffer();
if (!buffer.hasRemaining())
throw new EOFException();
}
return buffer.get();
}
@Override
public int readUnsignedByte() throws IOException
{
return readByte() & 0xff;
}
@Override
public short readShort() throws IOException
{
if (buffer.remaining() >= 2)
return buffer.getShort();
else
return (short) readPrimitiveSlowly(2);
}
@Override
public int readUnsignedShort() throws IOException
{
return readShort() & 0xFFFF;
}
@Override
public char readChar() throws IOException
{
if (buffer.remaining() >= 2)
return buffer.getChar();
else
return (char) readPrimitiveSlowly(2);
}
@Override
public int readInt() throws IOException
{
if (buffer.remaining() >= 4)
return buffer.getInt();
else
return (int) readPrimitiveSlowly(4);
}
@Override
public long readLong() throws IOException
{
if (buffer.remaining() >= 8)
return buffer.getLong();
else
return readPrimitiveSlowly(8);
}
public long readVInt() throws IOException
{
return VIntCoding.decodeZigZag64(readUnsignedVInt());
}
public long readUnsignedVInt() throws IOException
{
//If 9 bytes aren't available use the slow path in VIntCoding
if (buffer.remaining() < 9)
return VIntCoding.readUnsignedVInt(this);
byte firstByte = buffer.get();
//Bail out early if this is one byte, necessary or it fails later
if (firstByte >= 0)
return firstByte;
int extraBytes = VIntCoding.numberOfExtraBytesToRead(firstByte);
int position = buffer.position();
int extraBits = extraBytes * 8;
long retval = buffer.getLong(position);
if (buffer.order() == ByteOrder.LITTLE_ENDIAN)
retval = Long.reverseBytes(retval);
buffer.position(position + extraBytes);
// truncate the bytes we read in excess of those we needed
retval >>>= 64 - extraBits;
// remove the non-value bits from the first byte
firstByte &= VIntCoding.firstByteValueMask(extraBytes);
// shift the first byte up to its correct position
retval |= (long) firstByte << extraBits;
return retval;
}
@Override
public float readFloat() throws IOException
{
if (buffer.remaining() >= 4)
return buffer.getFloat();
else
return Float.intBitsToFloat((int)readPrimitiveSlowly(4));
}
@Override
public double readDouble() throws IOException
{
if (buffer.remaining() >= 8)
return buffer.getDouble();
else
return Double.longBitsToDouble(readPrimitiveSlowly(8));
}
@Override
public String readLine() throws IOException
{
throw new UnsupportedOperationException();
}
@Override
public String readUTF() throws IOException
{
return DataInputStream.readUTF(this);
}
@Override
public int read() throws IOException
{
try
{
return readUnsignedByte();
}
catch (EOFException ex)
{
return -1;
}
}
@Override
public void reset() throws IOException
{
throw new IOException("mark/reset not supported");
}
@Override
public boolean markSupported()
{
return false;
}
}