Skip to content

Commit 25afcde

Browse files
authored
Nio output stream open options (Azure#18735)
* Updated the flags * Added tests * Changelog
1 parent 0ab856a commit 25afcde

File tree

6 files changed

+890
-68
lines changed

6 files changed

+890
-68
lines changed

sdk/storage/azure-storage-blob-nio/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
## 12.0.0-beta.3 (Unreleased)
44
- Added support for FileSystemProvider.checkAccess method
55
- Added support for file key on AzureBasicFileAttributes and AzureBlobFileAttributes
6+
- Adjusted the required flags for opening an outputstream
67

78
## 12.0.0-beta.2 (2020-08-13)
89
- Added checks to ensure file system has not been closed before operating on data

sdk/storage/azure-storage-blob-nio/src/main/java/com/azure/storage/blob/nio/AzureFileSystemProvider.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -372,11 +372,16 @@ public OutputStream newOutputStream(Path path, OpenOption... options) throws IOE
372372
}
373373
}
374374

375-
// Write and truncate must be specified
375+
/*
376+
Write must be specified. Either create_new or truncate must be specified. This is to ensure that no edits or
377+
appends are allowed.
378+
*/
376379
if (!optionsList.contains(StandardOpenOption.WRITE)
377-
|| !optionsList.contains(StandardOpenOption.TRUNCATE_EXISTING)) {
380+
|| !(optionsList.contains(StandardOpenOption.TRUNCATE_EXISTING)
381+
|| optionsList.contains(StandardOpenOption.CREATE_NEW))) {
378382
throw LoggingUtility.logError(logger,
379-
new IllegalArgumentException("Write and TruncateExisting must be specified to open an OutputStream"));
383+
new IllegalArgumentException("Write and either CreateNew or TruncateExisting must be specified to open "
384+
+ "an OutputStream"));
380385
}
381386

382387
AzureResource resource = new AzureResource(path);

sdk/storage/azure-storage-blob-nio/src/test/java/com/azure/storage/blob/nio/AzureFileSystemProviderTest.groovy

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ class AzureFileSystemProviderTest extends APISpec {
931931

932932
def "OutputStream options create"() {
933933
// Create works both on creating new and opening existing. We test these scenarios above.
934-
// Here we assert that we cannot create without this option
934+
// Here we assert that we cannot create without this option (i.e. you are only allowed to overwrite, not create)
935935
setup:
936936
def fs = createFS(config)
937937

@@ -982,12 +982,25 @@ class AzureFileSystemProviderTest extends APISpec {
982982
then:
983983
thrown(IllegalArgumentException)
984984

985-
when: "Missing TRUNCATE_EXISTING"
985+
when: "Missing TRUNCATE_EXISTING and CREATE_NEW"
986986
fs.provider().newOutputStream(fs.getPath(generateBlobName()), StandardOpenOption.WRITE)
987987

988988
then:
989989
thrown(IllegalArgumentException)
990990

991+
when: "Missing only TRUNCATE_EXISTING"
992+
fs.provider().newOutputStream(fs.getPath(generateBlobName()), StandardOpenOption.WRITE,
993+
StandardOpenOption.CREATE_NEW)
994+
995+
then:
996+
notThrown(IllegalArgumentException)
997+
998+
when: "Missing only CREATE_NEW"
999+
fs.provider().newOutputStream(fs.getPath(generateBlobName()), StandardOpenOption.WRITE,
1000+
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.CREATE)
1001+
1002+
then:
1003+
notThrown(IllegalArgumentException)
9911004
}
9921005

9931006
@Unroll

sdk/storage/azure-storage-blob-nio/src/test/java/com/azure/storage/blob/nio/CompositeTest.groovy

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.azure.storage.blob.nio
22

33
import java.nio.file.Files
4+
import java.nio.file.StandardCopyOption
45

56
/**
67
* This test class is for testing static helper methods provided by the JDK. Customers often rely on these methods
@@ -27,4 +28,38 @@ class CompositeTest extends APISpec {
2728
Files.isDirectory(fs.getPath('mydir1/mydir2'))
2829
Files.isDirectory(fs.getPath('mydir1/mydir2/mydir3'))
2930
}
31+
32+
def "Files copy"() {
33+
setup:
34+
def fs = createFS(config)
35+
def dest = fs.getPath("dest")
36+
def resultArr = new byte[defaultDataSize]
37+
38+
when:
39+
Files.copy(defaultInputStream.get(), dest)
40+
fs.provider().newInputStream(dest).read(resultArr)
41+
42+
then:
43+
resultArr == defaultData.array()
44+
45+
when:
46+
def dest2 = fs.getPath("dest2")
47+
def outStream = fs.provider().newOutputStream(dest2)
48+
Files.copy(dest, outStream)
49+
outStream.close()
50+
resultArr = new byte[defaultDataSize]
51+
fs.provider().newInputStream(dest2).read(resultArr)
52+
53+
then:
54+
resultArr == defaultData.array()
55+
56+
when:
57+
def dest3 = fs.getPath("dest3")
58+
Files.copy(dest, dest3, StandardCopyOption.COPY_ATTRIBUTES)
59+
resultArr = new byte[defaultDataSize]
60+
fs.provider().newInputStream(dest3).read(resultArr)
61+
62+
then:
63+
resultArr == defaultData.array()
64+
}
3065
}

0 commit comments

Comments
 (0)