Last active
August 29, 2015 14:06
-
-
Save louiszuckerman/65e01d6b2f970726ff46 to your computer and use it in GitHub Desktop.
A naive implementation of du -s in Java using NIO.2
This file contains hidden or 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
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
import java.nio.file.*; | |
public class Du { | |
public static final String PREFIX = | |
"gluster://172.31.31.31:foo/usr"; | |
public static long total = 0; | |
public static void main(String[] args) throws URISyntaxException, IOException { | |
Path path = Paths.get(new URI(PREFIX)); | |
long startTime = System.nanoTime(); | |
scanDir(path); | |
long endTime = System.nanoTime(); | |
double time = (endTime - startTime) / 1000000.0; | |
System.out.println("TOTAL: " + total + " RUNTIME: " + time); | |
} | |
private static void scanDir(Path path) throws IOException { | |
total += Files.size(path); | |
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path); | |
for (Path p : directoryStream) { | |
if (p.endsWith("/.") || p.endsWith("/..") || Files.isSymbolicLink(p)) { | |
continue; | |
} | |
if (Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS)) { | |
scanDir(p); | |
} else { | |
try { | |
long size = Files.size(p); | |
total += size; | |
} catch (Exception e) { | |
System.out.println("EXCEPTION: "+p); | |
e.printStackTrace(); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment