-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor nio's package-info and add snippets
- Loading branch information
Showing
6 changed files
with
251 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...es/src/main/java/com/google/cloud/examples/nio/snippets/CreateCloudStorageFileSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.nio.snippets; | ||
|
||
import com.google.cloud.storage.contrib.nio.CloudStorageFileSystem; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
/** | ||
* A snippet for Google Cloud Storage NIO that shows how to create a {@link CloudStorageFileSystem} | ||
* for a bucket. The snippet also shows how to create a file, given the file system. | ||
*/ | ||
public class CreateCloudStorageFileSystem { | ||
|
||
public static void main(String... args) throws IOException { | ||
// Create a file system for the bucket | ||
CloudStorageFileSystem fs = CloudStorageFileSystem.forBucket("bucket"); | ||
byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); | ||
Path path = fs.getPath("/object"); | ||
// Write a file in the bucket | ||
Files.write(path, data); | ||
// Read a file from the bucket | ||
data = Files.readAllBytes(path); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
...java-examples/src/main/java/com/google/cloud/examples/nio/snippets/CreateInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.nio.snippets; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
|
||
/** | ||
* A snippet showing how to create an input stream for a Google Cloud Storage file using NIO. | ||
*/ | ||
public class CreateInputStream { | ||
|
||
public static void main(String... args) throws IOException { | ||
Path path = Paths.get(URI.create("gs://bucket/lolcat.csv")); | ||
try (InputStream input = Files.newInputStream(path)) { | ||
// use input stream | ||
} | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/GetFileSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.nio.snippets; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.FileSystem; | ||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.List; | ||
|
||
/** | ||
* A snippet showing how to get a {@link FileSystem} instance for a Google Cloud Storage bucket. | ||
* This snippet also shows how to create a file and read its lines. | ||
*/ | ||
public class GetFileSystem { | ||
|
||
public static void main(String... args) throws IOException { | ||
FileSystem fs = FileSystems.getFileSystem(URI.create("gs://bucket")); | ||
byte[] data = "hello world".getBytes(StandardCharsets.UTF_8); | ||
Path path = fs.getPath("/object"); | ||
Files.write(path, data); | ||
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/snippets/ReadAllLines.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.nio.snippets; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
/** | ||
* A snippet showing how to read all lines of a Google Cloud Storage file using NIO. | ||
*/ | ||
public class ReadAllLines { | ||
|
||
public static void main(String... args) throws IOException { | ||
Path path = Paths.get(URI.create("gs://bucket/lolcat.csv")); | ||
List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
...xamples/src/main/java/com/google/cloud/examples/nio/snippets/WriteFileWithAttributes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.examples.nio.snippets; | ||
|
||
import static com.google.cloud.storage.contrib.nio.CloudStorageOptions.withMimeType; | ||
import static com.google.cloud.storage.contrib.nio.CloudStorageOptions.withoutCaching; | ||
|
||
import com.google.cloud.storage.contrib.nio.CloudStorageOptions; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* A snippet showing how to write a file to Google Cloud Storage using NIO. This example also shows | ||
* how to set file attributes, using {@link CloudStorageOptions} static helpers. | ||
*/ | ||
public class WriteFileWithAttributes { | ||
|
||
private static final String[] LINES = {"value1,", "value"}; | ||
|
||
public static void main(String... args) throws IOException { | ||
List<String> csvLines = Arrays.asList(LINES); | ||
Path path = Paths.get(URI.create("gs://bucket/lolcat.csv")); | ||
Files.write(path, csvLines, StandardCharsets.UTF_8, | ||
withMimeType("text/csv; charset=UTF-8"), | ||
withoutCaching()); | ||
} | ||
} |