Example usage for org.apache.commons.io IOUtils copy

List of usage examples for org.apache.commons.io IOUtils copy

Introduction

In this page you can find the example usage for org.apache.commons.io IOUtils copy.

Prototype

public static void copy(Reader input, OutputStream output) throws IOException 

Source Link

Document

Copy chars from a Reader to bytes on an OutputStream using the default character encoding of the platform, and calling flush.

Usage

From source file:edu.scripps.fl.pubchem.app.relations.ELinkStage.java

@Override
public void process(Object obj) throws StageException {
    Integer id = (Integer) obj;
    try {// w w w . jav  a  2s  . c  om
        Thread.sleep(333);
        InputStream in = EUtilsWebSession.getInstance()
                .getInputStream("http://www.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi", "dbfrom", "pcassay",
                        "db", databases, "id", "" + id)
                .call();
        File file = File.createTempFile("pubchem", "link.xml");
        IOUtils.copy(in, new FileOutputStream(file));
        emit(file);
    } catch (Exception ex) {
        throw new StageException(this, ex);
    }
}

From source file:edu.cornell.mannlib.vitro.webapp.controller.api.sparqlquery.SparqlQueryApiRdfProducer.java

@Override
public void executeAndFormat(OutputStream out) throws RDFServiceException, IOException {

    InputStream rawResult = getRawResultStream();

    if (mediaType.isNativeFormat()) {
        IOUtils.copy(rawResult, out);
    } else if (mediaType.getJenaResponseFormat().equals("JSON")) {
        // JSON-LD is a special case, since jena 2.6.4 doesn't support it.
        try {//from   www  .j  a v  a  2  s .  c o m
            JenaRDFParser parser = new JenaRDFParser();
            Object json = JSONLD.fromRDF(parseToModel(rawResult), parser);
            JSONUtils.write(new OutputStreamWriter(out, "UTF-8"), json);
        } catch (JSONLDProcessingError e) {
            throw new RDFServiceException("Could not convert from Jena model to JSON-LD", e);
        }
    } else {
        parseToModel(rawResult).write(out, mediaType.getJenaResponseFormat());
    }
}

From source file:com.crosstreelabs.cognitio.communicator.amqp.provider.BrokerProvider.java

protected Broker buildBroker() throws Exception {
    Broker broker = new Broker();
    BrokerOptions options = new BrokerOptions();
    options.setManagementMode(false);//from w ww.j  av  a2  s  .  co m

    File workDir = getWorkDir();
    File configFile = new File(workDir, "config.json");
    File passwdFile = new File(workDir, "passwd");
    workDir.mkdirs();

    if (!configFile.exists() || config.amqCleanWorkDir) {
        configFile.createNewFile();
        IOUtils.copy(BrokerProvider.class.getResourceAsStream("/qpid-config.json"),
                new FileOutputStream(configFile));
    }
    if (!passwdFile.exists() || config.amqCleanWorkDir) {
        passwdFile.createNewFile();
        IOUtils.copy(BrokerProvider.class.getResourceAsStream("/qpid-passwd"),
                new FileOutputStream(passwdFile));
    }

    options.setConfigProperty("qpid.work_dir", workDir.getAbsolutePath());
    options.setConfigProperty("qpid.amqp_port", "" + config.amqPort);
    options.setConfigProperty("qpid.http_port", "" + config.amqHttpPort);
    options.setConfigProperty("qpid.home_dir", workDir.getAbsolutePath());
    options.setInitialConfigurationLocation(configFile.getAbsolutePath());

    broker.startup(options);
    LOGGER.info("Broker started");
    return broker;
}

From source file:com.crosstreelabs.cognitio.gumshoe.format.HtmlFormatHandler.java

