public static CloudFile uploadNewFile( CloudFileShare share, InputStream stream, int length, OperationContext context) throws StorageException, IOException, URISyntaxException { String name = generateRandomFileName(); CloudFile file = null; file = share.getRootDirectoryReference().getFileReference(name); file.upload(stream, length, null, null, context); return file; }
@Test public void testCloudFileDownloadRangeToByteArray() throws URISyntaxException, StorageException, IOException { CloudFile file = this.share.getRootDirectoryReference().getFileReference( FileTestHelper.generateRandomFileName()); FileTestHelper.doDownloadRangeToByteArrayTest(file, 8 * 1024 * 1024, 8 * 1024 * 1024, 1 * 1024 * 1024, new Long(1 * 1024 * 1024), new Long(5 * 1024 * 1024)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 8 * 1024 * 1024, 8 * 1024 * 1024, 2 * 1024 * 1024, new Long(2 * 1024 * 1024), new Long(6 * 1024 * 1024)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 8 * 1024 * 1024, 8 * 1024 * 1024, 1 * 1024 * 1024, new Long(4 * 1024 * 1024), new Long(4 * 1024 * 1024)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 2 * 512, 4 * 512, 0, new Long(1 * 512), new Long(1 * 512)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 2 * 512, 4 * 512, 1 * 512, new Long(0), null); FileTestHelper.doDownloadRangeToByteArrayTest(file, 2 * 512, 4 * 512, 1 * 512, new Long(1 * 512), null); FileTestHelper.doDownloadRangeToByteArrayTest(file, 2 * 512, 4 * 512, 1 * 512, new Long(0), new Long(1 * 512)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 2 * 512, 4 * 512, 2 * 512, new Long(1 * 512), new Long( 1 * 512)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 2 * 512, 4 * 512, 2 * 512, new Long(1 * 512), new Long( 2 * 512)); // Edge cases FileTestHelper.doDownloadRangeToByteArrayTest(file, 1024, 1024, 1023, new Long(1023), new Long(1)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 1024, 1024, 0, new Long(1023), new Long(1)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 1024, 1024, 0, new Long(0), new Long(1)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 1024, 1024, 0, new Long(512), new Long(1)); FileTestHelper.doDownloadRangeToByteArrayTest(file, 1024, 1024, 512, new Long(1023), new Long(1)); }
@Test public void testFileContentMD5NewFileTest() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); FileRequestOptions options = new FileRequestOptions(); options.setStoreFileContentMD5(true); options.setDisableContentMD5Validation(false); File tempFile = File.createTempFile("sourceFile", ".tmp"); file.uploadFromFile(tempFile.getAbsolutePath(), null, options, null); }
@Test public void testCloudFileDownloadRangeToByteArrayNegativeTest() throws URISyntaxException, StorageException, IOException { CloudFile file = this.share.getRootDirectoryReference().getFileReference( FileTestHelper.generateRandomFileName()); FileTestHelper.doDownloadRangeToByteArrayNegativeTests(file); }
@Test public void testFileUploadFromStreamTest() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); int length = 2 * 1024; ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length); fileRef.upload(srcStream, length); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); fileRef.download(dstStream); FileTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = FileTestHelper.getRandomDataStream(length); fileRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); fileRef.download(dstStream); FileTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
/** * Test file stream uploading. * * @throws URISyntaxException * @throws StorageException */ @Test public void testFileUploadFromStreamTest1() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); int length = 2 * 1024; ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length); fileRef.upload(srcStream, length); ByteArrayOutputStream dstStream = new ByteArrayOutputStream(); fileRef.download(dstStream); FileTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); length = 5 * 1024 * 1024; srcStream = FileTestHelper.getRandomDataStream(length); fileRef.upload(srcStream, length); dstStream = new ByteArrayOutputStream(); fileRef.download(dstStream); FileTestHelper.assertStreamsAreEqual(srcStream, new ByteArrayInputStream(dstStream.toByteArray())); }
byte[] buffer = FileTestHelper.getRandomBuffer(8 * 512); String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(fileLengthToUse);
/** * Test file upload and download using text. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileUploadDownloadFromText() throws IOException, StorageException, URISyntaxException { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); String textFile = "string of text to upload to a file. The_quick_brown_fox_jumps_over_the_lazy_dog. 1!\". "."; file.uploadText(textFile); String result = file.downloadText(); assertEquals(textFile, result); }
@Test public void testFileEmptyHeaderSigningTest() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); final int length = 2 * 1024; ByteArrayInputStream srcStream = FileTestHelper.getRandomDataStream(length); OperationContext context = new OperationContext(); context.getSendingRequestEventHandler().addListener(new StorageEvent<SendingRequestEvent>() { @Override public void eventOccurred(SendingRequestEvent eventArg) { HttpURLConnection connection = (HttpURLConnection) eventArg.getConnectionObject(); connection.setRequestProperty("x-ms-foo", ""); } }); fileRef.upload(srcStream, length, null, null, context); fileRef.download(new ByteArrayOutputStream(), null, null, context); }
/** * Test file uploading from byte arrays. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileUploadFromByteArray() throws Exception { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); this.doUploadFromByteArrayTest(file, 4 * 512, 0, 4 * 512); this.doUploadFromByteArrayTest(file, 4 * 512, 0, 2 * 512); this.doUploadFromByteArrayTest(file, 4 * 512, 1 * 512, 2 * 512); this.doUploadFromByteArrayTest(file, 4 * 512, 2 * 512, 2 * 512); }
/** * Test file upload and download using io files. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileUploadDownloadFromFile() throws IOException, StorageException, URISyntaxException { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); this.doUploadDownloadFileTest(file, 512); this.doUploadDownloadFileTest(file, 4096); this.doUploadDownloadFileTest(file, 5 * 1024 * 1024); this.doUploadDownloadFileTest(file, 11 * 1024 * 1024); }
byte[] buffer = FileTestHelper.getRandomBuffer(8 * 512); String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(fileLengthToUse);
byte[] buffer = FileTestHelper.getRandomBuffer(8 * 512); String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(fileLengthToUse);
final int fileLength = 16 * 1024; final Random randGenerator = new Random(); String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName);
/** * Test clearing file ranges. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileClearRange() throws URISyntaxException, StorageException, IOException { int fileLengthToUse = 8 * 512; byte[] buffer = FileTestHelper.getRandomBuffer(8 * 512); String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(fileLengthToUse); // Upload one page (page 0) ByteArrayInputStream inputStream = new ByteArrayInputStream(buffer); fileRef.uploadRange(inputStream, 0, fileLengthToUse); fileRef.clearRange(3 * 512, 2 * 512); byte[] result = new byte[fileLengthToUse]; fileRef.downloadToByteArray(result, 0); int i = 0; for (; i < 3 * 512; i++) { assertEquals(buffer[i], result[i]); } for (; i < 5 * 512; i++) { assertEquals(0, result[i]); } for (; i < 8 * 512; i++) { assertEquals(buffer[i], result[i]); } }
/** * Test requesting stored content MD5 with OpenWriteExisting(). * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileOpenWriteExistingWithMD5() throws URISyntaxException, StorageException, IOException { String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.create(512); FileRequestOptions options = new FileRequestOptions(); options.setStoreFileContentMD5(true); options.setDisableContentMD5Validation(false); try { fileRef.openWriteExisting(null, options, null); fail("Expect failure due to requesting MD5 calculation"); } catch (IllegalArgumentException e) { } }
/** * Create a file and try to download a range of its contents * * @throws StorageException * @throws URISyntaxException * @throws IOException * @throws InterruptedException */ @Test public void testFileDownloadRangeValidationTest() throws StorageException, URISyntaxException, IOException { final int length = 5 * 1024 * 1024; final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef.upload(FileTestHelper.getRandomDataStream(length), length); //Download full file fileRef.download(new ByteArrayOutputStream()); assertEquals(length, fileRef.getProperties().getLength()); //Download file range. byte[] downloadBuffer = new byte[100]; int downloadLength = fileRef.downloadRangeToByteArray(0, (long) 100, downloadBuffer, 0); assertEquals(length, fileRef.getProperties().getLength()); assertEquals(100, downloadLength); }
String fileName1 = FileTestHelper.generateRandomFileName(); CloudFile fileRef1 = this.share.getRootDirectoryReference().getFileReference(fileName1); fileName1 = FileTestHelper.generateRandomFileName(); fileRef1 = this.share.getRootDirectoryReference().getFileReference(fileName1);
@Test @Category({ DevFabricTests.class, DevStoreTests.class }) public void testVerifyTransactionalMD5ValidationMissingOverallMD5() throws URISyntaxException, StorageException, IOException { final String fileName = FileTestHelper.generateRandomFileName(); final CloudFile fileRef = this.share.getRootDirectoryReference().getFileReference(fileName); final int length = 3*1024; ByteArrayInputStream srcStream = BlobTestHelper.getRandomDataStream(length); FileRequestOptions options = new FileRequestOptions(); options.setDisableContentMD5Validation(true); options.setStoreFileContentMD5(false); fileRef.upload(srcStream, length, null, options, null); options.setDisableContentMD5Validation(false); options.setStoreFileContentMD5(true); options.setUseTransactionalContentMD5(true); final CloudFile fileRef2 = this.share.getRootDirectoryReference().getFileReference(fileName); fileRef2.downloadRange(1024, (long)1024, new ByteArrayOutputStream(), null, options, null); assertNull(fileRef2.getProperties().getContentMD5()); }
/** * Test FileOutputStream. * * @throws URISyntaxException * @throws StorageException */ @Test public void testCloudFileOpenOutputStreamNoArgs() throws URISyntaxException, StorageException { String fileName = FileTestHelper.generateRandomFileName(); CloudFile file = this.share.getRootDirectoryReference().getFileReference(fileName); try { file.openWriteExisting(); } catch (StorageException ex) { assertEquals("The specified resource does not exist.", ex.getMessage()); assertEquals(HttpURLConnection.HTTP_NOT_FOUND, ex.getHttpStatusCode()); } file.openWriteNew(1024); file.openWriteExisting(); CloudFile file2 = this.share.getRootDirectoryReference().getFileReference(fileName); file2.downloadAttributes(); assertEquals(1024, file2.getProperties().getLength()); }