LoopbackBlobStore implements BlobStore { @Override public int readBlob(String blobId, long pos, byte[] buff, int off, int length) { final int binaryLength = blobId.length(); checkBinaryOffsetInRange(pos, binaryLength); final int effectiveSrcPos = Math.toIntExact(pos); final int effectiveBlobLengthToBeRead = Math.min( binaryLength - effectiveSrcPos, length); checkForBufferOverflow(buff, off, effectiveBlobLengthToBeRead); final byte[] blobIdBytes = getBlobIdStringAsByteArray(blobId); System.arraycopy(blobIdBytes, effectiveSrcPos, buff, off, effectiveBlobLengthToBeRead); return effectiveBlobLengthToBeRead; }  @Override String writeBlob(InputStream in); @Override String writeBlob(InputStream in, BlobOptions options); @Override int readBlob(String blobId, long pos, byte[] buff, int off,
            int length); @Override long getBlobLength(String blobId); @Override InputStream getInputStream(String blobId); @Override String getBlobId(@NotNull String reference); @Override String getReference(@NotNull String blobId); @Override void close();  }
@Test @Parameters(method = "blobIdsReads") public void shouldAlwaysFillBufferWithRequestedBlobIdUtf8BinRepresentation( final String blobId, int offsetToRead, int bufSize, int bufOffset, int lengthToRead, final String expectedBufferContent, final int expectedNumberOfBytesRead) throws IOException { given: { final String encoding = "UTF-8"; final BlobStore blobStore = new LoopbackBlobStore(); final byte[] buffer = new byte[bufSize]; when: { final int numberOfBytesRead = blobStore.readBlob( blobId, offsetToRead, buffer, bufOffset, lengthToRead); and: { final String actualInputStreamAsString = IOUtils.toString( buffer, encoding); then: { assertEquals(numberOfBytesRead, expectedNumberOfBytesRead); assertEquals(expectedBufferContent, encodeBufferFreeSpace(actualInputStreamAsString)); } } } } }
@Test(expected = UnsupportedOperationException.class) @Parameters(method = "blobIdsFailedBufferReadsCases") public void getInputStreamShouldAlwaysReturnExceptionIfBufferTooSmall( final String blobId, int offsetToRead, int bufSize, int bufOffset, int lengthToRead) throws IOException { given: { final BlobStore store = new LoopbackBlobStore(); final byte[] buffer = new byte[bufSize]; when: { store.readBlob( blobId, offsetToRead, buffer, bufOffset, lengthToRead); } } }
@Test(expected = IllegalArgumentException.class) @Parameters(method = "blobIdsFailedOffsetReadsCases") public void getInputStreamShouldAlwaysReturnExceptionIfBinaryOffsetIsBad( final String blobId, int offsetToRead, int bufSize, int bufOffset, int lengthToRead) throws IOException { given: { final BlobStore store = new LoopbackBlobStore(); final byte[] buffer = new byte[bufSize]; when: { store.readBlob( blobId, offsetToRead, buffer, bufOffset, lengthToRead); } } }