Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NIO CountBytes example #975

Merged
merged 4 commits into from
May 4, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions gcloud-java-examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ To run examples from your command line:

2. Set your current project using `gcloud config set project PROJECT_ID`. This step is not necessary for `ResourceManagerExample`.

3. Compile using Maven: `mvn install -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true` in command line from your base project directory
then `cd gcloud-java-examples` and finally `mvn package appassembler:assemble -DskipTests -Dmaven.javadoc.skip=true -Dmaven.source.skip=true`.
3. Compile using Maven: `mvn install -DskipTests` in command line from your base project directory
then `cd gcloud-java-examples` and finally `mvn package appassembler:assemble -DskipTests`.

4. Run an example from the command line using the Maven-generated scripts.

Expand Down
4 changes: 4 additions & 0 deletions gcloud-java-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@
<mainClass>com.google.cloud.examples.nio.Stat</mainClass>
<name>Stat</name>
</program>
<program>
<mainClass>com.google.cloud.examples.nio.CountBytes</mainClass>
<name>CountBytes</name>
</program>
<program>
<mainClass>
com.google.cloud.examples.resourcemanager.ResourceManagerExample
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.google.cloud.examples.nio;

This comment was marked as spam.

This comment was marked as spam.


import com.google.common.base.Stopwatch;

import java.io.IOException;
import java.net.URI;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.concurrent.TimeUnit;

/**
* CountBytes will read through the whole file given as input.
*
* <p>It's meant for testing that NIO correctly saturates your network link.

This comment was marked as spam.

This comment was marked as spam.

*
* <p>See the README for compilation instructions. Run this code with
* {@code target/appassembler/bin/CountBytes <file>}
*/
public class CountBytes {

/**
* See the class documentation.
*/
public static void main(String[] args) throws IOException {
if (args.length == 0 || args[0].equals("--help")) {
help();
return;
}
for (String a : args) {
countFile(a);
}
}

/**
* Print the length of the indicated file.
*
* <p>This uses the normal Java NIO Api, so it can take advantage of any installed
* NIO Filesystem provider without any extra effort.
*/
private static void countFile(String fname) {
// large buffers pay off
final int bufSize = 50 * 1024 * 1024;
try {
Path path = Paths.get(new URI(fname));
long size = Files.size(path);
System.out.println(fname + ": " + size + " bytes.");
ByteBuffer buf = ByteBuffer.allocate(bufSize);
System.out.println("Reading the whole file...");
Stopwatch sw = Stopwatch.createStarted();
SeekableByteChannel chan = Files.newByteChannel(path);
long total = 0;
int readCalls = 0;
for (int read = 1; read > 0; ) {
readCalls ++;

This comment was marked as spam.

This comment was marked as spam.

read = chan.read(buf);
buf.flip();
total += read;
}
long elapsed = sw.elapsed(TimeUnit.SECONDS);
System.out.println("Read all " + total + " bytes in " + elapsed + "s. (" + readCalls +" calls to chan.read)");

This comment was marked as spam.

This comment was marked as spam.

if (total != size) {
System.out.println("Wait, this doesn't match! We saw " + total + " bytes, yet the file size is listed at " + size + " bytes.");

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

}
} catch (Exception ex) {
System.out.println(fname + ": " + ex.toString());
}
}

private static void help() {
String[] help =
{"The argument is a <path>",
"and we show the length of that file.",

This comment was marked as spam.

This comment was marked as spam.

};
for (String s : help) {
System.out.println(s);
}
}

This comment was marked as spam.

}