private Iterator<Page> readPages() { checkState(writable, "Repeated reads are disallowed to prevent potential resource leaks"); writable = false; try { InputStream input = closer.register(targetFile.newInputStream()); Iterator<Page> pages = PagesSerdeUtil.readPages(serde, new InputStreamSliceInput(input, BUFFER_SIZE)); return closeWhenExhausted(pages, input); } catch (IOException e) { throw new PrestoException(GENERIC_INTERNAL_ERROR, "Failed to read spilled pages", e); } }
@Test public void testRetainedSize() { int bufferSize = 1024; InputStreamSliceInput input = new InputStreamSliceInput(new ByteArrayInputStream(new byte[] {0, 1}), bufferSize); assertEquals(input.getRetainedSize(), ClassLayout.parseClass(InputStreamSliceInput.class).instanceSize() + sizeOfByteArray(bufferSize)); }
@Override public void readBytes(OutputStream out, int length) throws IOException { while (length > 0) { int batch = Math.min(availableBytes(), length); out.write(buffer, bufferPosition, batch); bufferPosition += batch; length -= batch; ensureAvailable(Math.min(length, MINIMUM_CHUNK_SIZE)); } }
@Override public int read(byte[] destination, int destinationIndex, int length) { if (available() == 0) { return -1; } verify(availableBytes() > 0); int batch = Math.min(availableBytes(), length); slice.getBytes(bufferPosition, destination, destinationIndex, batch); bufferPosition += batch; return batch; }
@Override public long readLong() { ensureAvailable(SIZE_OF_LONG); long v = slice.getLongUnchecked(bufferPosition); bufferPosition += SIZE_OF_LONG; return v; }
@Override public int available() { if (bufferPosition < bufferFill) { return availableBytes(); } return fillBuffer(); }
@Override public boolean isReadable() { return available() > 0; }
@Override public long position() { return checkedCast(bufferOffset + bufferPosition); }
private void ensureAvailable(int size) { if (bufferPosition + size < bufferFill) { return; } if (fillBuffer() < size) { throw new IndexOutOfBoundsException("End of stream"); } }
@Override public long skip(long length) { int availableBytes = availableBytes(); // is skip within the current buffer? if (availableBytes >= length) { bufferPosition += length; return length; } // drop current buffer bufferPosition = bufferFill; try { // skip the rest in inputStream long inputStreamSkip = inputStream.skip(length - availableBytes); bufferOffset += inputStreamSkip; return availableBytes + inputStreamSkip; } catch (IOException e) { throw new UncheckedIOException(e); } }
@Test public void testRetainedSize() { int bufferSize = 1024; InputStreamSliceInput input = new InputStreamSliceInput(new ByteArrayInputStream(new byte[] {0, 1}), bufferSize); assertEquals(input.getRetainedSize(), ClassLayout.parseClass(InputStreamSliceInput.class).instanceSize() + sizeOfByteArray(bufferSize)); }
@Override public void readBytes(OutputStream out, int length) throws IOException { while (length > 0) { int batch = Math.min(availableBytes(), length); out.write(buffer, bufferPosition, batch); bufferPosition += batch; length -= batch; ensureAvailable(Math.min(length, MINIMUM_CHUNK_SIZE)); } }
@Override public int read(byte[] destination, int destinationIndex, int length) { if (available() == 0) { return -1; } verify(availableBytes() > 0); int batch = Math.min(availableBytes(), length); slice.getBytes(bufferPosition, destination, destinationIndex, batch); bufferPosition += batch; return batch; }
@Override public long readLong() { ensureAvailable(SIZE_OF_LONG); long v = slice.getLongUnchecked(bufferPosition); bufferPosition += SIZE_OF_LONG; return v; }
@Override public int available() { if (bufferPosition < bufferFill) { return availableBytes(); } return fillBuffer(); }
@Override public boolean isReadable() { return available() > 0; }
@Override public long position() { return checkedCast(bufferOffset + bufferPosition); }
private void ensureAvailable(int size) { if (bufferPosition + size < bufferFill) { return; } if (fillBuffer() < size) { throw new IndexOutOfBoundsException("End of stream"); } }
@Override public long skip(long length) { int availableBytes = availableBytes(); // is skip within the current buffer? if (availableBytes >= length) { bufferPosition += length; return length; } // drop current buffer bufferPosition = bufferFill; try { // skip the rest in inputStream long inputStreamSkip = inputStream.skip(length - availableBytes); bufferOffset += inputStreamSkip; return availableBytes + inputStreamSkip; } catch (IOException e) { throw new UncheckedIOException(e); } }
boolean complete = getComplete(response); try (SliceInput input = new InputStreamSliceInput(response.getInputStream())) { List<SerializedPage> pages = ImmutableList.copyOf(readSerializedPages(input)); return createPagesResponse(taskInstanceId, token, nextToken, pages, complete);