thredds.filesystem.ControllerOS7 Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 1998-2018 University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package thredds.filesystem;
import thredds.inventory.CollectionConfig;
import thredds.inventory.MController;
import thredds.inventory.MFile;
import javax.annotation.concurrent.ThreadSafe;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Iterator;
/**
* Use Java 7 NIO for scanning the file system
*
* @author caron
* @since 11/8/13
*/
@ThreadSafe
public class ControllerOS7 implements MController {
private static org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(ControllerOS7.class);
////////////////////////////////////////
@Override
public Iterator getInventoryAll(CollectionConfig mc, boolean recheck) {
return null;
}
@Override
public Iterator getInventoryTop(CollectionConfig mc, boolean recheck) throws IOException {
String path = mc.getDirectoryName();
if (path.startsWith("file:")) {
path = path.substring(5);
}
Path cd = Paths.get(path);
if (!Files.exists(cd))
return null;
return new MFileIterator(cd, new CollectionFilter(mc)); // removes subdirs
}
public Iterator getSubdirs(CollectionConfig mc, boolean recheck) {
return null;
}
public void close() {} // NOOP
////////////////////////////////////////////////////////////
private static class CollectionFilter implements DirectoryStream.Filter {
CollectionConfig mc; // LOOK not used yet
private CollectionFilter(CollectionConfig mc) {
this.mc = mc;
}
@Override
public boolean accept(Path entry) {
return !entry.endsWith(".gbx9") && !entry.endsWith(".ncx");
}
}
// returns everything in the current directory
private static class MFileIterator implements Iterator {
Iterator dirStream;
MFileIterator(Path dir, DirectoryStream.Filter filter) throws IOException {
if (filter != null)
dirStream = Files.newDirectoryStream(dir, filter).iterator();
else
dirStream = Files.newDirectoryStream(dir).iterator();
}
public boolean hasNext() {
return dirStream.hasNext();
}
public MFile next() {
try {
return new MFileOS7(dirStream.next());
} catch (IOException e) {
e.printStackTrace(); // LOOK we should pass this exception up
throw new RuntimeException(e);
}
}
public void remove() {
throw new UnsupportedOperationException();
}
}
//////////////////////////////////////////////////////////////////
// playing around with NIO
public static class PrintFiles extends SimpleFileVisitor {
private int countFiles;
private int countDirs;
private int countOther;
private int countSyms;
long start = System.currentTimeMillis();
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
if (attr.isSymbolicLink()) {
countSyms++;
} else if (attr.isRegularFile()) {
countFiles++;
} else {
countOther++;
}
if (countFiles % 10000 == 0) {
double took = (System.currentTimeMillis() - start);
double rate = countFiles / took;
double drate = countDirs / took;
}
return FileVisitResult.CONTINUE;
}
// Print each directory visited.
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
countDirs++;
return FileVisitResult.CONTINUE;
}
// If there is some error accessing the file, let the user know.
// If you don't override this method and an error occurs, an IOException is thrown.
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
return FileVisitResult.CONTINUE;
}
@Override
public String toString() {
String sb = "PrintFiles{" + "countFiles=" + countFiles + ", countDirs=" + countDirs + ", countOther=" + countOther
+ ", countSyms=" + countSyms + '}';
return sb;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy