Skip to content

Commit 7b35876

Browse files
committed
Refactor nio's package-info and add snippets
1 parent 4653567 commit 7b35876

File tree

6 files changed

+251
-45
lines changed

6 files changed

+251
-45
lines changed

gcloud-java-contrib/gcloud-java-nio/src/main/java/com/google/cloud/storage/contrib/nio/package-info.java

Lines changed: 47 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,55 @@
2222
*
2323
* <h3>How It Works</h3>
2424
*
25-
* The simplest way to get started is with {@code Paths} and {@code Files}:<pre> {@code
26-
*
25+
* The simplest way to get started is with {@code Paths} and {@code Files}:
26+
* <pre>{@code
2727
* Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
28-
* List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);}</pre>
28+
* List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
29+
* }</pre>
30+
*
31+
* <p>For the complete source code see
32+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/ReadAllLines.java">
33+
* ReadAllLines.java</a>.
2934
*
3035
* <p>If you want to configure the bucket per-environment, it might make more sense to use the
3136
* {@code FileSystem} API:
32-
* <pre>
33-
* class Foo {
34-
* static String bucket = System.getProperty(...);
35-
* static FileSystem fs = FileSystems.getFileSystem(URI.create("gs://" + bucket));
36-
* void bar() {
37-
* byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
38-
* Path path = fs.getPath("/object");
39-
* Files.write(path, data);
40-
* data = Files.readBytes(path);
41-
* }
42-
* void doodle() {
43-
* Path path = fs.getPath("/path/to/doodle.csv");
44-
* List&lt;String&gt; lines = Files.readAllLines(path, StandardCharsets.UTF_8);
45-
* }
46-
* }</pre>
47-
*
48-
* <p>You can also use InputStream and OutputStream for streaming:
37+
* <pre>{@code
38+
* FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket"));
39+
* byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
40+
* Path path = fs.getPath("/object");
41+
* Files.write(path, data);
42+
* List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
43+
* }</pre>
44+
*
45+
* <p>For the complete source code see
46+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/GetFileSystem.java">
47+
* GetFileSystem.java</a>.
48+
*
49+
* <p>You can also use {@code InputStream} and {@code OutputStream} for streaming:
4950
* <pre>
5051
* Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
51-
* try (InputStream input = Files.openInputStream(path)) {
52-
* // ...
53-
* }</pre>
52+
* try (InputStream input = Files.newInputStream(path)) {
53+
* // use input stream
54+
* }
55+
* </pre>
56+
*
57+
* <p>For the complete source code see
58+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateInputStream.java">
59+
* CreateInputStream.java</a>.
5460
*
5561
* <p>You can set various attributes using
5662
* {@link com.google.cloud.storage.contrib.nio.CloudStorageOptions CloudStorageOptions} static
5763
* helpers:
5864
* <pre>
59-
* Files.write(csvPath, csvLines, StandardCharsets.UTF_8,
60-
* withMimeType(MediaType.CSV_UTF8),
61-
* withoutCaching());</pre>
65+
* Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
66+
* Files.write(path, csvLines, StandardCharsets.UTF_8,
67+
* withMimeType("text/csv; charset=UTF-8"),
68+
* withoutCaching());
69+
* </pre>
70+
*
71+
* <p>For the complete source code see
72+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/WriteFileWithAttributes.java">
73+
* WriteFileWithAttributes.java</a>.
6274
*
6375
* <p><b>NOTE:</b> Cloud Storage uses a flat namespace and therefore doesn't support real
6476
* directories. So this library supports what's known as "pseudo-directories". Any path that
@@ -67,32 +79,22 @@
6779
* would with the normal UNIX file system implementation. You can disable this feature with
6880
* {@link com.google.cloud.storage.contrib.nio.CloudStorageConfiguration#usePseudoDirectories()}.
6981
*
70-
* <h3>Unit Testing</h3>
71-
*
72-
* <p>Here's a simple unit test:<pre>
73-
*
74-
* class MyTest {
75-
* {@literal @}Rule
76-
* public final AppEngineRule appEngine = AppEngineRule.builder().build();
77-
*
78-
* {@literal @}Test
79-
* public test_fileWrite() throws Exception {
80-
* Path path = Paths.get(URI.create("gs://bucket/traditional"));
81-
* Files.write(path, "eyebrow".getBytes(StandardCharsets.US_ASCII));
82-
* assertEquals("eyebrow", new String(Files.readBytes(path), StandardCharsets.US_ASCII));
83-
* }
84-
* }</pre>
85-
*
8682
* <h3>Non-SPI Interface</h3>
8783
*
8884
* <p>If you don't want to rely on Java SPI, which requires a META-INF file in your jar generated by
89-
* Google Auto, you can instantiate this file system directly as follows:<pre> {@code
85+
* Google Auto, you can instantiate this file system directly as follows:
9086
*
91-
* CloudStorageFileSystem fs = CloudStorageFileSystemProvider.forBucket("bucket");
87+
* <pre>
88+
* CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("bucket");
9289
* byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
9390
* Path path = fs.getPath("/object");
9491
* Files.write(path, data);
95-
* data = Files.readBytes(path);}</pre>
92+
* data = Files.readAllBytes(path);
93+
* </pre>
94+
*
95+
* <p>For the complete source code see
96+
* <a href="https://github.com/GoogleCloudPlatform/gcloud-java/tree/master/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateCloudStorageFileSystem.java">
97+
* CreateCloudStorageFileSystem.java</a>.
9698
*/
9799
@javax.annotation.ParametersAreNonnullByDefault
98100
package com.google.cloud.storage.contrib.nio;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.nio.snippets;
18+
19+
import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem;
20+
21+
import java.io.IOException;
22+
import java.nio.charset.StandardCharsets;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
25+
26+
/**
27+
* A snippet for Google Cloud Storage NIO that shows how to create a {@link CloudStorageFileSystem}
28+
* for a bucket. The snippet also shows how to create a file, given the file system.
29+
*/
30+
public class CreateCloudStorageFileSystem {
31+
32+
public static void main(String... args) throws IOException {
33+
// Create a file system for the bucket
34+
CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("bucket");
35+
byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
36+
Path path = fs.getPath("/object");
37+
// Write a file in the bucket
38+
Files.write(path, data);
39+
// Read a file from the bucket
40+
data = Files.readAllBytes(path);
41+
}
42+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.nio.snippets;
18+
19+
import java.io.IOException;
20+
import java.io.InputStream;
21+
import java.net.URI;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
26+
/**
27+
* A snippet showing how to create an input stream for a Google Cloud Storage file using NIO.
28+
*/
29+
public class CreateInputStream {
30+
31+
public static void main(String... args) throws IOException {
32+
Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
33+
try (InputStream input = Files.newInputStream(path)) {
34+
// use input stream
35+
}
36+
}
37+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.nio.snippets;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.FileSystem;
23+
import java.nio.file.FileSystems;
24+
import java.nio.file.Files;
25+
import java.nio.file.Path;
26+
import java.util.List;
27+
28+
/**
29+
* A snippet showing how to get a {@link FileSystem} instance for a Google Cloud Storage bucket.
30+
* This snippet also shows how to create a file and read its lines.
31+
*/
32+
public class GetFileSystem {
33+
34+
public static void main(String... args) throws IOException {
35+
FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket"));
36+
byte[] data = "hello world".getBytes(StandardCharsets.UTF_8);
37+
Path path = fs.getPath("/object");
38+
Files.write(path, data);
39+
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
40+
}
41+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.nio.snippets;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.nio.charset.StandardCharsets;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.nio.file.Paths;
25+
import java.util.List;
26+
27+
/**
28+
* A snippet showing how to read all lines of a Google Cloud Storage file using NIO.
29+
*/
30+
public class ReadAllLines {
31+
32+
public static void main(String... args) throws IOException {
33+
Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
34+
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8);
35+
}
36+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2016 Google Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.cloud.examples.nio.snippets;
18+
19+
import static com.google.cloud.storage.contrib.nio.CloudStorageOptions.withMimeType;
20+
import static com.google.cloud.storage.contrib.nio.CloudStorageOptions.withoutCaching;
21+
22+
import com.google.cloud.storage.contrib.nio.CloudStorageOptions;
23+
24+
import java.io.IOException;
25+
import java.net.URI;
26+
import java.nio.charset.StandardCharsets;
27+
import java.nio.file.Files;
28+
import java.nio.file.Path;
29+
import java.nio.file.Paths;
30+
import java.util.Arrays;
31+
import java.util.List;
32+
33+
/**
34+
* A snippet showing how to write a file to Google Cloud Storage using NIO. This example also shows
35+
* how to set file attributes, using {@link CloudStorageOptions} static helpers.
36+
*/
37+
public class WriteFileWithAttributes {
38+
39+
private static final String[] LINES = {"value1,", "value"};
40+
41+
public static void main(String... args) throws IOException {
42+
List<String> csvLines = Arrays.asList(LINES);
43+
Path path = Paths.get(URI.create("gs://bucket/lolcat.csv"));
44+
Files.write(path, csvLines, StandardCharsets.UTF_8,
45+
withMimeType("text/csv; charset=UTF-8"),
46+
withoutCaching());
47+
}
48+
}

0 commit comments

Comments
 (0)