Skip to content

Commit 3a70d08

Browse files
jean-philippe-martinmziccard
authored andcommitted
---
yaml --- r: 4311 b: refs/heads/gcs-nio c: 7ea0fc8 h: refs/heads/master i: 4309: 2f8c56f 4307: 7b25185 4303: 3c74cd4
1 parent 555fc7f commit 3a70d08

4 files changed

Lines changed: 123 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ refs/tags/v0.0.12: 2fd8066e891fb3dfea69b65f6bf6461db79342b9
1111
refs/heads/compute-alpha: 969cba2627f1d53d352cc4a5ffe0879dacf65e6c
1212
refs/heads/dns-alpha: 2f90e7e338349287ace33375896907af0f032ca1
1313
refs/heads/dns-alpha-batch: 17442b07867021b85d0452f5f3eda29a3413288f
14-
refs/heads/gcs-nio: e1d734a3c99a328ec35af7502a7da76273fddda9
14+
refs/heads/gcs-nio: 7ea0fc88ab8dcacf6c09b06fe9fc6fac7b5be2df
1515
refs/heads/logging-alpha: db5312bffa7fccac194f6a7feb8cc3066de16aff
1616
refs/tags/v0.1.0: a615317f7424ed58621b1f65d5c4d8cbbe8a6ed8
1717
refs/tags/v0.1.1: 7a7f6985fe465e9dd6a075af55493f42b4933be0

branches/gcs-nio/gcloud-java-examples/pom.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@
6363
<mainClass>com.google.cloud.examples.nio.Stat</mainClass>
6464
<name>Stat</name>
6565
</program>
66+
<program>
67+
<mainClass>com.google.cloud.examples.nio.CountBytes</mainClass>
68+
<name>CountBytes</name>
69+
</program>
6670
<program>
6771
<mainClass>
6872
com.google.cloud.examples.resourcemanager.ResourceManagerExample
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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;
18+
19+
import com.google.common.base.Stopwatch;
20+
21+
import java.io.IOException;
22+
import java.net.URI;
23+
import java.nio.ByteBuffer;
24+
import java.nio.channels.SeekableByteChannel;
25+
import java.nio.file.Files;
26+
import java.nio.file.Path;
27+
import java.nio.file.Paths;
28+
import java.util.concurrent.TimeUnit;
29+
30+
/**
31+
* CountBytes will read through the whole file given as input.
32+
*
33+
* <p>This example shows how to read a file size using NIO.
34+
* File.size returns the size of the file as saved in Storage metadata.
35+
* This class also shows how to read all of the file's contents using NIO,
36+
* and reports how long it took.
37+
*
38+
* <p>See the README for compilation instructions. Run this code with
39+
* {@code target/appassembler/bin/CountBytes <file>}
40+
*/
41+
public class CountBytes {
42+
43+
/**
44+
* See the class documentation.
45+
*/
46+
public static void main(String[] args) throws IOException {
47+
if (args.length == 0 || args[0].equals("--help")) {
48+
help();
49+
return;
50+
}
51+
for (String a : args) {
52+
countFile(a);
53+
}
54+
}
55+
56+
/**
57+
* Print the length of the indicated file.
58+
*
59+
* <p>This uses the normal Java NIO Api, so it can take advantage of any installed
60+
* NIO Filesystem provider without any extra effort.
61+
*/
62+
private static void countFile(String fname) {
63+
// large buffers pay off
64+
final int bufSize = 50 * 1024 * 1024;
65+
try {
66+
Path path = Paths.get(new URI(fname));
67+
long size = Files.size(path);
68+
System.out.println(fname + ": " + size + " bytes.");
69+
ByteBuffer buf = ByteBuffer.allocate(bufSize);
70+
System.out.println("Reading the whole file...");
71+
Stopwatch sw = Stopwatch.createStarted();
72+
SeekableByteChannel chan = Files.newByteChannel(path);
73+
long total = 0;
74+
int readCalls = 0;
75+
while (chan.read(buf) > 0) {
76+
readCalls++;
77+
total += buf.position();
78+
buf.flip();
79+
}
80+
readCalls++; // We must count the last call
81+
long elapsed = sw.elapsed(TimeUnit.SECONDS);
82+
System.out.println("Read all " + total + " bytes in " + elapsed + "s. " +
83+
"(" + readCalls +" calls to chan.read)");
84+
if (total != size) {
85+
System.out.println("Wait, this doesn't match! We saw " + total + " bytes, " +
86+
"yet the file size is listed at " + size + " bytes.");
87+
}
88+
} catch (Exception ex) {
89+
System.out.println(fname + ": " + ex.toString());
90+
}
91+
}
92+
93+
private static void help() {
94+
String[] help =
95+
{"The argument is a <path>",
96+
"and we show the length of that file."
97+
};
98+
for (String s : help) {
99+
System.out.println(s);
100+
}
101+
}
102+
}

branches/gcs-nio/gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/Stat.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
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+
117
package com.google.cloud.examples.nio;
218

319
import java.io.IOException;

0 commit comments

Comments
 (0)