List of usage examples for java.nio.file DirectoryStream close
public void close() throws IOException;
From source file:edu.harvard.hul.ois.drs.pdfaconvert.PdfaConvert.java
public static void main(String[] args) throws IOException { if (logger == null) { System.out.println("About to initialize Log4j"); logger = LogManager.getLogger(); System.out.println("Finished initializing Log4j"); }//from www .j av a 2s . c om logger.debug("Entering main()"); // WIP: the following command line code was pulled from FITS Options options = new Options(); Option inputFileOption = new Option(PARAM_I, true, "input file"); options.addOption(inputFileOption); options.addOption(PARAM_V, false, "print version information"); options.addOption(PARAM_H, false, "help information"); options.addOption(PARAM_O, true, "output sub-directory"); CommandLineParser parser = new DefaultParser(); CommandLine cmd = null; try { cmd = parser.parse(options, args, true); } catch (ParseException e) { System.err.println(e.getMessage()); System.exit(1); } // print version info if (cmd.hasOption(PARAM_V)) { if (StringUtils.isEmpty(applicationVersion)) { applicationVersion = "<not set>"; System.exit(1); } System.out.println("Version: " + applicationVersion); System.exit(0); } // print help info if (cmd.hasOption(PARAM_H)) { displayHelp(); System.exit(0); } // input parameter if (cmd.hasOption(PARAM_I)) { String input = cmd.getOptionValue(PARAM_I); boolean hasValue = cmd.hasOption(PARAM_I); logger.debug("Has option {} value: [{}]", PARAM_I, hasValue); String paramVal = cmd.getOptionValue(PARAM_I); logger.debug("value of option: [{}] ****", paramVal); File inputFile = new File(input); if (!inputFile.exists()) { logger.warn("{} does not exist or is not readable.", input); System.exit(1); } String subDir = cmd.getOptionValue(PARAM_O); PdfaConvert convert; if (!StringUtils.isEmpty(subDir)) { convert = new PdfaConvert(subDir); } else { convert = new PdfaConvert(); } if (inputFile.isDirectory()) { if (inputFile.listFiles() == null || inputFile.listFiles().length < 1) { logger.warn("Input directory is empty, nothing to process."); System.exit(1); } else { logger.debug("Have directory: [{}] with file count: {}", inputFile.getAbsolutePath(), inputFile.listFiles().length); DirectoryStream<Path> dirStream = null; dirStream = Files.newDirectoryStream(inputFile.toPath()); for (Path filePath : dirStream) { logger.debug("Have file name: {}", filePath.toString()); // Note: only handling files, not recursively going into sub-directories if (filePath.toFile().isFile()) { // Catch possible exception for each file so can handle other files in directory. try { convert.examine(filePath.toFile()); } catch (Exception e) { logger.error("Problem processing file: {} -- Error message: {}", filePath.getFileName(), e.getMessage()); } } else { logger.warn("Not a file so not processing: {}", filePath.toString()); // could be a directory but not recursing } } dirStream.close(); } } else { logger.debug("About to process file: {}", inputFile.getPath()); try { convert.examine(inputFile); } catch (Exception e) { logger.error("Problem processing file: {} -- Error message: {}", inputFile.getName(), e.getMessage()); logger.debug("Problem processing file: {} -- Error message: {}", inputFile.getName(), e.getMessage(), e); } } } else { System.err.println("Missing required option: " + PARAM_I); displayHelp(); System.exit(-1); } System.exit(0); }
From source file:com.nwn.NwnFileHandler.java
/** * Get names of files in given directory * @param dir Path to directory for parsing * @return String of file names in directory *///from w w w. ja v a2 s .co m public static ArrayList<String> getFileNamesInDirectory(Path dir) throws NoSuchFileException, IOException { ArrayList<String> fileNamesInDir = new ArrayList<String>(); DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir); for (Path file : dirStream) { fileNamesInDir.add(file.getFileName().toString()); } dirStream.close(); return fileNamesInDir; }
From source file:ch.admin.suis.msghandler.checker.StatusCheckerSessionImpl.java
private void closeStream(DirectoryStream stream) { try {// w ww . j a va 2s . co m stream.close(); } catch (IOException e) { LOG.error("Unable to close directory stream. " + e); } }
From source file:com.oneops.util.SearchSenderTest.java
private boolean isRetryDirectoryEmpty() { DirectoryStream<Path> dirStream = null; try {//from w w w. j a v a 2s .c o m Path retryPath = FileSystems.getDefault().getPath(retryDir); dirStream = java.nio.file.Files.newDirectoryStream(retryPath); return !dirStream.iterator().hasNext(); } catch (IOException e) { e.printStackTrace(); } finally { try { dirStream.close(); } catch (IOException e) { e.printStackTrace(); } } return true; }
From source file:ch.admin.suis.msghandler.common.MessageCollection.java
private List<File> directoryStreamToListOfFiles(DirectoryStream<Path> stream, long limit) { int processed = 0; List<File> envFiles = new ArrayList<>(); for (Path path : stream) { processed++;// w ww . j a va 2 s.c o m if (processed > limit) { LOG.warn( "This job has reached the maximum it could handle. Due to configuration, this job will be throttled. Configuration currently allows " + Inbox.incomingMessageLimit + " messages."); break; // This allows to continue without breaking stuff } envFiles.add(new File(path.toAbsolutePath().toString())); } try { stream.close(); } catch (IOException e) { LOG.error("Unable to close directory stream. " + e); } return new ArrayList<>(envFiles); }
From source file:de.bbe_consulting.mavento.MagentoInfoMojo.java
@Override public void execute() throws MojoExecutionException, MojoFailureException { initMojo();/*from ww w .j a v a 2 s . c om*/ getLog().info("Scanning: " + magentoPath); getLog().info(""); if (mVersion != null) { getLog().info("Version: Magento " + mVersion.toString()); } // parse sql properties from local.xml final Path localXmlPath = Paths.get(magentoPath + "/app/etc/local.xml"); Document localXml = null; if (Files.exists(localXmlPath)) { localXml = MagentoXmlUtil.readXmlFile(localXmlPath.toAbsolutePath().toString()); } else { throw new MojoExecutionException( "Could not read or parse /app/etc/local.xml." + " Use -DmagentoPath= to set Magento dir."); } final Map<String, String> dbSettings = MagentoXmlUtil.getDbValues(localXml); final String jdbcUrl = MagentoSqlUtil.getJdbcUrl(dbSettings.get("host"), dbSettings.get("port"), dbSettings.get("dbname")); // fetch installdate final String magentoInstallDate = MagentoXmlUtil.getMagentoInstallData(localXml); getLog().info("Installed: " + magentoInstallDate); getLog().info(""); // read baseUrl MagentoCoreConfig baseUrl = null; try { baseUrl = new MagentoCoreConfig("web/unsecure/base_url"); } catch (Exception e) { throw new MojoExecutionException("Error creating config entry. " + e.getMessage(), e); } String sqlError = SQL_CONNECTION_VALID; try { baseUrl = MagentoSqlUtil.getCoreConfigData(baseUrl, dbSettings.get("user"), dbSettings.get("password"), jdbcUrl, getLog()); getLog().info("URL: " + baseUrl.getValue()); getLog().info(""); } catch (MojoExecutionException e) { sqlError = e.getMessage(); } getLog().info("Database: " + dbSettings.get("dbname") + " via " + dbSettings.get("user") + "@" + dbSettings.get("host") + ":" + dbSettings.get("port")); getLog().info("Connection: " + sqlError); getLog().info(""); if (!skipSize) { MutableLong rootSizeTotal = new MutableLong(); try { FileSizeVisitor fs = new FileSizeVisitor(rootSizeTotal); Files.walkFileTree(Paths.get(magentoPath), fs); } catch (IOException e) { throw new MojoExecutionException(e.getMessage(), e); } getLog().info( "Magento files total: " + String.format("%,8d", rootSizeTotal.toLong()).trim() + " bytes"); if (SQL_CONNECTION_VALID.equals(sqlError)) { try { final Map<String, Integer> dbDetails = MagentoSqlUtil.getDbSize(dbSettings.get("dbname"), dbSettings.get("user"), dbSettings.get("password"), jdbcUrl, getLog()); final List<MysqlTable> logTableDetails = MagentoSqlUtil.getLogTablesSize( dbSettings.get("dbname"), dbSettings.get("user"), dbSettings.get("password"), jdbcUrl, getLog()); getLog().info("Database total: " + String.format("%,8d", dbDetails.get("totalRows")).trim() + " entries / " + String.format("%,8d", dbDetails.get("totalSize")).trim() + "mb"); int logSizeTotal = 0; int logRowsTotal = 0; for (MysqlTable t : logTableDetails) { logSizeTotal += t.getTableSizeInMb(); logRowsTotal += t.getTableRows(); if (showDetails) { getLog().info(" " + t.getTableName() + ": " + t.getFormatedTableEntries() + " entries / " + t.getFormatedTableSizeInMb() + "mb"); } } getLog().info("Log tables total: " + String.format("%,8d", logRowsTotal).trim() + " entries / " + String.format("%,8d", logSizeTotal).trim() + "mb"); } catch (MojoExecutionException e) { getLog().info("Error: " + e.getMessage()); } } getLog().info(""); } // parse modules final Path modulesXmlPath = Paths.get(magentoPath + "/app/etc/modules"); if (!Files.exists(modulesXmlPath)) { throw new MojoExecutionException("Could not find /app/etc/modules directory."); } DirectoryStream<Path> files = null; final ArrayList<MagentoModule> localModules = new ArrayList<MagentoModule>(); final ArrayList<MagentoModule> communityModules = new ArrayList<MagentoModule>(); try { files = Files.newDirectoryStream(modulesXmlPath); for (Path path : files) { if (!path.getFileName().toString().startsWith("Mage")) { MagentoModule m = new MagentoModule(path); if (m.getCodePool().equals("local")) { localModules.add(m); } else { communityModules.add(m); } } } } catch (IOException e) { throw new MojoExecutionException("Could not read modules directory. " + e.getMessage(), e); } finally { try { files.close(); } catch (IOException e) { throw new MojoExecutionException("Error closing directory stream. " + e.getMessage(), e); } } // print module sorted module list final MagentoModuleComperator mmc = new MagentoModuleComperator(); Collections.sort(localModules, mmc); Collections.sort(communityModules, mmc); getLog().info("Installed modules in.."); getLog().info("..local: "); for (MagentoModule m : localModules) { getLog().info(m.getNamespace() + "_" + m.getName() + " version: " + m.getVersion() + " active: " + m.isActive()); } if (localModules.size() == 0) { getLog().info("--none--"); } getLog().info(""); getLog().info("..community: "); for (MagentoModule m : communityModules) { getLog().info(m.getNamespace() + "_" + m.getName() + " version: " + m.getVersion() + " active: " + m.isActive()); } if (communityModules.size() == 0) { getLog().info("--none--"); } getLog().info(""); // check local overlays for content getLog().info("Overlay status.."); int fileCount = -1; final File localMage = new File(magentoPath + "/app/code/local/Mage"); if (localMage.exists()) { fileCount = localMage.list().length; if (fileCount > 0) { getLog().info("local/Mage: " + localMage.list().length + " file(s)"); } } final File localVarien = new File(magentoPath + "/app/code/local/Varien"); if (localVarien.exists()) { fileCount = localVarien.list().length; if (fileCount > 0) { getLog().info("local/Varien: " + localVarien.list().length + " file(s)"); } } if (fileCount == -1) { getLog().info("..not in use."); } }
From source file:ch.admin.suis.msghandler.sender.SenderSessionImpl.java
private void prepareOutbox(Outbox outbox, LogService logService, HashMap<Integer, Message> pairs, String defaultSenderId, ProtocolService protocolService) { // the outbox folder handleSigning(outbox.getSigningOutboxes(), outbox.getDirectory()); // check for the files over there DirectoryStream<Path> files = FileUtils.listFiles(outbox.getDirectory(), FileFilters.ALL_FILES_FILTER_PATH); ArrayList<File> corruptedFiles = new ArrayList<>(); File corruptedDir = new File(new File(this.getContext().getClientConfiguration().getWorkingDir()), CORRUPTED_DIR);//from w ww . j a v a2s .c om for (Path path : files) { // Converting code to keep code compatible. File file = path.toFile(); // determine the recipient ID (only via the script) final String participantId = outbox.getParticipantIdResolver().resolve(file.getAbsolutePath()); if (StringUtils.isEmpty(participantId)) { LOG.fatal("cannot determine recipient ID for the file " + file.getAbsolutePath()); corruptedFiles.add(file); continue; // but try another file anyway } // can we send this message? try { if (!logService.setSending(Mode.MH, participantId, file.getName())) { LOG.info(MessageFormat.format("file {0} is already sent to the recipient {1} ", file.getName(), participantId)); // try another file continue; } } catch (LogServiceException e) { LOG.fatal("internal problem with the log service: " + e); // this is a fatal problem caused by some underlying problem Thread.currentThread().interrupt(); } // determine the message type final MessageType type = outbox.getType(); // determine the sender ID String senderId = outbox.getSedexId(); if (StringUtils.isBlank(senderId)) { // in vain, take default senderId = defaultSenderId; // as in the setup LOG.debug("using the sedex ID to determine the sender ID for the file " + file.getAbsolutePath()); } else { LOG.debug("using the sender's sedex ID attribute to determine the sender ID for the file " + file.getAbsolutePath()); } int hashCode = new HashCodeBuilder().append(participantId).append(type).append(senderId).toHashCode(); Message message = pairs.get(hashCode); if (null == message) { message = new Message(); pairs.put(hashCode, message); // set the message message.setMessageType(type); // message type message.addRecipientId(participantId);// the recipient message.setMessageId(UUID.randomUUID().toString()); // generate the message ID message.setMessageClass("0"); // initial message message.setSenderId(senderId); message.setMessageDate(ISO8601Utils.format(new Date())); // TODO how should we set the event time while creating a message? message.setEventDate(ISO8601Utils.format(new Date())); } // log the event protocolService.logPreparing(file.getAbsolutePath(), message); // add this file to the message message.addFile(file); LOG.info(MessageFormat.format("message ID {1}: preparing to send the file {0}", file, message.getMessageId())); } for (File file : corruptedFiles) { LOG.info("Moving file to corrupted folder (" + corruptedDir.toPath() + ")."); try { FileUtils.moveToDirectory(file, corruptedDir); } catch (IOException e) { LOG.error(e); } } try { files.close(); } catch (IOException e) { LOG.error("Unable to close stream directory. " + e); } }
From source file:org.apache.reef.runtime.mesos.driver.REEFScheduler.java
private String getReefTarUri(final String jobIdentifier) { try {//from ww w .j a v a2 s .c o m // Create REEF_TAR final FileOutputStream fileOutputStream = new FileOutputStream(REEF_TAR); final TarArchiveOutputStream tarArchiveOutputStream = new TarArchiveOutputStream( new GZIPOutputStream(fileOutputStream)); final File globalFolder = new File(this.fileNames.getGlobalFolderPath()); final DirectoryStream<Path> directoryStream = Files.newDirectoryStream(globalFolder.toPath()); for (final Path path : directoryStream) { tarArchiveOutputStream.putArchiveEntry( new TarArchiveEntry(path.toFile(), globalFolder + "/" + path.getFileName())); final BufferedInputStream bufferedInputStream = new BufferedInputStream( new FileInputStream(path.toFile())); IOUtils.copy(bufferedInputStream, tarArchiveOutputStream); bufferedInputStream.close(); tarArchiveOutputStream.closeArchiveEntry(); } directoryStream.close(); tarArchiveOutputStream.close(); fileOutputStream.close(); // Upload REEF_TAR to HDFS final FileSystem fileSystem = FileSystem.get(new Configuration()); final org.apache.hadoop.fs.Path src = new org.apache.hadoop.fs.Path(REEF_TAR); final String reefTarUriValue = fileSystem.getUri().toString() + this.jobSubmissionDirectoryPrefix + "/" + jobIdentifier + "/" + REEF_TAR; final org.apache.hadoop.fs.Path dst = new org.apache.hadoop.fs.Path(reefTarUriValue); fileSystem.copyFromLocalFile(src, dst); return reefTarUriValue; } catch (final IOException e) { throw new RuntimeException(e); } }
From source file:org.bigbluebutton.api.RecordingService.java
private static List<File> getDirectories(String path) { List<File> files = new ArrayList<File>(); try {/*from w w w. j av a2 s . c om*/ DirectoryStream<Path> stream = Files.newDirectoryStream(FileSystems.getDefault().getPath(path)); Iterator<Path> iter = stream.iterator(); while (iter.hasNext()) { Path next = iter.next(); files.add(next.toFile()); } stream.close(); } catch (IOException e) { e.printStackTrace(); } return files; }
From source file:org.carcv.impl.core.io.FFMPEGVideoHandlerIT.java
/** * Test method for {@link org.carcv.impl.core.io.FFMPEGVideoHandler#copyCarImagesToDir(java.util.List, java.nio.file.Path)}. * * @throws IOException/* w ww .j av a 2s . com*/ */ @Test public void testCopyCarImagesToDir() throws IOException { FFMPEGVideoHandler.copyCarImagesToDir(entry.getCarImages(), videoDir); DirectoryStream<Path> dirStream = Files.newDirectoryStream(videoDir); ArrayList<Path> paths = new ArrayList<>(); for (Path p : dirStream) { paths.add(p); } dirStream.close(); assertEquals(2, paths.size()); Collections.sort(paths, new Comparator<Path>() { @Override public int compare(Path o1, Path o2) { return new CompareToBuilder().append(o1.getFileName().toString(), o2.getFileName().toString()) .toComparison(); } }); for (int i = 0; i < paths.size(); i++) { assertTrue(paths.get(i).getFileName().toString().startsWith(i + "")); } }