/** * Writes <code>b.length</code> bytes from the specified byte array to this output stream. * * @param data * A <code>byte</code> array which represents the data to write. * * @throws IOException * If an I/O error occurs. In particular, an IOException may be thrown if the output stream has been * closed. */ @Override @DoesServiceRequest public void write(final byte[] data) throws IOException { this.write(data, 0, data.length); }
/** * Writes the specified byte to this output stream. The general contract for write is that one byte is written to * the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits * of b are ignored. * * @param byteVal * An <code>int</code> which represents the bye value to write. * * @throws IOException * If an I/O error occurs. In particular, an IOException may be thrown if the output stream has been * closed. */ @Override @DoesServiceRequest public void write(final int byteVal) throws IOException { this.write(new byte[] { (byte) (byteVal & 0xFF) }); }
/** * Writes <code>b.length</code> bytes from the specified byte array to this output stream. * <p> * If you are using {@link CloudAppendBlob} and are certain of a single writer scenario, please look at * {@link BlobRequestOptions#setAbsorbConditionalErrorsOnRetry(Boolean)} and see if setting this flag to * <code>true</code> is acceptable for you. * * @param data * A <code>byte</code> array which represents the data to write. * * @throws IOException * If an I/O error occurs. In particular, an IOException may be thrown if the output stream has been * closed. */ @Override @DoesServiceRequest public void write(final byte[] data) throws IOException { this.write(data, 0, data.length); }
/** * Writes the specified byte to this output stream. The general contract for write is that one byte is written to * the output stream. The byte to be written is the eight low-order bits of the argument b. The 24 high-order bits * of b are ignored. * <p> * If you are using {@link CloudAppendBlob} and are certain of a single writer scenario, please look at * {@link BlobRequestOptions#setAbsorbConditionalErrorsOnRetry(Boolean)} and see if setting this flag to * <code>true</code> is acceptable for you. * * @param byteVal * An <code>int</code> which represents the bye value to write. * * @throws IOException * If an I/O error occurs. In particular, an IOException may be thrown if the output stream has been * closed. */ @Override @DoesServiceRequest public void write(final int byteVal) throws IOException { this.write(new byte[] { (byte) (byteVal & 0xFF) }); }
@Override public Void call() { try { byte[] buffer = BlobTestHelper.getRandomBuffer(this.length*this.writes); for (int i = 0; i < writes; i ++) { this.blobOutputStream.write(buffer, i*this.length, this.length); if ((i+1)%flushPeriod == 0) { this.blobOutputStream.flush(); } } } catch (Exception e) { fail("flushHelper should succeed."); } return null; } }
@Test public void testWriteStream() throws URISyntaxException, StorageException, IOException { int blobLengthToUse = 8 * 512; byte[] buffer = BlobTestHelper.getRandomBuffer(blobLengthToUse); String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudBlockBlob blockBlob = this.container.getBlockBlobReference(blobName); BlobOutputStream blobOutputStream = blockBlob.openOutputStream(); ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); blobOutputStream.write(inputStream, 512); inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512); blobOutputStream.write(inputStream, 3 * 512); blobOutputStream.close(); byte[] result = new byte[blobLengthToUse]; blockBlob.downloadToByteArray(result, 0); int i = 0; for (; i < 4 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 8 * 512; i++) { assertEquals(0, result[i]); } }
@Test public void testWithAccessCondition() throws URISyntaxException, StorageException, IOException { int blobLengthToUse = 8 * 512; byte[] buffer = BlobTestHelper.getRandomBuffer(blobLengthToUse); String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); AccessCondition accessCondition = AccessCondition.generateIfNotModifiedSinceCondition(new Date()); CloudBlockBlob blockBlob = this.container.getBlockBlobReference(blobName); BlobOutputStream blobOutputStream = blockBlob.openOutputStream(accessCondition, null, null); ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); blobOutputStream.write(inputStream, 512); inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512); blobOutputStream.write(inputStream, 3 * 512); blobOutputStream.close(); byte[] result = new byte[blobLengthToUse]; blockBlob.downloadToByteArray(result, 0); int i = 0; for (; i < 4 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 8 * 512; i++) { assertEquals(0, result[i]); } }
@Test public void testOpenOutputStream() throws URISyntaxException, StorageException, IOException { int blobLengthToUse = 8 * 512; byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512); String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName); blobRef.create(blobLengthToUse); BlobOutputStream blobOutputStream = blobRef.openWriteNew(blobLengthToUse); ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); blobOutputStream = blobRef.openWriteNew(blobLengthToUse); inputStream = new ByteArrayInputStream(buffer); blobOutputStream.write(inputStream, 512); inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512); blobOutputStream.write(inputStream, 3 * 512); blobOutputStream.close(); byte[] result = new byte[blobLengthToUse]; blobRef.downloadToByteArray(result, 0); int i = 0; for (; i < 4 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 8 * 512; i++) { assertEquals(0, result[i]); } }
@Test public void testOpenOutputStream() throws URISyntaxException, StorageException, IOException { int blobLengthToUse = 8 * 512; byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512); String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); final CloudAppendBlob blobRef = this.container .getAppendBlobReference(blobName); blobRef.createOrReplace(); BlobOutputStream blobOutputStream = blobRef.openWriteNew(); ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); blobOutputStream = blobRef.openWriteNew(); inputStream = new ByteArrayInputStream(buffer); blobOutputStream.write(inputStream, 512); inputStream = new ByteArrayInputStream(buffer, 512, 3 * 512); blobOutputStream.write(inputStream, 3 * 512); blobOutputStream.close(); byte[] result = new byte[blobLengthToUse]; blobRef.downloadToByteArray(result, 0); int i = 0; for (; i < 4 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 8 * 512; i++) { assertEquals(0, result[i]); } }
private static CloudBlockBlob primeRootContainer(CloudBlobClient blobClient, String accountName, String blobName, int fileSize) throws Exception { // Create a container if it does not exist. The container name // must be lower case. CloudBlobContainer container = blobClient.getContainerReference("https://" + accountName + "/" + "$root"); container.createIfNotExists(); // Create a blob output stream. CloudBlockBlob blob = container.getBlockBlobReference(blobName); BlobOutputStream outputStream = blob.openOutputStream(); outputStream.write(new byte[fileSize]); outputStream.close(); // Return a reference to the block blob object. return blob; }
private static CloudBlockBlob primeRootContainer(CloudBlobClient blobClient, String accountName, String blobName, int fileSize) throws Exception { // Create a container if it does not exist. The container name // must be lower case. CloudBlobContainer container = blobClient.getContainerReference("https://" + accountName + "/" + "$root"); container.createIfNotExists(); // Create a blob output stream. CloudBlockBlob blob = container.getBlockBlobReference(blobName); BlobOutputStream outputStream = blob.openOutputStream(); outputStream.write(new byte[fileSize]); outputStream.close(); // Return a reference to the block blob object. return blob; }
@Test public void testOpenOutputStreamNotAligned() throws StorageException, URISyntaxException { int blobLengthToUse = 8 * 512; byte[] buffer = BlobTestHelper.getRandomBuffer(8 * 512); String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); final CloudPageBlob blobRef = this.container.getPageBlobReference(blobName); blobRef.create(blobLengthToUse); try { blobRef.openWriteNew(blobLengthToUse + 1); fail("Did not throw expected exception on non-512-byte-aligned offset"); } catch (IllegalArgumentException ex) { assertEquals(SR.INVALID_PAGE_BLOB_LENGTH, ex.getMessage()); } BlobOutputStream blobOutputStream = blobRef.openWriteNew(blobLengthToUse); ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); try { blobOutputStream.write(inputStream, 511); blobOutputStream.close(); fail("Did not throw expected exception on non-512-byte-aligned length"); } catch (IOException ex) { assertEquals(String.format(SR.INVALID_NUMBER_OF_BYTES_IN_THE_BUFFER, 511), ex.getMessage()); } }
@Test public void testContainerExistAfterDoesNotExist() throws Exception { testAccount = AzureBlobStorageTestAccount.create("", EnumSet.noneOf(CreateOptions.class)); assumeNotNull(testAccount); CloudBlobContainer container = testAccount.getRealContainer(); FileSystem fs = testAccount.getFileSystem(); // Starting off with the container not there assertFalse(container.exists()); // A list shouldn't create the container and will set file system store // state to DoesNotExist try { fs.listStatus(new Path("/")); assertTrue("Should've thrown.", false); } catch (FileNotFoundException ex) { assertTrue("Unexpected exception: " + ex, ex.getMessage().contains("does not exist.")); } assertFalse(container.exists()); // Create a container outside of the WASB FileSystem container.create(); // Add a file to the container outside of the WASB FileSystem CloudBlockBlob blob = testAccount.getBlobReference("foo"); BlobOutputStream outputStream = blob.openOutputStream(); outputStream.write(new byte[10]); outputStream.close(); // Make sure the file is visible assertTrue(fs.exists(new Path("/foo"))); assertTrue(container.exists()); }
BlobOutputStream blobOutputStream = blockBlob.openOutputStream(null, options, null); for (int i = 0; i < writes; i ++) { blobOutputStream.write(buffer, i*length, length);
@Test public void testClose() throws URISyntaxException, StorageException, IOException { String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob"); CloudBlockBlob blockBlob = this.container.getBlockBlobReference(blobName); BlobOutputStream str = blockBlob.openOutputStream(); str.close(); try { str.close(); fail("Can't close twice."); } catch(IOException e) { assertEquals(SR.STREAM_CLOSED, e.getMessage()); } str = blockBlob.openOutputStream(); str.write(8); ArrayList<BlockEntry> blocks = blockBlob.downloadBlockList(BlockListingFilter.ALL, null, null, null); assertEquals(0, blocks.size()); str.close(); blocks = blockBlob.downloadBlockList(BlockListingFilter.COMMITTED, null, null, null); assertEquals(1, blocks.size()); }
@Test public void testContainerExistAfterDoesNotExist() throws Exception { testAccount = blobStorageTestAccount(); assumeNotNull(testAccount); CloudBlobContainer container = testAccount.getRealContainer(); FileSystem fs = testAccount.getFileSystem(); // Starting off with the container not there assertFalse(container.exists()); // A list shouldn't create the container and will set file system store // state to DoesNotExist try { fs.listStatus(new Path("/")); assertTrue("Should've thrown.", false); } catch (FileNotFoundException ex) { assertTrue("Unexpected exception: " + ex, ex.getMessage().contains("is not found")); } assertFalse(container.exists()); // Create a container outside of the WASB FileSystem container.create(); // Add a file to the container outside of the WASB FileSystem CloudBlockBlob blob = testAccount.getBlobReference("foo"); BlobOutputStream outputStream = blob.openOutputStream(); outputStream.write(new byte[10]); outputStream.close(); // Make sure the file is visible assertTrue(fs.exists(new Path("/foo"))); assertTrue(container.exists()); }
BlobOutputStream outputStream = blob.openOutputStream(); outputStream.write(new byte[fileSize]); outputStream.close();
private void smallPutThresholdHelper(int length, int writes, BlobRequestOptions options) throws URISyntaxException, StorageException, IOException { byte[] buffer = BlobTestHelper.getRandomBuffer(length*writes); String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("concurrency"); CloudBlockBlob blockBlob = this.container.getBlockBlobReference(blobName); blockBlob.setStreamWriteSizeInBytes(length); BlobOutputStream blobOutputStream = blockBlob.openOutputStream(null, options, null); for (int i = 0; i < writes; i ++) { blobOutputStream.write(buffer, i*length, length); } blobOutputStream.flush(); ArrayList<BlockEntry> blocks = blockBlob.downloadBlockList(BlockListingFilter.UNCOMMITTED, null, null, null); assertEquals(writes, blocks.size()); blobOutputStream.close(); blocks = blockBlob.downloadBlockList(BlockListingFilter.COMMITTED, null, null, null); assertEquals(writes, blocks.size()); byte[] outBuffer = new byte[writes*length]; blockBlob.downloadToByteArray(outBuffer, 0); for (int i = 0; i < length*writes; i ++) { assertEquals(buffer[i], outBuffer[i]); } }
@Test public void testAppendBlockOperations() throws Exception { CloudBlobContainer container = getTestAccount().getRealContainer(); OperationContext context = new OperationContext(); context.getResponseReceivedEventHandler().addListener( new ResponseReceivedEventHandler()); context.getSendingRequestEventHandler().addListener( new SendingRequestEventHandler()); CloudAppendBlob appendBlob = container.getAppendBlobReference( "testAppendBlockOperations"); assertNull(lastOperationTypeSent); assertNull(lastOperationTypeReceived); assertEquals(0, lastContentLengthReceived); try ( BlobOutputStream output = appendBlob.openWriteNew(null, null, context); ) { assertEquals(BlobOperationDescriptor.OperationType.CreateBlob, lastOperationTypeReceived); assertEquals(0, lastContentLengthReceived); String message = "this is a test"; output.write(message.getBytes("UTF-8")); output.flush(); assertEquals(BlobOperationDescriptor.OperationType.AppendBlock, lastOperationTypeSent); assertEquals(BlobOperationDescriptor.OperationType.AppendBlock, lastOperationTypeReceived); assertEquals(message.length(), lastContentLengthReceived); } }
output.write(message.getBytes("UTF-8")); output.flush(); assertEquals(BlobOperationDescriptor.OperationType.PutBlock,