| 1 |
/* |
| 2 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
* you may not use this file except in compliance with the License. |
| 4 |
* You may obtain a copy of the License at |
| 5 |
* |
| 6 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
* |
| 8 |
* Unless required by applicable law or agreed to in writing, software |
| 9 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
* See the License for the specific language governing permissions and |
| 12 |
* limitations under the License. |
| 13 |
*/ |
| 14 |
package io.airlift.slice; |
| 15 |
|
| 16 |
import java.io.Closeable; |
| 17 |
import java.io.IOException; |
| 18 |
import java.io.OutputStream; |
| 19 |
|
| 20 |
import static io.airlift.slice.Preconditions.checkArgument; |
| 21 |
import static io.airlift.slice.SizeOf.SIZE_OF_BYTE; |
| 22 |
import static io.airlift.slice.SizeOf.SIZE_OF_DOUBLE; |
| 23 |
import static io.airlift.slice.SizeOf.SIZE_OF_FLOAT; |
| 24 |
import static io.airlift.slice.SizeOf.SIZE_OF_INT; |
| 25 |
import static io.airlift.slice.SizeOf.SIZE_OF_LONG; |
| 26 |
import static io.airlift.slice.SizeOf.SIZE_OF_SHORT; |
| 27 |
import static java.util.Objects.requireNonNull; |
| 28 |
|
| 29 |
@SuppressWarnings("JavaDoc") // IDEA-81310 |
| 30 |
public final class ChunkedSliceInput |
| 31 |
extends FixedLengthSliceInput |
| 32 |
{ |
| 33 |
private final InternalLoader<?> loader; |
| 34 |
private final Slice buffer; |
| 35 |
|
| 36 |
private final long globalLength; |
| 37 |
private long globalPosition; |
| 38 |
private int bufferPosition; |
| 39 |
private int bufferLength; |
| 40 |
|
| 41 |
public ChunkedSliceInput(SliceLoader<?> loader, int bufferSize) |
| 42 |
{ |
| 43 |
this.loader = new InternalLoader<>(requireNonNull(loader, "loader is null"), bufferSize); |
| 44 |
this.buffer = this.loader.getBufferSlice(); |
| 45 |
this.globalLength = loader.getSize(); |
| 46 |
} |
| 47 |
|
| 48 |
@Override |
| 49 |
public long length() |
| 50 |
{ |
| 51 |
return globalLength; |
| 52 |
} |
| 53 |
|
| 54 |
@Override |
| 55 |
public long position() |
| 56 |
{ |
| 57 |
return globalPosition + bufferPosition; |
| 58 |
} |
| 59 |
|
| 60 |
@Override |
| 61 |
public void setPosition(long position) |
| 62 |
{ |
| 63 |
if (position < 0 || position > globalLength) { |
| 64 |
throw new IndexOutOfBoundsException("Invalid position " + position + " for slice with length " + globalLength); |
| 65 |
} |
| 66 |
this.globalPosition = position; |
| 67 |
this.bufferLength = 0; |
| 68 |
this.bufferPosition = 0; |
| 69 |
} |
| 70 |
|
| 71 |
@Override |
| 72 |
public boolean isReadable() |
| 73 |
{ |
| 74 |
return bufferPosition < bufferLength; |
| 75 |
} |
| 76 |
|
| 77 |
@Override |
| 78 |
public int available() |
| 79 |
{ |
| 80 |
return bufferLength - bufferPosition; |
| 81 |
} |
| 82 |
|
| 83 |
public void ensureAvailable(int size) |
| 84 |
{ |
| 85 |
if (available() >= size) { |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
checkArgument(size <= buffer.length(), "Size is larger than buffer"); |
| 90 |
checkBound(position() + size, globalLength, "End of stream"); |
| 91 |
|
| 92 |
// advance position |
| 93 |
globalPosition += bufferPosition; |
| 94 |
bufferPosition = 0; |
| 95 |
|
| 96 |
// this will reread unused data in the buffer |
| 97 |
long readSize = Math.min(buffer.length(), globalLength - globalPosition); |
| 98 |
if (readSize > Integer.MAX_VALUE) { |
| 99 |
readSize = Integer.MAX_VALUE; |
| 100 |
} |
| 101 |
bufferLength = (int) readSize; |
| 102 |
loader.load(globalPosition, bufferLength); |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public boolean readBoolean() |
| 107 |
{ |
| 108 |
return readByte() != 0; |
| 109 |
} |
| 110 |
|
| 111 |
@Override |
| 112 |
public int read() |
| 113 |
{ |
| 114 |
if (position() >= globalLength) { |
| 115 |
return -1; |
| 116 |
} |
| 117 |
ensureAvailable(SIZE_OF_BYTE); |
| 118 |
int result = buffer.getByte(bufferPosition) & 0xFF; |
| 119 |
bufferPosition++; |
| 120 |
return result; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public byte readByte() |
| 125 |
{ |
| 126 |
int value = read(); |
| 127 |
if (value == -1) { |
| 128 |
throw new IndexOutOfBoundsException(); |
| 129 |
} |
| 130 |
return (byte) value; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public int readUnsignedByte() |
| 135 |
{ |
| 136 |
return readByte() & 0xFF; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public short readShort() |
| 141 |
{ |
| 142 |
ensureAvailable(SIZE_OF_SHORT); |
| 143 |
short v = buffer.getShort(bufferPosition); |
| 144 |
bufferPosition += SIZE_OF_SHORT; |
| 145 |
return v; |
| 146 |
} |
| 147 |
|
| 148 |
@Override |
| 149 |
public int readUnsignedShort() |
| 150 |
{ |
| 151 |
return readShort() & 0xFFFF; |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public int readInt() |
| 156 |
{ |
| 157 |
ensureAvailable(SIZE_OF_INT); |
| 158 |
int v = buffer.getInt(bufferPosition); |
| 159 |
bufferPosition += SIZE_OF_INT; |
| 160 |
return v; |
| 161 |
} |
| 162 |
|
| 163 |
@Override |
| 164 |
public long readLong() |
| 165 |
{ |
| 166 |
ensureAvailable(SIZE_OF_LONG); |
| 167 |
long v = buffer.getLong(bufferPosition); |
| 168 |
bufferPosition += SIZE_OF_LONG; |
| 169 |
return v; |
| 170 |
} |
| 171 |
|
| 172 |
@Override |
| 173 |
public float readFloat() |
| 174 |
{ |
| 175 |
ensureAvailable(SIZE_OF_FLOAT); |
| 176 |
float v = buffer.getFloat(bufferPosition); |
| 177 |
bufferPosition += SIZE_OF_FLOAT; |
| 178 |
return v; |
| 179 |
} |
| 180 |
|
| 181 |
@Override |
| 182 |
public double readDouble() |
| 183 |
{ |
| 184 |
ensureAvailable(SIZE_OF_DOUBLE); |
| 185 |
double v = buffer.getDouble(bufferPosition); |
| 186 |
bufferPosition += SIZE_OF_DOUBLE; |
| 187 |
return v; |
| 188 |
} |
| 189 |
|
| 190 |
@Override |
| 191 |
public Slice readSlice(int length) |
| 192 |
{ |
| 193 |
if (length == 0) { |
| 194 |
return Slices.EMPTY_SLICE; |
| 195 |
} |
| 196 |
Slice slice = Slices.allocate(length); |
| 197 |
readBytes(slice); |
| 198 |
return slice; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void readBytes(Slice destination, int destinationIndex, int length) |
| 203 |
{ |
| 204 |
checkBound(position() + length, globalLength, "End of stream"); |
| 205 |
|
| 206 |
while (length > 0) { |
| 207 |
int bytesToRead = Math.min(available(), length); |
| 208 |
buffer.getBytes(bufferPosition, destination, destinationIndex, bytesToRead); |
| 209 |
|
| 210 |
bufferPosition += bytesToRead; |
| 211 |
length -= bytesToRead; |
| 212 |
destinationIndex += bytesToRead; |
| 213 |
|
| 214 |
ensureAvailable(Math.min(length, buffer.length())); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public int read(byte[] destination, int destinationIndex, int length) |
| 220 |
{ |
| 221 |
if (length == 0) { |
| 222 |
return 0; |
| 223 |
} |
| 224 |
|
| 225 |
if (globalLength - position() == 0) { |
| 226 |
return -1; |
| 227 |
} |
| 228 |
|
| 229 |
// limit read to stream size |
| 230 |
length = (int) Math.min(length, globalLength - position()); |
| 231 |
|
| 232 |
// do a full read of the available data |
| 233 |
readBytes(destination, destinationIndex, length); |
| 234 |
return length; |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void readBytes(byte[] destination, int destinationIndex, int length) |
| 239 |
{ |
| 240 |
checkBound(position() + length, globalLength, "End of stream"); |
| 241 |
|
| 242 |
while (length > 0) { |
| 243 |
int bytesToRead = Math.min(available(), length); |
| 244 |
buffer.getBytes(bufferPosition, destination, destinationIndex, bytesToRead); |
| 245 |
|
| 246 |
bufferPosition += bytesToRead; |
| 247 |
length -= bytesToRead; |
| 248 |
destinationIndex += bytesToRead; |
| 249 |
|
| 250 |
ensureAvailable(Math.min(length, buffer.length())); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
public void readBytes(OutputStream out, int length) |
| 256 |
throws IOException |
| 257 |
{ |
| 258 |
checkBound(position() + length, globalLength, "End of stream"); |
| 259 |
|
| 260 |
while (length > 0) { |
| 261 |
int bytesToRead = Math.min(available(), length); |
| 262 |
buffer.getBytes(bufferPosition, out, bytesToRead); |
| 263 |
|
| 264 |
bufferPosition += bytesToRead; |
| 265 |
length -= bytesToRead; |
| 266 |
|
| 267 |
ensureAvailable(Math.min(length, buffer.length())); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
public long skip(long length) |
| 273 |
{ |
| 274 |
// is skip within the current buffer? |
| 275 |
if (available() >= length) { |
| 276 |
bufferPosition += length; |
| 277 |
return length; |
| 278 |
} |
| 279 |
|
| 280 |
// drop current buffer |
| 281 |
globalPosition += bufferPosition; |
| 282 |
bufferPosition = 0; |
| 283 |
bufferLength = 0; |
| 284 |
|
| 285 |
// trim length to stream size |
| 286 |
length = Math.min(length, remaining()); |
| 287 |
|
| 288 |
// skip |
| 289 |
globalPosition += length; |
| 290 |
|
| 291 |
return length; |
| 292 |
} |
| 293 |
|
| 294 |
@Override |
| 295 |
public int skipBytes(int length) |
| 296 |
{ |
| 297 |
return (int) skip(length); |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public void close() |
| 302 |
{ |
| 303 |
globalPosition = globalLength; |
| 304 |
bufferPosition = 0; |
| 305 |
bufferLength = 0; |
| 306 |
loader.close(); |
| 307 |
} |
| 308 |
|
| 309 |
@Override |
| 310 |
public String toString() |
| 311 |
{ |
| 312 |
StringBuilder builder = new StringBuilder("SliceStreamInput{"); |
| 313 |
builder.append("globalLength=").append(globalLength); |
| 314 |
builder.append(", globalPosition=").append(globalPosition); |
| 315 |
builder.append(", bufferLength=").append(bufferLength); |
| 316 |
builder.append(", bufferPosition=").append(bufferPosition); |
| 317 |
builder.append('}'); |
| 318 |
return builder.toString(); |
| 319 |
} |
| 320 |
|
| 321 |
private static void checkBound(long index, long size, String message) |
| 322 |
{ |
| 323 |
if (index > size) { |
| 324 |
throw new IndexOutOfBoundsException(message); |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
public interface SliceLoader<B extends BufferReference> |
| 329 |
extends Closeable |
| 330 |
{ |
| 331 |
B createBuffer(int bufferSize); |
| 332 |
|
| 333 |
long getSize(); |
| 334 |
|
| 335 |
void load(long position, B bufferReference, int length); |
| 336 |
|
| 337 |
@Override |
| 338 |
void close(); |
| 339 |
} |
| 340 |
|
| 341 |
public interface BufferReference |
| 342 |
{ |
| 343 |
Slice getSlice(); |
| 344 |
} |
| 345 |
|
| 346 |
private static class InternalLoader<T extends BufferReference> |
| 347 |
{ |
| 348 |
private final SliceLoader<T> loader; |
| 349 |
private final T bufferReference; |
| 350 |
|
| 351 |
public InternalLoader(SliceLoader<T> loader, int bufferSize) |
| 352 |
{ |
| 353 |
this.loader = loader; |
| 354 |
checkArgument(bufferSize >= 128, "Buffer size must be at least 128"); |
| 355 |
this.bufferReference = loader.createBuffer(bufferSize); |
| 356 |
} |
| 357 |
|
| 358 |
public Slice getBufferSlice() |
| 359 |
{ |
| 360 |
return bufferReference.getSlice(); |
| 361 |
} |
| 362 |
|
| 363 |
public void load(long position, int length) |
| 364 |
{ |
| 365 |
loader.load(position, bufferReference, length); |
| 366 |
} |
| 367 |
|
| 368 |
public void close() |
| 369 |
{ |
| 370 |
loader.close(); |
| 371 |
} |
| 372 |
} |
| 373 |
} |