@Override
public boolean handles(final Visit visit) {
    if ("text/html".equals(visit.contentType)) {
        return true;
    }/*w  w w  .  j a v a 2s .  c  om*/
    if (visit.contentStream == null) {
        return false;
    }
    try {
        if (!(visit.contentStream instanceof ByteArrayInputStream)) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            IOUtils.copy(visit.contentStream, baos);
            visit.contentStream = new ByteArrayInputStream(baos.toByteArray());
        }
        byte[] buf = new byte[250];
        IOUtils.read(visit.contentStream, buf, 0, 250);
        visit.contentStream.reset();
        String chunk = new String(buf);
        return chunk.toLowerCase().contains("<html");
    } catch (IOException ex) {
    }
    return false;
}

From source file:com.redhat.red.offliner.PlaintextArtifactListReaderTest.java

@BeforeClass
public static void prepare() throws IOException {
    File tempDir = new File(TEMP_PLAINTEXT_DIR);
    if (tempDir.exists()) {
        FileUtils.deleteDirectory(tempDir);
    }/*from   w w w  . j av  a2s. c  o  m*/
    tempDir.mkdirs();

    List<String> resources = new ArrayList<String>(4);
    resources.add(RESOURCE0);
    resources.add(RESOURCE1);
    resources.add(RESOURCE2);
    resources.add(RESOURCE3);

    for (String resource : resources) {
        File target = new File(TEMP_PLAINTEXT_DIR, resource);
        OutputStream os = new FileOutputStream(target);

        if (resource.equals(RESOURCE0)) {
            IOUtils.closeQuietly(os);
            continue;
        }

        InputStream is = PlaintextArtifactListReaderTest.class.getClassLoader().getResourceAsStream(resource);

        try {
            IOUtils.copy(is, os);
        } finally {
            IOUtils.closeQuietly(is);
            IOUtils.closeQuietly(os);
        }
    }
}

From source file:com.datatorrent.lib.io.HttpOutputOperatorTest.java

@Test
public void testHttpOutputNode() throws Exception {

    final List<String> receivedMessages = new ArrayList<String>();
    Handler handler = new AbstractHandler() {
        @Override/*from  w  w  w.  j ava 2  s .c  o m*/
        @Consumes({ MediaType.APPLICATION_JSON })
        public void handle(String string, Request rq, HttpServletRequest request, HttpServletResponse response)
                throws IOException, ServletException {
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            IOUtils.copy(request.getInputStream(), bos);
            receivedMessages.add(new String(bos.toByteArray()));
            response.setContentType("text/html");
            response.setStatus(HttpServletResponse.SC_OK);
            response.getWriter().println("<h1>Thanks</h1>");
            ((Request) request).setHandled(true);
            receivedMessage = true;
        }
    };

    Server server = new Server(0);
    server.setHandler(handler);
    server.start();

    String url = "http://localhost:" + server.getConnectors()[0].getLocalPort() + "/somecontext";
    System.out.println("url: " + url);

    HttpOutputOperator<Object> node = new HttpOutputOperator<Object>();
    node.setResourceURL(new URI(url));

    node.setup(null);

    Map<String, String> data = new HashMap<String, String>();
    data.put("somekey", "somevalue");
    node.input.process(data);

    // Wait till the message is received or a maximum timeout elapses
    int timeoutMillis = 10000;
    while (!receivedMessage && timeoutMillis > 0) {
        timeoutMillis -= 20;
        Thread.sleep(20);
    }

    Assert.assertEquals("number requests", 1, receivedMessages.size());
    System.out.println(receivedMessages.get(0));
    JSONObject json = new JSONObject(data);
    Assert.assertTrue("request body " + receivedMessages.get(0),
            receivedMessages.get(0).contains(json.toString()));

    receivedMessages.clear();
    String stringData = "stringData";
    node.input.process(stringData);
    Assert.assertEquals("number requests", 1, receivedMessages.size());
    Assert.assertEquals("request body " + receivedMessages.get(0), stringData, receivedMessages.get(0));

    node.teardown();
    server.stop();
}

From source file:edu.dfci.cccb.mev.deseq.domain.cli.CliRDESeqTest.java

