View Javadoc
1   // Copyright (c) Microsoft Corporation. All rights reserved.
2   // Licensed under the MIT License.
3   
4   package com.azure.storage.blob;
5   
6   import com.azure.storage.common.credentials.SharedKeyCredential;
7   import org.junit.Assert;
8   import org.junit.BeforeClass;
9   
10  import java.io.ByteArrayOutputStream;
11  import java.io.IOException;
12  import java.io.InputStream;
13  import java.io.UncheckedIOException;
14  import java.util.ArrayList;
15  import java.util.List;
16  import java.util.Random;
17  
18  public class BlobOutputStreamTest {
19      private static final Random RANDOM = new Random();
20      private static BlobServiceClient storageClient;
21      private static ContainerClient containerClient;
22  
23      @BeforeClass
24      public static void setup() {
25          storageClient = new BlobServiceClientBuilder()
26              .endpoint("https://" + System.getenv("ACCOUNT_NAME") + ".blob.core.windows.net")
27              .credential(new SharedKeyCredential(System.getenv("ACCOUNT_NAME"), System.getenv("ACCOUNT_KEY")))
28  //            .httpClient(HttpClient.createDefault().proxy(() -> new ProxyOptions(Type.HTTP, new InetSocketAddress("localhost", 8888))))
29              .buildClient();
30          String containerName = "testcontainer" + RANDOM.nextInt(1000);
31          containerClient = storageClient.getContainerClient(containerName);
32          if (!containerClient.exists().value()) {
33              containerClient.create();
34          }
35      }
36  
37  //    @Test
38      public void testBlockBlobOutputStream() throws Exception {
39          String blobName = "testblob" + RANDOM.nextInt(1000);
40          int length = 100 * Constants.MB;
41          byte[] randomBytes = new byte[length];
42          RANDOM.nextBytes(randomBytes);
43  
44          BlockBlobClient blockBlobClient = containerClient.getBlockBlobClient(blobName);
45          BlobOutputStream outStream = blockBlobClient.getBlobOutputStream();
46          outStream.write(randomBytes);
47          outStream.close();
48  
49          Assert.assertEquals(length, blockBlobClient.getProperties().value().blobSize());
50          BlobInputStream blobInputStream = blockBlobClient.openInputStream();
51          byte[] downloaded = convertInputStreamToByteArray(blobInputStream);
52          Assert.assertArrayEquals(randomBytes, downloaded);
53      }
54  
55  //    @Test
56      public void testPageBlobOutputStream() throws Exception {
57          int length = 1024 * Constants.MB - 512;
58          String blobName = "testblob" + RANDOM.nextInt(1000);
59          byte[] randomBytes = new byte[length];
60          RANDOM.nextBytes(randomBytes);
61  
62          PageBlobClient pageBlobClient = containerClient.getPageBlobClient(blobName);
63          pageBlobClient.create(length);
64          BlobOutputStream outStream = pageBlobClient.getBlobOutputStream(length);
65          outStream.write(randomBytes);
66          outStream.close();
67  
68          BlobInputStream blobInputStream = pageBlobClient.openInputStream();
69          byte[] downloaded = convertInputStreamToByteArray(blobInputStream);
70          Assert.assertArrayEquals(randomBytes, downloaded);
71      }
72  
73  //    @Test
74      public void testAppendBlobOutputStream() throws Exception {
75          int length = 0;
76          String blobName = "testblob" + RANDOM.nextInt(1000);
77          List<byte[]> randomBytes = new ArrayList<>();
78          ByteArrayOutputStream stream = new ByteArrayOutputStream();
79          for (int i = 0; i != 64; ++i) {
80              int subLength = RANDOM.nextInt(4 * Constants.MB);
81              length += subLength;
82              byte[] bytes = new byte[subLength];
83              RANDOM.nextBytes(bytes);
84              randomBytes.add(bytes);
85              stream.write(bytes);
86          }
87  
88          byte[] uploaded = stream.toByteArray();
89  
90          AppendBlobClient appendBlobClient = containerClient.getAppendBlobClient(blobName);
91          appendBlobClient.create();
92          BlobOutputStream outStream = appendBlobClient.getBlobOutputStream();
93          for (int i = 0; i != 64; i++) {
94              outStream.write(randomBytes.get(i));
95          }
96          outStream.close();
97  
98          Assert.assertEquals(length, appendBlobClient.getProperties().value().blobSize());
99          BlobInputStream blobInputStream = appendBlobClient.openInputStream();
100         byte[] downloaded = convertInputStreamToByteArray(blobInputStream);
101         Assert.assertArrayEquals(uploaded, downloaded);
102     }
103 
104     private byte[] convertInputStreamToByteArray(InputStream inputStream) {
105         int b;
106         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
107         try {
108             while ((b = inputStream.read()) != -1) {
109                 outputStream.write(b);
110             }
111         } catch (IOException ex) {
112             throw new UncheckedIOException(ex);
113         }
114 
115         return outputStream.toByteArray();
116     }
117 }