use of java.nio.file.DirectoryStream in project gerrit by GerritCodeReview.
the class StaleLibraryRemover method remove.
public void remove(String pattern) {
if (!Strings.isNullOrEmpty(pattern)) {
DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) {
return entry.getFileName().toString().matches("^" + pattern + "$");
}
};
try (DirectoryStream<Path> paths = Files.newDirectoryStream(lib_dir, filter)) {
for (Path p : paths) {
String old = p.getFileName().toString();
String bak = "." + old + ".backup";
Path dest = p.resolveSibling(bak);
if (Files.exists(dest)) {
ui.message("WARNING: not renaming %s to %s: already exists\n", old, bak);
continue;
}
ui.message("Renaming %s to %s\n", old, bak);
try {
Files.move(p, dest);
} catch (IOException e) {
throw new Die("cannot rename " + old, e);
}
}
} catch (IOException e) {
throw new Die("cannot remove stale library versions", e);
}
}
}
use of java.nio.file.DirectoryStream in project jdk8u_jdk by JetBrains.
the class SeedGenerator method getSystemEntropy.
/**
* Retrieve some system information, hashed.
*/
static byte[] getSystemEntropy() {
byte[] ba;
final MessageDigest md;
try {
md = MessageDigest.getInstance("SHA");
} catch (NoSuchAlgorithmException nsae) {
throw new InternalError("internal error: SHA-1 not available.", nsae);
}
// The current time in millis
byte b = (byte) System.currentTimeMillis();
md.update(b);
java.security.AccessController.doPrivileged(new java.security.PrivilegedAction<Void>() {
@Override
public Void run() {
try {
// System properties can change from machine to machine
String s;
Properties p = System.getProperties();
Enumeration<?> e = p.propertyNames();
while (e.hasMoreElements()) {
s = (String) e.nextElement();
md.update(s.getBytes());
md.update(p.getProperty(s).getBytes());
}
// Include network adapter names (and a Mac address)
addNetworkAdapterInfo(md);
// The temporary dir
File f = new File(p.getProperty("java.io.tmpdir"));
int count = 0;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(f.toPath())) {
// We use a Random object to choose what file names
// should be used. Otherwise on a machine with too
// many files, the same first 1024 files always get
// used. Any, We make sure the first 512 files are
// always used.
Random r = new Random();
for (Path entry : stream) {
if (count < 512 || r.nextBoolean()) {
md.update(entry.getFileName().toString().getBytes());
}
if (count++ > 1024) {
break;
}
}
}
} catch (Exception ex) {
md.update((byte) ex.hashCode());
}
// get Runtime memory stats
Runtime rt = Runtime.getRuntime();
byte[] memBytes = longToByteArray(rt.totalMemory());
md.update(memBytes, 0, memBytes.length);
memBytes = longToByteArray(rt.freeMemory());
md.update(memBytes, 0, memBytes.length);
return null;
}
});
return md.digest();
}
use of java.nio.file.DirectoryStream in project jena by apache.
the class FusekiConfig method readConfigurationDirectory.
// ---- Directory of assemblers
/** Read service descriptions in the given directory */
public static List<DataAccessPoint> readConfigurationDirectory(String dir) {
Path pDir = Paths.get(dir).normalize();
File dirFile = pDir.toFile();
if (!dirFile.exists()) {
log.warn("Not found: directory for assembler files for services: '" + dir + "'");
return Collections.emptyList();
}
if (!dirFile.isDirectory()) {
log.warn("Not a directory: '" + dir + "'");
return Collections.emptyList();
}
// Files that are not hidden.
DirectoryStream.Filter<Path> filter = (entry) -> {
File f = entry.toFile();
final Lang lang = filenameToLang(f.getName());
return !f.isHidden() && f.isFile() && lang != null && isRegistered(lang);
};
List<DataAccessPoint> dataServiceRef = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(pDir, filter)) {
for (Path p : stream) {
DatasetDescriptionRegistry dsDescMap = FusekiServer.registryForBuild();
String fn = IRILib.filenameToIRI(p.toString());
log.info("Load configuration: " + fn);
Model m = readAssemblerFile(fn);
readConfiguration(m, dsDescMap, dataServiceRef);
}
} catch (IOException ex) {
log.warn("IOException:" + ex.getMessage(), ex);
}
return dataServiceRef;
}
use of java.nio.file.DirectoryStream in project karaf by apache.
the class ArchiveMojo method archive.
public //ArchiverException,
File archive(//ArchiverException,
File source, //ArchiverException,
File dest, //ArchiverException,
Artifact artifact) throws IOException {
String serverName = null;
if (targetFile != null) {
serverName = targetFile.getName();
} else {
serverName = artifact.getArtifactId() + "-" + artifact.getVersion();
}
dest = new File(dest, serverName + "." + artifact.getType());
String prefix = "";
if (usePathPrefix) {
prefix = pathPrefix.trim();
if (prefix.length() > 0 && !prefix.endsWith("/")) {
prefix += "/";
}
}
if ("tar.gz".equals(artifact.getType())) {
try (OutputStream fOut = Files.newOutputStream(dest.toPath());
OutputStream bOut = new BufferedOutputStream(fOut);
OutputStream gzOut = new GzipCompressorOutputStream(bOut);
TarArchiveOutputStream tOut = new TarArchiveOutputStream(gzOut);
DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())) {
tOut.setLongFileMode(TarArchiveOutputStream.LONGFILE_POSIX);
tOut.setBigNumberMode(TarArchiveOutputStream.BIGNUMBER_POSIX);
for (Path child : children) {
addFileToTarGz(tOut, child, prefix);
}
}
} else if ("zip".equals(artifact.getType())) {
try (OutputStream fOut = Files.newOutputStream(dest.toPath());
OutputStream bOut = new BufferedOutputStream(fOut);
ZipArchiveOutputStream tOut = new ZipArchiveOutputStream(bOut);
DirectoryStream<Path> children = Files.newDirectoryStream(source.toPath())) {
for (Path child : children) {
addFileToZip(tOut, child, prefix);
}
}
} else {
throw new IllegalArgumentException("Unknown target type: " + artifact.getType());
}
return dest;
}
use of java.nio.file.DirectoryStream in project chilo-producer by cccties.
the class Epub3Maker method main.
public static void main(String[] args) {
/*
* 引数を処理
*/
String homeDir = null;
String configFile = null;
String seriesDir = null;
String template = "basic";
String inputPath = "./";
String outputPath = "./";
String outputName = "";
boolean doWeko = false;
for (int ai = 0; ai < args.length && args[ai].startsWith("-"); ai++) {
if (args[ai].equals("-home")) {
if (ai < args.length - 1) {
homeDir = args[++ai];
continue;
} else {
usage();
}
} else if (args[ai].equals("-config")) {
if (ai < args.length - 1) {
configFile = args[++ai];
continue;
} else {
usage();
}
} else if (args[ai].equals("-series")) {
if (ai < args.length - 1) {
seriesDir = args[++ai];
continue;
} else {
usage();
}
} else if (args[ai].equals("-template")) {
if (ai < args.length - 1) {
template = args[++ai];
continue;
} else {
usage();
}
} else if (args[ai].equals("-input-path")) {
if (ai < args.length - 1) {
inputPath = args[++ai];
} else {
usage();
}
} else if (args[ai].equals("-output-path")) {
if (ai < args.length - 1) {
outputPath = args[++ai];
} else {
usage();
}
} else if (args[ai].equals("-output-name")) {
if (ai < args.length - 1) {
outputName = args[++ai];
} else {
usage();
}
} else if (args[ai].equals("-weko")) {
doWeko = true;
}
}
Velocity.addProperty(Log4JLogChute.RUNTIME_LOG_LOG4J_LOGGER, "velocity");
try {
/*
* 設定値を読込
*/
new Config(configFile, homeDir);
Config.setTemplate(template);
Config.setInputPath(inputPath);
Config.setOutputPath(outputPath);
Config.setOutputName(outputName);
ArrayList<Path> seriesList = new ArrayList<Path>();
Path seriesBasePath = Paths.get(Config.getSeriesBaseDir());
if (seriesDir == null) {
/*
* series dir 一覧を取得
*/
DirectoryStream<Path> stream = Files.newDirectoryStream(seriesBasePath, new DirectoryStream.Filter<Path>() {
@Override
public boolean accept(Path entry) throws IOException {
return Files.isDirectory(entry);
}
});
for (Path p : stream) {
seriesList.add(p);
}
} else if (seriesDir.endsWith(".xlsx")) {
seriesList.add(Paths.get(seriesDir));
} else {
seriesList.add(seriesBasePath.resolve(seriesDir));
}
Process proc = new Process();
for (Path c : seriesList) {
proc.process(c, doWeko);
}
} catch (Exception e) {
log.fatal(e.getMessage());
e.printStackTrace();
}
}
Aggregations