/** * Downloads a range of bytes from the file to the given byte buffer. * * @param offset * A <code>long</code> which represents the byte offset to use as the starting point for the source. * @param length * A <code>Long</code> which represents the number of bytes to read or null. * @param buffer * A <code>byte</code> array which represents the buffer to which the file bytes are downloaded. * @param bufferOffset * An <code>int</code> which represents the byte offset to use as the starting point for the target. * * @throws StorageException */ @DoesServiceRequest public final int downloadRangeToByteArray(final long offset, final Long length, final byte[] buffer, final int bufferOffset) throws StorageException { return this.downloadRangeToByteArray(offset, length, buffer, bufferOffset, null /* accessCondition */, null /* options */, null /* opContext */); }
/** * Downloads a range of bytes from the file to the given byte buffer. * * @param offset * A <code>long</code> which represents the byte offset to use as the starting point for the source. * @param length * A <code>Long</code> which represents the number of bytes to read or null. * @param buffer * A <code>byte</code> array which represents the buffer to which the file bytes are downloaded. * @param bufferOffset * An <code>int</code> which represents the byte offset to use as the starting point for the target. * * @throws StorageException */ @DoesServiceRequest public final int downloadRangeToByteArray(final long offset, final Long length, final byte[] buffer, final int bufferOffset) throws StorageException { return this.downloadRangeToByteArray(offset, length, buffer, bufferOffset, null /* accessCondition */, null /* options */, null /* opContext */); }
file.downloadRangeToByteArray(1024, (long) 1, resultBuffer, 0); Assert.fail(); file.downloadRangeToByteArray(0, (long) 1023, resultBuffer, 2); Assert.fail(); file.downloadRangeToByteArray(0, (long) -10, resultBuffer, 0); Assert.fail(); file.downloadRangeToByteArray(-10, (long) 20, resultBuffer, 0); Assert.fail(); file.downloadRangeToByteArray(0, (long) 20, resultBuffer, -10); Assert.fail();
protected static void doDownloadRangeToByteArrayTest(CloudFile file, int fileSize, int bufferSize, int bufferOffset, Long fileOffset, Long length) throws IOException, StorageException, URISyntaxException { final Random randGenerator = new Random(); final byte[] buffer = new byte[fileSize]; randGenerator.nextBytes(buffer); byte[] resultBuffer = new byte[bufferSize]; int downloadLength; FileRequestOptions options = new FileRequestOptions(); file.upload(new ByteArrayInputStream(buffer), buffer.length); downloadLength = file.downloadRangeToByteArray(fileOffset, length, resultBuffer, bufferOffset, null, options, null); int downloadSize = Math.min(fileSize - (int) (fileOffset != null ? fileOffset : 0), bufferSize - bufferOffset); if (length != null && length < downloadSize) { downloadSize = length.intValue(); } Assert.assertEquals(downloadSize, downloadLength); for (int i = 0; i < bufferOffset; i++) { Assert.assertEquals(0, resultBuffer[i]); } for (int j = 0; j < downloadLength; j++) { Assert.assertEquals(buffer[(int) ((fileOffset != null ? fileOffset : 0) + j)], resultBuffer[bufferOffset + j]); } for (int k = bufferOffset + downloadLength; k < bufferSize; k++) { Assert.assertEquals(0, resultBuffer[k]); } }
/** * Create a file and try to download a range of its contents * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testFileDownloadRangeValidationTest() throws StorageException, URISyntaxException, IOException { final int length = 5 * 1024 * 1024; final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.upload(FileTestHelper.getRandomDataStream(length), length); //Download full file fileRef.download(new ByteArrayOutputStream()); assertEquals(length, fileRef.getProperties().getLength()); //Download file range. byte[] downloadBuffer = new byte[100]; int downloadLength = fileRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0); assertEquals(length, fileRef.getProperties().getLength()); assertEquals(100, downloadLength); }
fileRef2.downloadRangeToByteArray(0L, 4L, target, 0); assertEquals(calculatedMD5, fileRef2.getProperties().getContentMD5());