/** * Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If * no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks * until input data is available, the end of the stream is detected, or an exception is thrown. * * @return An <code>int</code> which represents the total number of bytes read into the buffer, or -1 if * there is no more data because the end of the stream has been reached. * * @throws IOException * If an I/O error occurs. */ @Override @DoesServiceRequest public int read() throws IOException { final byte[] tBuff = new byte[1]; final int numberOfBytesRead = this.read(tBuff, 0, 1); if (numberOfBytesRead > 0) { return tBuff[0] & 0xFF; } else if (numberOfBytesRead == 0) { throw new IOException(SR.UNEXPECTED_STREAM_READ_ERROR); } else { return -1; } }
return this.readInternal(b, off, len);
/** * Repositions this stream to the position at the time the mark method was last called on this input stream. Note * repositioning the blob read stream will disable blob MD5 checking. * * @throws IOException * If this stream has not been marked or if the mark has been invalidated. */ @Override public synchronized void reset() throws IOException { if (this.markedPosition + this.markExpiry < this.currentAbsoluteReadPosition) { throw new IOException(SR.MARK_EXPIRED); } this.validateBlobMd5 = false; this.md5Digest = null; this.reposition(this.markedPosition); }
this.checkStreamState(); this.dispatchRead((int) Math.min(this.readSize, this.streamLength - this.currentAbsoluteReadPosition));
false /* setStartTime */); return new BlobInputStream(this, accessCondition, options, opContext);
this.checkStreamState(); this.dispatchRead((int) Math.min(this.readSize, this.streamLength - this.currentAbsoluteReadPosition));
false /* setStartTime */); return new BlobInputStream(this, accessCondition, options, opContext);
/** * Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If * no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks * until input data is available, the end of the stream is detected, or an exception is thrown. * * @return An <code>int</code> which represents the total number of bytes read into the buffer, or -1 if * there is no more data because the end of the stream has been reached. * * @throws IOException * If an I/O error occurs. */ @Override @DoesServiceRequest public int read() throws IOException { final byte[] tBuff = new byte[1]; final int numberOfBytesRead = this.read(tBuff, 0, 1); if (numberOfBytesRead > 0) { return tBuff[0] & 0xFF; } else if (numberOfBytesRead == 0) { throw new IOException(SR.UNEXPECTED_STREAM_READ_ERROR); } else { return -1; } }
/** * Repositions this stream to the position at the time the mark method was last called on this input stream. Note * repositioning the blob read stream will disable blob MD5 checking. * * @throws IOException * If this stream has not been marked or if the mark has been invalidated. */ @Override public synchronized void reset() throws IOException { if (this.markedPosition + this.markExpiry < this.currentAbsoluteReadPosition) { throw new IOException(SR.MARK_EXPIRED); } this.validateBlobMd5 = false; this.md5Digest = null; this.reposition(this.markedPosition); }
return this.readInternal(b, off, len);
@DoesServiceRequest public int read(final byte[] b) throws IOException { return this.read(b, 0, b.length);
/** * Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, * end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of * conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of * bytes skipped is returned. If n is negative, no bytes are skipped. * * Note repositioning the blob read stream will disable blob MD5 checking. * * @param n * A <code>long</code> which represents the number of bytes to skip. */ @Override public synchronized long skip(final long n) throws IOException { if (n == 0) { return 0; } if (n < 0 || this.currentAbsoluteReadPosition + n > this.streamLength) { throw new IndexOutOfBoundsException(); } this.validateBlobMd5 = false; this.md5Digest = null; this.reposition(this.currentAbsoluteReadPosition + n); return n; } }
@DoesServiceRequest public int read(final byte[] b) throws IOException { return this.read(b, 0, b.length);
/** * Skips over and discards n bytes of data from this input stream. The skip method may, for a variety of reasons, * end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of * conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of * bytes skipped is returned. If n is negative, no bytes are skipped. * * Note repositioning the blob read stream will disable blob MD5 checking. * * @param n * A <code>long</code> which represents the number of bytes to skip. */ @Override public synchronized long skip(final long n) throws IOException { if (n == 0) { return 0; } if (n < 0 || this.currentAbsoluteReadPosition + n > this.streamLength) { throw new IndexOutOfBoundsException(); } this.validateBlobMd5 = false; this.md5Digest = null; this.reposition(this.currentAbsoluteReadPosition + n); return n; } }
@Test public void testAppendBlobInputStream() throws URISyntaxException, StorageException, IOException { final int blobLength = 16 * 1024; final Random randGenerator = new Random(); String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); final byte[] buff = new byte[blobLength]; randGenerator.nextBytes(buff); buff[0] = -1; buff[1] = -128; final ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff); final BlobRequestOptions options = new BlobRequestOptions(); final OperationContext operationContext = new OperationContext(); options.setTimeoutIntervalInMs(90000); options.setRetryPolicyFactory(new RetryNoRetry()); blobRef.upload(sourceStream, blobLength, null, options, operationContext); BlobInputStream blobStream = blobRef.openInputStream(); for (int i = 0; i < blobLength; i++) { int data = blobStream.read(); assertTrue(data >= 0); assertEquals(buff[i], (byte) data); } assertEquals(-1, blobStream.read()); blobRef.delete(); }
/** * @throws URISyntaxException * @throws StorageException * @throws IOException */ @Test public void testPageBlobInputStream() throws URISyntaxException, StorageException, IOException { final int blobLength = 16 * 1024; final Random randGenerator = new Random(); String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName); final byte[] buff = new byte[blobLength]; randGenerator.nextBytes(buff); buff[0] = -1; buff[1] = -128; final ByteArrayInputStream sourceStream = new ByteArrayInputStream(buff); final BlobRequestOptions options = new BlobRequestOptions(); final OperationContext operationContext = new OperationContext(); options.setTimeoutIntervalInMs(90000); options.setRetryPolicyFactory(new RetryNoRetry()); blobRef.upload(sourceStream, blobLength, null, options, operationContext); BlobInputStream blobStream = blobRef.openInputStream(); for (int i = 0; i < blobLength; i++) { int data = blobStream.read(); assertTrue(data >= 0); assertEquals(buff[i], (byte) data); } assertEquals(-1, blobStream.read()); blobRef.delete(); }
int numBytesRead = input.read(buffer); assertEquals(BlobOperationDescriptor.OperationType.GetBlob, lastOperationTypeSent);