@Override public int read(long position, byte[] buffer, int offset, int length) throws IOException { if (mClosed) { throw new IOException(ExceptionMessage.READ_CLOSED_STREAM.getMessage()); } int bytesRead = mInputStream.positionedRead(position, buffer, offset, length); if (bytesRead != -1 && mStatistics != null) { mStatistics.incrementBytesRead(bytesRead); } return bytesRead; }
@Test public void positionedReadRetry() throws Exception { TestBlockInStream workingStream = mInStreams.get(0); TestBlockInStream brokenStream = mock(TestBlockInStream.class); when(mBlockStore .getInStream(eq(0L), any(InStreamOptions.class), any())) .thenReturn(brokenStream).thenReturn(workingStream); when(brokenStream.positionedRead(anyLong(), any(byte[].class), anyInt(), anyInt())) .thenThrow(new UnavailableException("test exception")); byte[] b = new byte[(int) BLOCK_LENGTH * 2]; mTestStream.positionedRead(BLOCK_LENGTH / 2, b, 0, b.length); doReturn(0) .when(brokenStream).positionedRead(anyLong(), any(byte[].class), anyInt(), anyInt()); verify(brokenStream, times(1)) .positionedRead(anyLong(), any(byte[].class), anyInt(), anyInt()); assertArrayEquals(BufferUtils.getIncreasingByteArray((int) BLOCK_LENGTH / 2, (int) BLOCK_LENGTH * 2), b); }
@Test public void positionedRead() throws IOException { byte[] b = new byte[(int) BLOCK_LENGTH]; mTestStream.positionedRead(BLOCK_LENGTH, b, 0, b.length); assertArrayEquals(BufferUtils.getIncreasingByteArray((int) BLOCK_LENGTH, (int) BLOCK_LENGTH), b); }
@Test public void multiBlockPositionedRead() throws IOException { byte[] b = new byte[(int) BLOCK_LENGTH * 2]; mTestStream.positionedRead(BLOCK_LENGTH / 2, b, 0, b.length); assertArrayEquals(BufferUtils.getIncreasingByteArray((int) BLOCK_LENGTH / 2, (int) BLOCK_LENGTH * 2), b); }
@Override public int read(long position, byte[] buffer, int offset, int length) throws IOException { if (mClosed) { throw new IOException(ExceptionMessage.READ_CLOSED_STREAM.getMessage()); } int bytesRead = mInputStream.positionedRead(position, buffer, offset, length); if (bytesRead != -1 && mStatistics != null) { mStatistics.incrementBytesRead(bytesRead); } return bytesRead; }