-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
mziccard
merged 4 commits into
googleapis:gcs-nio
from
jean-philippe-martin:nio_countbytes
May 4, 2016
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
99 changes: 99 additions & 0 deletions
99
gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/CountBytes.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,99 @@ | ||
/* | ||
* 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; | ||
|
||
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 doesn't read too slowly. | ||
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong. |
||
* | ||
* <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; | ||
while (chan.read(buf) > 0) { | ||
readCalls++; | ||
total += buf.position(); | ||
buf.flip(); | ||
} | ||
readCalls++; // We must count the last call | ||
long elapsed = sw.elapsed(TimeUnit.SECONDS); | ||
System.out.println("Read all " + total + " bytes in " + elapsed + "s. " + | ||
"(" + readCalls +" calls to chan.read)"); | ||
if (total != size) { | ||
System.out.println("Wait, this doesn't match! We saw " + total + " bytes, " + | ||
"yet the file size is listed at " + size + " bytes."); | ||
} | ||
} 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." | ||
}; | ||
for (String s : help) { | ||
System.out.println(s); | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
gcloud-java-examples/src/main/java/com/google/cloud/examples/nio/Stat.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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.