List of usage examples for org.apache.commons.io.input BOMInputStream BOMInputStream
public BOMInputStream(InputStream delegate)
From source file:hrytsenko.gscripts.io.CsvFiles.java
/** * Load records from file./*from ww w. j a v a 2s. co m*/ * * @param args * the named arguments. * * @return the list of text records. * * @throws AppException * if file could not be loaded. */ public static List<Map<String, String>> loadCsv(Map<String, ?> args) { Path path = NamedArgs.findPath(args); LOGGER.info("Load {}.", path.getFileName()); CsvSchema schema = schemaFrom(args).setUseHeader(true).build(); try (InputStream dataStream = Files.newInputStream(path); InputStream bomStream = new BOMInputStream(dataStream); Reader dataReader = new InputStreamReader(bomStream, charsetFrom(args))) { CsvMapper mapper = new CsvMapper(); ObjectReader reader = mapper.readerFor(Map.class).with(schema); return Lists.newArrayList(reader.readValues(dataReader)); } catch (Exception exception) { throw new AppException(String.format("Could not load file %s.", path.getFileName()), exception); } }
From source file:hrytsenko.csv.IO.java
/** * Gets records from file.//from ww w . j a v a 2s . co m * * <p> * If closure is given, then it will be applied to each record. * * @param args * the named arguments {@link IO}. * @param closure * the closure to be applied to each record. * * @return the loaded records. * * @throws IOException * if file could not be read. */ public static List<Record> load(Map<String, ?> args, Closure<?> closure) throws IOException { Path path = getPath(args); LOGGER.info("Load: {}.", path.getFileName()); try (InputStream dataStream = newInputStream(path, StandardOpenOption.READ); InputStream bomStream = new BOMInputStream(dataStream); Reader dataReader = new InputStreamReader(bomStream, getCharset(args))) { CsvSchema csvSchema = getSchema(args).setUseHeader(true).build(); CsvMapper csvMapper = new CsvMapper(); ObjectReader csvReader = csvMapper.reader(Map.class).with(csvSchema); Iterator<Map<String, String>> rows = csvReader.readValues(dataReader); List<Record> records = new ArrayList<>(); while (rows.hasNext()) { Map<String, String> row = rows.next(); Record record = new Record(); record.putAll(row); records.add(record); if (closure != null) { closure.call(record); } } return records; } }
From source file:com.github.opensensingcity.lindt.api.ListExamples.java
@GET @Produces(MediaType.APPLICATION_JSON)//from w ww.j a va 2 s . co m @Path("/{id}") public Response doGet(@PathParam("id") String id) throws IOException, URISyntaxException, Exception { LOG.info("Loading example {}", id); File example = new File(ListExamples.class.getClassLoader().getResource("examples/" + id).toURI()); Request request = new Request(); try { request.query = IOUtils.toString(new BOMInputStream(new FileInputStream(new File(example, "query.rq"))), "UTF-8"); } catch (Exception ex) { request.query = ""; LOG.trace(ex.getMessage()); } try { request.graph = IOUtils .toString(new BOMInputStream(new FileInputStream(new File(example, "graph.ttl"))), "UTF-8"); } catch (Exception ex) { request.graph = ""; LOG.trace(ex.getMessage()); } Response.ResponseBuilder res = Response.ok(gson.toJson(request), "application/json"); return res.build(); }
From source file:com.itemanalysis.jmetrik.data.Scorer.java
private void readScoringFile(String fileName) throws IOException { File f = new File(fileName); CSVParser parser = null;/*w w w . j a v a 2s. c o m*/ Reader reader = null; GenericItemScoring itemScoring = null; SpecialDataCodes specialCodes = null; String name = ""; String option = ""; int score = 0; try { reader = new InputStreamReader(new BOMInputStream(new FileInputStream(f)), "UTF-8"); parser = new CSVParser(reader, CSVFormat.EXCEL.withHeader()); for (CSVRecord csvRecord : parser) { name = csvRecord.get("name"); option = csvRecord.get("option"); score = Integer.parseInt(csvRecord.get("score")); itemScoring = new GenericItemScoring(name); itemScoring.addCategory(option, score); specialCodes = new SpecialDataCodes(); if (csvRecord.isMapped("missing")) specialCodes.setMissingDataCode(csvRecord.get("missing")); if (csvRecord.isMapped("missing score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("missing score"))); if (csvRecord.isMapped("notreached")) specialCodes.setMissingDataCode(csvRecord.get("notreached")); if (csvRecord.isMapped("notreached score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("notreached score"))); if (csvRecord.isMapped("omitted")) specialCodes.setMissingDataCode(csvRecord.get("omitted")); if (csvRecord.isMapped("omitted score")) specialCodes.setMissingDataScore(Integer.parseInt(csvRecord.get("omitted score"))); scoring.put(name, itemScoring); } } catch (IOException ex) { throw (ex); } finally { parser.close(); reader.close(); } }
From source file:eu.peppol.smp.SmpContentRetrieverImpl.java
/** * Gets the XML content of a given url, wrapped in an InputSource object. *//*www . j av a2 s . co m*/ @Override public InputSource getUrlContent(URL url) { HttpURLConnection httpURLConnection = null; try { httpURLConnection = (HttpURLConnection) url.openConnection(); httpURLConnection.connect(); } catch (IOException e) { throw new IllegalStateException("Unable to connect to " + url + " ; " + e.getMessage(), e); } try { if (httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_UNAVAILABLE) throw new TryAgainLaterException(url, httpURLConnection.getHeaderField("Retry-After")); if (httpURLConnection.getResponseCode() != HttpURLConnection.HTTP_OK) throw new ConnectionException(url, httpURLConnection.getResponseCode()); } catch (IOException e) { throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e); } try { String encoding = httpURLConnection.getContentEncoding(); InputStream in = new BOMInputStream(httpURLConnection.getInputStream()); InputStream result; if (encoding != null && encoding.equalsIgnoreCase(ENCODING_GZIP)) { result = new GZIPInputStream(in); } else if (encoding != null && encoding.equalsIgnoreCase(ENCODING_DEFLATE)) { result = new InflaterInputStream(in); } else { result = in; } String xml = readInputStreamIntoString(result); return new InputSource(new StringReader(xml)); } catch (Exception e) { throw new RuntimeException("Problem reading URL data at " + url.toExternalForm(), e); } }
From source file:com.hp.alm.ali.idea.services.CustomizationService.java
@Override protected APMCommonSettings doGetValue(Integer key) { InputStream is = null;/* ww w. j a va2s . co m*/ try { is = restService.getForStream("customization/extensions/dev/preferences"); } catch (Exception e) { // IDE Customization extension may not be installed } if (is != null) { try { return JAXBSupport.unmarshall(new BOMInputStream(is), APMCommonSettings.class); } catch (Exception e) { // go with defaults if anything goes wrong } } return new APMCommonSettings(); }
From source file:de.uzk.hki.da.metadata.MetsMetadataStructure.java
public MetsMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents) throws FileNotFoundException, JDOMException, IOException { super(workPath, metadataFile, documents); metsFile = metadataFile;// www. ja v a 2 s . c o m currentDocuments = documents; SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, metsFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); metsDoc = builder.build(is); metsParser = new MetsParser(metsDoc); fileElements = metsParser.getFileElementsFromMetsDoc(metsDoc); fileInputStream.close(); bomInputStream.close(); reader.close(); }
From source file:de.uzk.hki.da.metadata.XMPMetadataStructure.java
public XMPMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents) throws FileNotFoundException, JDOMException, IOException { super(workPath, metadataFile, documents); logger.debug("Instantiate new xmp metadata structure with metadata file " + metadataFile.getAbsolutePath() + " ... "); xmpFile = metadataFile;/*from w w w . ja v a2 s.c om*/ currentDocuments = documents; SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, xmpFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); rdfDoc = builder.build(is); descriptionElements = getXMPDescriptionElements(); fileInputStream.close(); bomInputStream.close(); reader.close(); }
From source file:net.orzo.scripting.SourceCode.java
/** * Creates source code from a Java resource identified by its absolute path * (e.g. net/orzo/userenv.js)/*www .j a v a 2 s . co m*/ * * @param res */ public static SourceCode fromResource(String res) throws IOException { String id = res.substring(Math.max(0, res.lastIndexOf("/") + 1)); InputStream sourceStream = new ResourceLoader().getResourceStream(res); if (sourceStream == null) { throw new IOException("Empty resource: " + res); } try (BOMInputStream bis = new BOMInputStream(sourceStream)) { String source = new String(ByteStreams.toByteArray(bis)); return new SourceCode(res, id, source); } }
From source file:de.uzk.hki.da.metadata.EadMetsMetadataStructure.java
public EadMetsMetadataStructure(Path workPath, File metadataFile, List<de.uzk.hki.da.model.Document> documents) throws JDOMException, IOException, ParserConfigurationException, SAXException { super(workPath, metadataFile, documents); eadFile = metadataFile;/*from ww w. j a v a 2 s .c o m*/ SAXBuilder builder = XMLUtils.createNonvalidatingSaxBuilder(); FileInputStream fileInputStream = new FileInputStream(Path.makeFile(workPath, eadFile.getPath())); BOMInputStream bomInputStream = new BOMInputStream(fileInputStream); Reader reader = new InputStreamReader(bomInputStream, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); eadDoc = builder.build(is); EAD_NS = eadDoc.getRootElement().getNamespace(); eadParser = new EadParser(eadDoc); metsReferencesInEAD = eadParser.getReferences(); metsFiles = getReferencedFiles(eadFile, metsReferencesInEAD, documents); mmsList = new ArrayList<MetsMetadataStructure>(); for (File metsFile : metsFiles) { MetsMetadataStructure mms = new MetsMetadataStructure(workPath, metsFile, documents); mmsList.add(mms); } fileInputStream.close(); bomInputStream.close(); reader.close(); }