|
| 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 | +} |
0 commit comments