/** * Opens an output stream object to write data to the append blob. The append blob does not need to yet exist. If * the blob already exists, this will replace it. * <p> * To avoid overwriting and instead throw an error, please use the * {@link #openWriteNew(AccessCondition, BlobRequestOptions, OperationContext)} overload with the appropriate * {@link AccessCondition}. * <p> * If you are doing writes in 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. * * @return A {@link BlobOutputStream} object used to write data to the blob. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public BlobOutputStream openWriteNew() throws StorageException { return this.openWriteNew(null /* accessCondition */, null /* options */, null /* opContext */); }
/** * Opens an output stream object to write data to the append blob. The append blob does not need to yet exist. If * the blob already exists, this will replace it. * <p> * To avoid overwriting and instead throw an error, please use the * {@link #openWriteNew(AccessCondition, BlobRequestOptions, OperationContext)} overload with the appropriate * {@link AccessCondition}. * <p> * If you are doing writes in 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. * * @return A {@link BlobOutputStream} object used to write data to the blob. * * @throws StorageException * If a storage service error occurred. */ @DoesServiceRequest public BlobOutputStream openWriteNew() throws StorageException { return this.openWriteNew(null /* accessCondition */, null /* options */, null /* opContext */); }
final BlobOutputStream streamRef = this.openWriteNew(accessCondition, options, opContext); try { streamRef.write(sourceStream, length);
final BlobOutputStream streamRef = this.openWriteNew(accessCondition, options, opContext); try { streamRef.write(sourceStream, length);
out = blob.openWriteNew(AccessCondition.generateEmptyCondition(), options, context);
@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]); } }
@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); } }
@Test public void testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String blobName = BlobTestHelper .generateRandomBlobNameWithPrefix("testblob"); CloudAppendBlob appendBlob = this.container .getAppendBlobReference(blobName); try { appendBlob.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified blob does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } appendBlob.openWriteNew(); appendBlob.openWriteExisting(); CloudAppendBlob appendBlob2 = this.container .getAppendBlobReference(blobName); appendBlob2.downloadAttributes(); assertEquals(0, appendBlob2.getProperties().getLength()); assertEquals(BlobType.APPEND_BLOB, appendBlob2.getProperties().getBlobType()); }
CloudAppendBlob blob = container.getAppendBlobReference(blobName); BlobOutputStream str = blob.openWriteNew(); byte[] buffer = BlobTestHelper.getRandomBuffer(blobSize); str.write(buffer);