@Test
@Ignore//from   w  w  w. j a v  a  2 s  .co  m
public void test() throws Exception {
    try (InputStream inp = getClass().getResourceAsStream("/test_data.tsv");
            ByteArrayOutputStream copy = new ByteArrayOutputStream()) {
        IOUtils.copy(inp, copy);
        Dataset dataset = new SimpleDatasetBuilder().setParserFactories(asList(new SuperCsvParserFactory()))
                .setValueStoreBuilder(new MapBackedValueStoreBuilder())
                .build(new MockTsvInput("mock", copy.toString()));
        Selection experiment = new SimpleSelection("experiment", new Properties(),
                asList("SA1_06_25_14", "SA2_06_25_14", "SA3_06_25_14", "SA4_006_25_14", "SA5_06_25_14"));
        Selection control = new SimpleSelection("control", new Properties(),
                asList("SA10-06-25-14", "SA11_06_25_14", "SA12_06_25_14"));
        dataset.dimension(COLUMN).selections().put(experiment);
        dataset.dimension(COLUMN).selections().put(control);
        DESeq result = new StatelessScriptEngineFileBackedDESeqBuilder()
                .r(new ScriptEngineManager().getEngineByName("CliR"))
                .composerFactory(new SuperCsvComposerFactory()).dataset(dataset).control(control)
                .experiment(experiment).build();
        for (Entry e : result.full())
            log.debug("Full DESeq entry: " + e);
    }
}

From source file:com.mebigfatguy.polycasso.URLFetcher.java

/**
 * retrieve arbitrary data found at a specific url
 * - either http or file urls/*  ww  w. j  a v  a  2 s . c  o m*/
 * for http requests, sets the user-agent to mozilla to avoid
 * sites being cranky about a java sniffer
 * 
 * @param url the url to retrieve
 * @param proxyHost the host to use for the proxy
 * @param proxyPort the port to use for the proxy
 * @return a byte array of the content
 * 
 * @throws IOException the site fails to respond
 */
public static byte[] fetchURLData(String url, String proxyHost, int proxyPort) throws IOException {
    HttpURLConnection con = null;
    InputStream is = null;

    try {
        URL u = new URL(url);
        if (url.startsWith("file://")) {
            is = new BufferedInputStream(u.openStream());
        } else {
            Proxy proxy;
            if (proxyHost != null) {
                proxy = new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
            } else {
                proxy = Proxy.NO_PROXY;
            }
            con = (HttpURLConnection) u.openConnection(proxy);
            con.addRequestProperty("User-Agent",
                    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6");
            con.addRequestProperty("Accept-Charset", "UTF-8");
            con.addRequestProperty("Accept-Language", "en-US,en");
            con.addRequestProperty("Accept", "text/html,image/*");
            con.setDoInput(true);
            con.setDoOutput(false);
            con.connect();

            is = new BufferedInputStream(con.getInputStream());
        }

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        IOUtils.copy(is, baos);
        return baos.toByteArray();
    } finally {
        IOUtils.closeQuietly(is);
        if (con != null) {
            con.disconnect();
        }
    }
}

From source file:com.ctriposs.r2.filter.compression.GzipCompressor.java

@Override
public byte[] deflate(InputStream data) throws CompressionException {
    ByteArrayOutputStream out;/* www .j av a  2  s.co m*/
    GZIPOutputStream gzip = null;

    try {
        out = new ByteArrayOutputStream();
        gzip = new GZIPOutputStream(out);

        IOUtils.copy(data, gzip);
    } catch (IOException e) {
        throw new CompressionException(CompressionConstants.DECODING_ERROR + getContentEncodingName(), e);
    } finally {
        if (gzip != null) {
            IOUtils.closeQuietly(gzip);
        }
    }

    return out.toByteArray();
}

From source file:com.boozallen.cognition.test.utils.TestResourceUtilsTest.java

@Test
public void testGetResourceAsStream() throws Exception {
    try (InputStream stream = getResourceAsStream(this.getClass(), "test-file.txt")) {
        StringWriter stringWriter = new StringWriter();
        IOUtils.copy(stream, stringWriter);
        assertThat(stringWriter.toString(), is(TEST_FILE_TEXT));
    }// w  w  w .j  av  a  2  s . co  m
}