List of usage examples for java.io FileNotFoundException toString
public String toString()
From source file:io.github.retz.mesosc.MesosHTTPFetcher.java
private static Pair<Integer, String> fetchHTTP(String addr, int retry) throws FileNotFoundException, IOException { block: try (UrlConnector conn = new UrlConnector(addr, "GET", true)) { int statusCode = conn.getResponseCode(); String message = conn.getResponseMessage(); if (LOG.isDebugEnabled()) { LOG.debug("{} {} for {}", statusCode, message, addr); }// w ww .ja v a 2 s . co m if (statusCode != 200) { if (statusCode < 200) { break block; // retry } LOG.warn("Non-200 status {} returned from Mesos '{}' for GET {}", statusCode, message, addr); if (statusCode < 300) { return new Pair<>(statusCode, ""); // Mostly 204; success } else if (statusCode < 400) { // TODO: Mesos master failover return new Pair<>(statusCode, message); } else if (statusCode == 404) { throw new FileNotFoundException(addr); } else { return new Pair<>(statusCode, message); } } try (InputStream in = conn.getInputStream(); ByteArrayOutputStream out = new ByteArrayOutputStream()) { long toRead = conn.getContentLength(); if (toRead < 0) { LOG.warn("Content length is not known ({}) on getting {}", toRead, addr); IOUtils.copy(in, out); return new Pair<>(statusCode, out.toString(UTF_8.toString())); } long read = IOUtils.copyLarge(in, out, 0, toRead); if (read < toRead) { LOG.warn("Unexpected EOF at {}/{} getting {}", read, toRead, addr); break block; } if (LOG.isDebugEnabled()) { LOG.debug("Fetched {} bytes from {}", read, addr); } return new Pair<>(statusCode, out.toString(UTF_8.toString())); } catch (FileNotFoundException e) { throw e; } catch (IOException e) { // Somehow this happens even HTTP was correct if (LOG.isDebugEnabled()) { LOG.debug("Cannot fetch file {}: {}", addr, e.toString()); } // Just retry until your stack get stuck; thanks to SO:33340848 // and to that crappy HttpURLConnection if (retry < 0) { LOG.error("Retry failed. Last error was: {}", e.toString()); throw e; } break block; // retry } } return fetchHTTP(addr, retry - 1); }
From source file:com.cloudera.impala.analysis.CreateTableLikeFileStmt.java
/** * Reads the first block from the given HDFS file and returns the Parquet schema. * Throws Analysis exception for any failure, such as failing to read the file * or failing to parse the contents./* w w w. ja va 2s. co m*/ */ private static parquet.schema.MessageType loadParquetSchema(Path pathToFile) throws AnalysisException { ParquetMetadata readFooter = null; try { readFooter = ParquetFileReader.readFooter(FileSystemUtil.getConfiguration(), pathToFile); } catch (FileNotFoundException e) { throw new AnalysisException("File not found: " + e); } catch (IOException e) { throw new AnalysisException("Failed to open file as a parquet file: " + e); } catch (RuntimeException e) { // Parquet throws a generic RuntimeException when reading a non-parquet file if (e.toString().contains("is not a Parquet file")) { throw new AnalysisException("File is not a parquet file: " + pathToFile); } // otherwise, who knows what we caught, throw it back up throw e; } return readFooter.getFileMetaData().getSchema(); }
From source file:fr.gouv.culture.vitam.digest.DigestCompute.java
public static int createDigest(File src, File dst, File ftar, File fglobal, boolean oneDigestPerFile, List<File> filesToScan) { try {/*ww w. j a va 2 s . c o m*/ Element global = null; Document globalDoc = null; if (fglobal != null) { global = XmlDom.factory.createElement("digests"); global.addAttribute("source", src.getAbsolutePath()); globalDoc = XmlDom.factory.createDocument(global); } OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding(StaticValues.CURRENT_OUTPUT_ENCODING); XMLWriter writer = new XMLWriter(format); int error = 0; int currank = 0; for (File file : filesToScan) { currank++; Element result = DigestCompute.checkDigest(StaticValues.config, src, file); if (result.selectSingleNode(".[@status='ok']") == null) { System.err.println(StaticValues.LBL.error_error.get() + StaticValues.LBL.error_computedigest.get() + StaticValues.getSubPath(file, src)); error++; } else if (oneDigestPerFile) { Element rootElement = XmlDom.factory.createElement("digest"); Document unique = XmlDom.factory.createDocument(rootElement); rootElement.add(result); FileOutputStream out = null; String shortname = StaticValues.getSubPath(file, src); String shortnameWithoutFilename = shortname.substring(0, shortname.lastIndexOf(file.getName())); File fdirout = new File(dst, shortnameWithoutFilename); fdirout.mkdirs(); File fout = new File(fdirout, file.getName() + "_digest.xml"); try { out = new FileOutputStream(fout); writer.setOutputStream(out); writer.write(unique); writer.close(); } catch (FileNotFoundException e) { System.err.println( StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString()); error++; } catch (IOException e) { System.err.println( StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString()); if (out != null) { try { out.close(); } catch (IOException e1) { } } error++; } result.detach(); } if (fglobal != null) { global.add(result); } } if (ftar != null) { currank++; Element result = DigestCompute.checkDigest(StaticValues.config, src, ftar); if (result.selectSingleNode(".[@status='ok']") == null) { System.err.println(StaticValues.LBL.error_error.get() + StaticValues.LBL.error_computedigest.get() + ftar.getAbsolutePath()); error++; } else if (oneDigestPerFile) { Element rootElement = XmlDom.factory.createElement("digest"); Document unique = XmlDom.factory.createDocument(rootElement); rootElement.add(result); FileOutputStream out = null; File fout = new File(dst, ftar.getName() + "_tar_digest.xml"); try { out = new FileOutputStream(fout); writer.setOutputStream(out); writer.write(unique); writer.close(); } catch (FileNotFoundException e) { System.err.println( StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString()); error++; } catch (IOException e) { System.err.println( StaticValues.LBL.error_error.get() + fout.getAbsolutePath() + " " + e.toString()); if (out != null) { try { out.close(); } catch (IOException e1) { } } error++; } result.detach(); } if (fglobal != null) { global.add(result); } } if (fglobal != null) { if (error > 0) { global.addAttribute("status", "error"); } else { global.addAttribute("status", "ok"); } XmlDom.addDate(VitamArgument.ONEXML, StaticValues.config, global); FileOutputStream out; try { out = new FileOutputStream(fglobal); writer.setOutputStream(out); writer.write(globalDoc); writer.close(); } catch (FileNotFoundException e) { System.err.println( StaticValues.LBL.error_error.get() + fglobal.getAbsolutePath() + " " + e.toString()); error++; } catch (IOException e) { System.err.println( StaticValues.LBL.error_error.get() + fglobal.getAbsolutePath() + " " + e.toString()); error++; } } if (error > 0) { System.err.println(StaticValues.LBL.error_error.get() + " Digest" + " [ " + currank + (error > 0 ? " (" + StaticValues.LBL.error_error.get() + error + " ) " : "") + " ]"); return -error; } return currank; } catch (UnsupportedEncodingException e) { System.err.println(StaticValues.LBL.error_error.get() + " " + e.toString()); return -1; } }
From source file:org.eclipse.jubula.client.archive.XmlStorage.java
/** * Reads the content of the file and returns it as a string. * @param fileURL The URL of the project to import * @return The file content// w w w .j av a 2 s . c o m * @throws PMReadException If the file couldn't be read (wrong file name, IOException) */ private static String readProjectFile(URL fileURL) throws PMReadException { try { final String encoding = getCharacterEncoding(fileURL); StringBuilder result = readFileContent(fileURL, encoding); String content = checkAndReduceXmlHeader(result); return content; } catch (FileNotFoundException e) { log.debug(Messages.ClassSerializedCouldntFound, e); throw new PMReadException(e.toString(), MessageIDs.E_FILE_NOT_FOUND); } catch (IOException e) { log.debug( Messages.FailedReadingFile + StringConstants.COLON + StringConstants.SPACE + fileURL.getFile()); throw new PMReadException(e.toString(), MessageIDs.E_FILE_IO); } catch (XmlException e) { log.debug(Messages.MalformedXMLData); throw new PMReadException(e.toString(), MessageIDs.E_LOAD_PROJECT); } }
From source file:com.streamsets.pipeline.lib.util.ProtobufTypeUtil.java
/** * Returns a protobuf descriptor instance from the provided descriptor file. * * @param context Stage context used for finding the SDC resources directory * @param protoDescriptorFile Path to descriptor file relative to SDC_RESOURCES * @param messageType The name of the message to decode * @param messageTypeToExtensionMap Map of protobuf extensions required for decoding * @param defaultValueMap Map of default values to use for the message * @return protobuf descriptor instance//from www . ja v a2 s.c o m * @throws StageException */ public static Descriptors.Descriptor getDescriptor(Stage.Context context, String protoDescriptorFile, String messageType, Map<String, Set<Descriptors.FieldDescriptor>> messageTypeToExtensionMap, Map<String, Object> defaultValueMap) throws StageException { File descriptorFileHandle = new File(context.getResourcesDirectory(), protoDescriptorFile); try { FileInputStream fin = new FileInputStream(descriptorFileHandle); DescriptorProtos.FileDescriptorSet set = DescriptorProtos.FileDescriptorSet.parseFrom(fin); // Iterate over all the file descriptor set computed above and cache dependencies and all encountered // file descriptors // this map holds all the dependencies that a given file descriptor has. // This cached map will be looked up while building FileDescriptor instances Map<String, Set<Descriptors.FileDescriptor>> fileDescriptorDependentsMap = new HashMap<>(); // All encountered FileDescriptor instances cached based on their name. Map<String, Descriptors.FileDescriptor> fileDescriptorMap = new HashMap<>(); ProtobufTypeUtil.getAllFileDescriptors(set, fileDescriptorDependentsMap, fileDescriptorMap); // Get the descriptor for the expected message type Descriptors.Descriptor descriptor = ProtobufTypeUtil.getDescriptor(set, fileDescriptorMap, protoDescriptorFile, messageType); // Compute and cache all extensions defined for each message type ProtobufTypeUtil.populateDefaultsAndExtensions(fileDescriptorMap, messageTypeToExtensionMap, defaultValueMap); return descriptor; } catch (FileNotFoundException e) { throw new StageException(Errors.PROTOBUF_06, descriptorFileHandle.getAbsolutePath(), e); } catch (IOException e) { throw new StageException(Errors.PROTOBUF_08, e.toString(), e); } }
From source file:eu.sisob.uma.extractors.adhoc.cvfilesinside.InternalCVFilesExtractor.java
/** * * @param input_file//from w w w . jav a2 s . com * @param results_dir * @param zip_output_file * @param output_file_2 * @param error_sw */ public static void download_files(File input_file, File results_dir, File zip_output_file, File output_file_2, StringWriter error_sw) { CSVReader reader = null; try { reader = new CSVReader(new FileReader(input_file), CSV_SEPARATOR); } catch (FileNotFoundException ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " - " + ex.toString()); return; } int idStaffIdentifier = -1; int idName = -1; int idFirstName = -1; int idLastName = -1; int idInitials = -1; int idUnitOfAssessment_Description = -1; int idInstitutionName = -1; int idWebAddress = -1; int idResearchGroupDescription = -1; int idResearcherWebAddress = -1; int idResearcherWebAddressType = -1; int idResearcherWebAddressExt = -1; int idScoreUrl = -1; String[] nextLine; try { if ((nextLine = reader.readNext()) != null) { for (int i = 0; i < nextLine.length; i++) { String column_name = nextLine[i]; if (column_name.equals(FileFormatConversor.CSV_COL_ID)) idStaffIdentifier = i; else if (column_name.equals(FileFormatConversor.CSV_COL_NAME)) idName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_FIRSTNAME)) idFirstName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_LASTNAME)) idLastName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INITIALS)) idInitials = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SUBJECT)) idUnitOfAssessment_Description = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_NAME)) idInstitutionName = i; else if (column_name.equals(FileFormatConversor.CSV_COL_INSTITUTION_URL)) idWebAddress = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_URL)) idResearcherWebAddress = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_TYPE)) idResearcherWebAddressType = i; else if (column_name.equals(FileFormatConversor.CSV_COL_RESEARCHER_PAGE_EXT)) idResearcherWebAddressExt = i; else if (column_name.equals(FileFormatConversor.CSV_COL_SCORE_URL)) idScoreUrl = i; } } } catch (Exception ex) { String error_msg = "Error reading headers of " + input_file.getName(); Logger.getRootLogger().error(error_msg + " - " + ex.toString()); if (error_sw != null) error_sw.append(error_msg + "\r\n"); return; } try { for (int i = 0; i < nextLine.length; i++) nextLine[i] = "\"" + nextLine[i] + "\""; FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8", false); } catch (IOException ex) { Logger.getLogger("root").error(ex.toString()); } if (idResearcherWebAddress != -1 && idResearcherWebAddressType != -1 && idResearcherWebAddressExt != -1) { Logger.getRootLogger().info("Going to downloads results files"); MessageDigest digest = null; try { digest = MessageDigest.getInstance("MD5"); } catch (NoSuchAlgorithmException ex) { } if (!results_dir.exists()) results_dir.mkdirs(); // File cv_results_dirs = new File(results_dir, "CV"); // if(!cv_results_dirs.exists()) // cv_results_dirs.mkdirs(); // // File pub_results_dirs = new File(results_dir, "PUB"); // if(!pub_results_dirs.exists()) // pub_results_dirs.mkdirs(); // // File homepage_results_dirs = new File(results_dir, "HOMEPAGE"); // if(!homepage_results_dirs.exists()) // homepage_results_dirs.mkdirs(); try { while ((nextLine = reader.readNext()) != null) { String url = nextLine[idResearcherWebAddress]; String ext = nextLine[idResearcherWebAddressExt]; String type = nextLine[idResearcherWebAddressType]; String id = nextLine[idStaffIdentifier]; try { Logger.getRootLogger().info("Downloading " + url); String filename = type + "_" + id + "_" + MD5(url) + "." + ext; File dest = null; // if(type.equals("CV")) // dest = new File(cv_results_dirs, filename); // else if(type.equals("PUB")) // dest = new File(pub_results_dirs, filename); // else if(type.equals("HOMEPAGE")) // dest = new File(homepage_results_dirs, filename); // else dest = new File(results_dir, filename); int max = 10; int num = 0; boolean download_finish = false; while (!download_finish) { try { Thread.sleep(200); URL fetched_url = Downloader.fetchURL(url); FileUtils.copyURLToFile(fetched_url, dest); download_finish = true; } catch (Exception ex) { Logger.getRootLogger().error("Error downloading " + url, ex); num++; } if (max <= num) throw new Exception("Error download time overflowed"); } nextLine[idResearcherWebAddress] = filename; try { for (int i = 0; i < nextLine.length; i++) nextLine[i] = "\"" + nextLine[i] + "\""; FileUtils.write(output_file_2, StringUtil.join(Arrays.asList(nextLine), ";") + "\r\n", "UTF-8", true); } catch (Exception ex) { Logger.getLogger("root").error(ex.toString()); } } catch (Exception ex) { Logger.getRootLogger().error("Error manage downloading " + url, ex); } } } catch (Exception ex) { Logger.getRootLogger().error("Error reading " + input_file.getName() + " " + ex.getMessage()); } ZipFile zf; try { zf = new ZipFile(zip_output_file); zf.createZipFileFromFolder(results_dir, new ZipParameters(), false, 0); } catch (Exception ex) { Logger.getRootLogger().error("Error zipping results from " + input_file.getName()); } } else { Logger.getRootLogger().error("Headers incorrect " + input_file.getName()); } }
From source file:org.eclipse.jubula.client.archive.XmlStorage.java
/** * Save a project as XML to a file or return the serialized project as * string, if fileName == null!/* w ww. ja v a 2 s .c o m*/ * * @param proj * proj to be saved * @param fileName * name for file to save or null, if wanting to get the project * as serialized string * @param includeTestResultSummaries * Whether to save the Test Result Summaries as well. * @param monitor * The progress monitor for this potentially long-running * operation. * @param writeToSystemTempDir * Indicates whether the project has to be written to the system * temp directory * @param listOfProjectFiles * If a project is written into the temp dir then the written * file is added to the list, if the list is not null. * @return the serialized project as string, if fileName == null<br> * or<br> * <b>Returns:</b><br> * null otherwise. Always returns <code>null</code> if the save * operation was canceled. * @throws PMException * if save failed for any reason * @throws ProjectDeletedException * in case of current project is already deleted */ public static String save(IProjectPO proj, String fileName, boolean includeTestResultSummaries, IProgressMonitor monitor, boolean writeToSystemTempDir, List<File> listOfProjectFiles) throws ProjectDeletedException, PMException { monitor.beginTask(Messages.XmlStorageSavingProject, getWorkToSave(proj)); Validate.notNull(proj); FileOutputStream fOut = null; try { String xml = buildXmlHeader() + XmlStorage.save(proj, includeTestResultSummaries, monitor); if (fileName == null) { return xml; } if (writeToSystemTempDir) { File fileInTempDir = createTempFile(fileName); if (listOfProjectFiles != null) { listOfProjectFiles.add(fileInTempDir); } fOut = new FileOutputStream(fileInTempDir); } else { fOut = new FileOutputStream(fileName); } IOCanceller canceller = new IOCanceller(monitor, fOut); canceller.startTask(); fOut.write(xml.getBytes(RECOMMENDED_CHAR_ENCODING)); canceller.taskFinished(); } catch (FileNotFoundException e) { log.debug(Messages.File + StringConstants.SPACE + Messages.NotFound, e); throw new PMSaveException(Messages.File + StringConstants.SPACE + fileName + Messages.NotFound + StringConstants.COLON + StringConstants.SPACE + e.toString(), MessageIDs.E_FILE_IO); } catch (IOException e) { // If the operation has been canceled, then this is just // a result of cancelling the IO. if (!monitor.isCanceled()) { log.debug(Messages.GeneralIoExeption, e); throw new PMSaveException(Messages.GeneralIoExeption + e.toString(), MessageIDs.E_FILE_IO); } } catch (PersistenceException e) { log.debug(Messages.CouldNotInitializeProxy + StringConstants.DOT, e); throw new PMSaveException(e.getMessage(), MessageIDs.E_DATABASE_GENERAL); } finally { if (fOut != null) { try { fOut.close(); } catch (IOException e) { // just log, we are already done log.error(Messages.CantCloseOOS + fOut.toString(), e); } } } return null; }
From source file:za.ac.pearson.cti.settingsreader.JSONFileSettingReader.java
@Override public Map<String, String> getAllSettings() { JSONParser parser = new JSONParser(); try {/*from w ww .j av a 2s . c om*/ Object obj = parser.parse(new FileReader(fileLocation)); JSONObject jsonObject = (JSONObject) obj; settings.putAll(jsonObject); } catch (FileNotFoundException ex) { log.error(ex.toString()); } catch (IOException | ParseException ex) { log.error(ex.toString()); } return settings; }
From source file:eionet.gdem.conversion.converters.ExcelConverter.java
@Override public String convert(InputStream source, InputStream xslt, OutputStream result, String cnvFileExt) throws GDEMException, Exception { String xmlFile = Utils.getUniqueTmpFileName(".xml"); String excelFile = Utils.getUniqueTmpFileName(".xls"); OutputStream xmlOut = null;/*from w ww. j ava2 s.co m*/ try { xmlOut = new FileOutputStream(xmlFile); runXslTransformation(source, xslt, xmlOut); ExcelProcessor ep = new ExcelProcessor(); if (result != null) { ep.makeExcel(xmlFile, result); } else { ep.makeExcel(xmlFile, excelFile); } } catch (FileNotFoundException e) { LOGGER.error("Error " + e.toString(), e); throw new GDEMException("Error transforming Excel " + e.toString(), e); } finally { IOUtils.closeQuietly(xmlOut); } try { Utils.deleteFile(xmlFile); } catch (Exception e) { LOGGER.error("Couldn't delete the result file: " + xmlFile, e); } return excelFile; }
From source file:eionet.gdem.conversion.converters.OdsConverter.java
@Override public String convert(InputStream source, InputStream xslt, OutputStream result, String cnvFileExt) throws GDEMException, Exception { FileOutputStream xmlOut = null; String xmlFile = Utils.getUniqueTmpFileName(".xml"); String odsFile = Utils.getUniqueTmpFileName(".ods"); try {// w w w.j a va 2s . co m xmlOut = new FileOutputStream(xmlFile); runXslTransformation(source, xslt, xmlOut); OpenDocumentProcessor odp = new OpenDocumentProcessor(); if (result != null) { odp.makeSpreadsheet(xmlFile, result); } else { odp.makeSpreadsheet(xmlFile, odsFile); } } catch (FileNotFoundException e) { LOGGER.error("Error " + e.toString(), e); throw new GDEMException("Error transforming OpenDocument Spreadhseet " + e.toString(), e); } finally { IOUtils.closeQuietly(xmlOut); } try { Utils.deleteFile(xmlFile); } catch (Exception e) { LOGGER.error("Couldn't delete the result file: " + xmlFile, e); } return odsFile; }