List of usage examples for org.apache.commons.io IOUtils readLines
public static List readLines(Reader input) throws IOException
Reader
as a list of Strings, one entry per line. From source file:com.conversantmedia.mapreduce.example.distribute.DistributedObjectExample.java
/** * Distributed {@link Set} containing the prepared list of blacklisted words. * // w w w. j a v a 2 s. c o m * @return Set the set of blacklisted words * @throws IOException if it fails to read in the file */ @Distribute public Set<String> getBlacklist() throws IOException { Set<String> blacklist = null; if (StringUtils.isNotBlank(context.blacklist)) { blacklist = new HashSet<>(); InputStreamReader reader = null; try { FileSystem fs = FileSystem.get(new Configuration()); FileStatus file = fs.getFileStatus(new Path(context.blacklist)); reader = new InputStreamReader(fs.open(file.getPath())); for (String line : IOUtils.readLines(reader)) { blacklist.add(line); } } finally { IOUtils.closeQuietly(reader); } } return blacklist; }
From source file:com.haulmont.yarg.formatters.impl.doc.connector.LinuxProcessManager.java
protected List<String> execute(String... args) throws IOException { Process process = new ProcessBuilder(args).start(); @SuppressWarnings("unchecked") List<String> lines = IOUtils.readLines(process.getInputStream()); return lines; }
From source file:edu.ucsd.crbs.cws.io.ResourceToExecutableScriptWriterImpl.java
/** * Writes a given resource to an executable script file. The result is written to * <b>destinationScript</b> and this script is made executable via File setExecutable * method./*from w w w.j a v a 2s . c o m*/ * * @param resourcePath Path that can be loaded via {@link Class.class.getResourceAsStream} * @param destinationScript File to write script to * @param replacer Optional object that lets caller alter script on a line by line basis before it is written * @throws Exception if there is an io error * @throws IllegalArgumentException if either <b>resourcePath</b> or <b>destinationScript</b> are null */ @Override public void writeResourceToScript(final String resourcePath, final String destinationScript, StringReplacer replacer) throws Exception { if (resourcePath == null) { throw new IllegalArgumentException("resourcePath method parameter cannot be null"); } if (destinationScript == null) { throw new IllegalArgumentException("destinationScript method parameter cannot be null"); } //load script List<String> scriptLines = IOUtils.readLines(Class.class.getResourceAsStream(resourcePath)); BufferedWriter bw = new BufferedWriter(new FileWriter(destinationScript)); for (String line : scriptLines) { if (replacer != null) { bw.write(replacer.replace(line)); } else { bw.write(line); } bw.newLine(); } bw.flush(); bw.close(); //make script executable File script = new File(destinationScript); script.setExecutable(true, false); }
From source file:de.tudarmstadt.ukp.experiments.argumentation.sequence.evaluation.helpers.FinalTableExtractor.java
@Deprecated public static void extractCDResults(String inFile, String outFile) throws IOException { File file = new File(inFile); Table<String, String, String> table = TreeBasedTable.create(); List<String> lines = IOUtils.readLines(new FileInputStream(file)); Iterator<String> iterator = lines.iterator(); while (iterator.hasNext()) { String featureSet = iterator.next(); // shorten fs name featureSet = featureSet.replaceAll("^\\./", ""); // extract domain DocumentDomain domain = null;//from w w w . j a v a 2 s . com for (DocumentDomain currentDomain : DocumentDomain.values()) { if (featureSet.contains(currentDomain.toString())) { if (domain != null) { throw new IllegalStateException("!!!"); } domain = currentDomain; } } if (domain == null) { throw new IllegalStateException("No domain found! " + featureSet); } String shortFeatureSet = featureSet.replaceAll("/.*", ""); System.out.println(shortFeatureSet); String[] fsSettings = shortFeatureSet.split("_", 6); String clusters = fsSettings[4]; if (includeRow(clusters)) { table.put(shortFeatureSet, "FS", fsSettings[1]); } // 12 lines with results for (int i = 0; i < 12; i++) { String line = iterator.next(); String[] split = line.split("\\s+"); String measure = split[0]; Double value = Double.valueOf(split[1]); // only a50 and a100,s1000 if (includeRow(clusters)) { table.put(shortFeatureSet, "Clusters", clusters); if (includeColumn(measure)) { table.put(shortFeatureSet, domain.toString() + measure, String.format(Locale.ENGLISH, "%.3f", value)); } } } } // tableToCsv(table, new FileWriter(outFile)); }
From source file:com.monarchapis.driver.jaxrs.common.SwaggerDocumentationResourceTest.java
@Test public void testApiDeclaration() throws IOException { String actual = resource.getApiDeclaration("v1", "test-resource", uriInfo); Reader reader = new StringReader(actual); List<String> lines = IOUtils.readLines(reader); assertEquals(7, lines.size());//from ww w.j av a 2 s .co m assertEquals("{", lines.get(0)); assertEquals("\t\"basePath\" : \"http://api.company.com/test\",", lines.get(1)); assertEquals("\t\"oauthPasswordUrl\" : \"passwordUrl\",", lines.get(2)); assertEquals("\t\"oauthImplicitUrl\" : \"implicitUrl\",", lines.get(3)); assertEquals("\t\"oauthAuthorizationCodeRequestUrl\" : \"authorizationCodeRequestUrl\",", lines.get(4)); assertEquals("\t\"oauthAuthorizationCodeTokenUrl\" : \"authorizationCodeTokenUrl\"", lines.get(5)); assertEquals("}", lines.get(6)); }
From source file:com.xpn.xwiki.pdf.impl.PDFURIResolverTest.java
@Test public void resolveWhenMapItemExists() throws Exception { DocumentReference documentReference = new DocumentReference("wiki", "space", "page"); AttachmentReference attachmentReference = new AttachmentReference("fileName", documentReference); com.xpn.xwiki.XWiki xwiki = mock(com.xpn.xwiki.XWiki.class); XWikiDocument document = mock(XWikiDocument.class); XWikiAttachment attachment = mock(XWikiAttachment.class); XWikiContext context = mock(XWikiContext.class); // Add an encoded space character to test that the URL is not decoded. The input URL (to be resolved) is // expected to be encoded because the URL factory creates encoded URLs. String url = "encoded+url"; when(context.get("pdfExportImageURLMap")).thenReturn(Collections.singletonMap(url, attachmentReference)); when(context.getWiki()).thenReturn(xwiki); when(xwiki.getDocument(attachmentReference.extractReference(EntityType.DOCUMENT), context)) .thenReturn(document);/*w w w. ja v a 2 s .c om*/ when(document.getAttachment("fileName")).thenReturn(attachment); when(attachment.getContentInputStream(context)).thenReturn(new ByteArrayInputStream("content".getBytes())); PDFURIResolver resolver = new PDFURIResolver(context); Source source = resolver.resolve(url, "base"); Assert.assertEquals(StreamSource.class, source.getClass()); Assert.assertEquals("content", IOUtils.readLines(((StreamSource) source).getInputStream()).get(0)); }
From source file:com.example.listsync.WebDavRepository.java
private List<String> doDownload(String file) throws IOException { LOGGER.info("downloading {}", file); GetMethod get = new GetMethod(getFullWatchURL() + file); int i = client.executeMethod(get); if (i != 200) { return new ArrayList<>(); }// w ww. ja v a 2 s . c o m byte[] bytes = get.getResponseBody(); LOGGER.info("download complete {}", file); return IOUtils.readLines(new ByteArrayInputStream(bytes)); }
From source file:com.turn.splicer.SuggestHttpWorker.java
@Override public String call() throws Exception { LinkedBlockingQueue<String> TSDs; //TODO: have it implement its own RegionChecker to get hbase locality looking for metric names //lets have it just pick a random host String hostname = getRandomHost(); TSDs = HttpWorker.TSDMap.get(hostname); if (TSDs == null) { LOG.error("We are not running TSDs on regionserver={}. Choosing a random host failed", hostname); return "{'error': 'Choice of hostname=" + hostname + " failed.'}"; }/*from w ww .j ava 2s . c o m*/ String server = TSDs.take(); String uri = "http://" + server + "/api/suggest?" + suggestQuery; CloseableHttpClient postman = HttpClientBuilder.create().build(); try { HttpGet getRequest = new HttpGet(uri); LOG.info("Sending query=" + uri + " to TSD running on host=" + hostname); HttpResponse response = postman.execute(getRequest); if (response.getStatusLine().getStatusCode() != 200) { throw new RuntimeException( "Failed : HTTP error code : " + response.getStatusLine().getStatusCode()); } List<String> dl = IOUtils.readLines(response.getEntity().getContent()); String result = StringUtils.join(dl, ""); LOG.info("Result={}", result); return result; } finally { IOUtils.closeQuietly(postman); TSDs.put(server); LOG.info("Returned {} into the available queue", server); } }
From source file:com.izforge.izpack.compiler.CompilerConfigSamplesTest.java
@Test @InstallFile("samples/silverpeas/silverpeas.xml") public void installerShouldConfigureSplashScreenCorrectly() throws Exception { compilerConfig.executeCompiler();// www .ja v a2 s .co m jar = testContainer.getComponent(JarFile.class); assertThat((ZipFile) jar, ZipMatcher.isZipMatching(IsCollectionContaining.hasItems("META-INF/Vim_splash.png"))); ZipEntry entry = jar.getEntry("META-INF/MANIFEST.MF"); InputStream content = jar.getInputStream(entry); try { List<String> list = IOUtils.readLines(content); assertThat(list, IsCollectionContaining.hasItem("SplashScreen-Image: META-INF/Vim_splash.png")); } finally { content.close(); } }
From source file:edu.cornell.mannlib.vitro.webapp.rdfservice.impl.virtuoso.RDFServiceVirtuoso.java
@Override protected void executeUpdate(String updateString) throws RDFServiceException { updateString = tweakUpdateStringSyntax(updateString); log.debug("UPDATE STRING: " + updateString); try {// w w w . j a v a 2 s . c om HttpResponse response = httpClient.execute(createHttpRequest(updateString), createHttpContext()); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode > 399) { log.error("response " + response.getStatusLine() + " to update. \n"); try (InputStream content = response.getEntity().getContent()) { for (String line : IOUtils.readLines(content)) { log.error("response-line >>" + line); } } throw new RDFServiceException("Unable to perform SPARQL UPDATE: status code = " + statusCode); } } catch (Exception e) { log.error("Failed to update: " + updateString, e); throw new RDFServiceException("Unable to perform change set update", e); } }