@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 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 testOpenOutputStreamNoArgs() throws URISyntaxException, StorageException {
    String blobName = BlobTestHelper.generateRandomBlobNameWithPrefix("testblob");
    CloudPageBlob pageBlob = this.container.getPageBlobReference(blobName);

    try {
      pageBlob.openWriteExisting();
    } catch (StorageException ex) {
      assertEquals("The specified blob does not exist.", ex.getMessage());
      assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode());
    }

    pageBlob.openWriteNew(1024);
    pageBlob.openWriteExisting();

    CloudPageBlob pageBlob2 = this.container.getPageBlobReference(blobName);
    pageBlob2.downloadAttributes();
    assertEquals(1024, pageBlob2.getProperties().getLength());
    assertEquals(BlobType.PAGE_BLOB, pageBlob2.getProperties().getBlobType());
  }