private void readBlock() throws IOException { if (buffer == null) { buffer = new byte[min(fileOffset, bufferSize)]; } else if (fileOffset < buffer.length) { buffer = new byte[fileOffset]; } if (buffer.length > 0) { fileOffset -= buffer.length; try { blob.downloadRangeToByteArray(fileOffset, Long.valueOf(buffer.length), buffer, 0); } catch (StorageException e) { throw new IOException(e); } } bufferOffset = buffer.length; }
private void readBlock() throws IOException { if (buffer == null) { buffer = new byte[min(fileOffset, bufferSize)]; } else if (fileOffset < buffer.length) { buffer = new byte[fileOffset]; } if (buffer.length > 0) { fileOffset -= buffer.length; try { blob.downloadRangeToByteArray(fileOffset, Long.valueOf(buffer.length), buffer, 0); } catch (StorageException e) { throw new IOException(e); } } bufferOffset = buffer.length; }
/** * Downloads a range of bytes from the blob 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 blob bytes are downloaded. * @param bufferOffset * An <code>int</code> which represents the byte offset to use as the starting point for the target. * @returns The total number of bytes read into the buffer. * * @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 blob 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 blob bytes are downloaded. * @param bufferOffset * An <code>int</code> which represents the byte offset to use as the starting point for the target. * @return The total number of bytes read into the buffer. * * @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 */); }
blob.downloadRangeToByteArray(1024, (long) 1, resultBuffer, 0); fail(); blob.downloadRangeToByteArray(0, (long) 1023, resultBuffer, 2); fail(); blob.downloadRangeToByteArray(0, (long) -10, resultBuffer, 0); fail(); blob.downloadRangeToByteArray(-10, (long) 20, resultBuffer, 0); fail(); blob.downloadRangeToByteArray(0, (long) 20, resultBuffer, -10); fail();
protected static void doDownloadRangeToByteArrayTest(CloudBlob blob, int blobSize, int bufferSize, int bufferOffset, Long blobOffset, Long length) throws IOException, StorageException { final Random randGenerator = new Random(); final byte[] buffer = new byte[blobSize]; randGenerator.nextBytes(buffer); byte[] resultBuffer = new byte[bufferSize]; int downloadLength; BlobRequestOptions options = new BlobRequestOptions(); blob.upload(new ByteArrayInputStream(buffer), buffer.length); downloadLength = blob.downloadRangeToByteArray(blobOffset, length, resultBuffer, bufferOffset, null, options, null); int downloadSize = Math.min(blobSize - (int) (blobOffset != null ? blobOffset : 0), bufferSize - bufferOffset); if (length != null && length < downloadSize) { downloadSize = length.intValue(); } assertEquals(downloadSize, downloadLength); for (int i = 0; i < bufferOffset; i++) { assertEquals(0, resultBuffer[i]); } for (int j = 0; j < downloadLength; j++) { assertEquals(buffer[(int) ((blobOffset != null ? blobOffset : 0) + j)], resultBuffer[bufferOffset + j]); } for (int k = bufferOffset + downloadLength; k < bufferSize; k++) { assertEquals(0, resultBuffer[k]); } }