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:fr.smile.services.PatchService.java

public void readFile() throws IOException {

    InputStream is = getClass().getResourceAsStream(PATH);

    StringWriter writer = new StringWriter();
    IOUtils.copy(is, writer);
    String json = writer.toString();
    is.close();/*from  w  w  w .ja v  a 2  s.c o  m*/
    writer.close();

    JSONArray a = jsonParser(json);

    String version, endVersion, url, instructions, instructionsCluster, warning;
    Boolean reboot, license;

    Patch patch;

    for (Object o : a) {
        JSONObject jsonVersion = (JSONObject) o;

        version = (String) jsonVersion.get("version");
        listVersion.add(version);

        JSONArray patches = (JSONArray) jsonVersion.get("patches");

        for (Object p : patches) {
            JSONObject jsonPatch = (JSONObject) p;
            endVersion = (String) jsonPatch.get("endVersion");
            url = (String) jsonPatch.get("url");
            instructions = (String) jsonPatch.get("instructions");
            instructionsCluster = (String) jsonPatch.get("instructionsCluster");
            warning = (String) jsonPatch.get("warning");
            String r = (String) jsonPatch.get("reboot");
            reboot = r == null || "1".equals(r);
            String l = (String) jsonPatch.get("license");
            license = l != null && "1".equals(l);

            patch = Patch.builder().startVersion(version).endVersion(endVersion).url(url)
                    .instructionsCluster(instructionsCluster).instructions(instructions).warning(warning)
                    .reboot(reboot).license(license).build();

            listPatch.add(patch);
        }
    }
}

From source file:cz.muni.fi.mir.mathmlcanonicalization.utils.DTDManipulatorTest.java

/**
 * Test of injectXHTMLPlusMathMLDTD method, of class DTDManipulator.
 *///from  w w w  . j  ava2  s  . co m
@Test
public void testInjectXHTMLPlusMathMLDTD() throws IOException {

    System.out.println("testInjectXHTMLPlusMathMLDTD");

    InputStream in = this.getClass()
            .getResourceAsStream(RESOURCE_SUBDIR + "injectXHTMLPlusMathMLDTD.input.xml");
    InputStream expResult = this.getClass()
            .getResourceAsStream(RESOURCE_SUBDIR + "injectXHTMLPlusMathMLDTD.output.xml");
    InputStream result = DTDManipulator.injectXHTMLPlusMathMLDTD(in);

    StringWriter resultWriter = new StringWriter();
    IOUtils.copy(result, resultWriter);
    String resultString = resultWriter.toString();
    StringWriter expResultWriter = new StringWriter();
    IOUtils.copy(expResult, expResultWriter);
    String expResultString = expResultWriter.toString();

    assertEquals("DTD not properly injected", expResultString, resultString);

}

From source file:io.v.x.media_sharing.SendMediaTask.java

@Override
protected Void doInBackground(Void... params) {
    try {//from  www .  j  a v  a 2 s  . c o  m
        InputStream is = activity.getContentResolver().openInputStream(uri);

        MediaSharingClient client = MediaSharingClientFactory.getMediaSharingClient(targetName);

        // TODO(bprosnitz) Remove this option when possible. It is allows the app to connect
        // without having the proper blessings.
        Options opts = new Options();
        opts.set(OptionDefs.SKIP_SERVER_ENDPOINT_AUTHORIZATION, true);

        String mimeType = activity.getIntent().getType();
        TypedClientStream<byte[], Void, Void> stream = client.displayBytes(vContext, mimeType, opts);

        ClientByteOutputStream os = new ClientByteOutputStream(stream);
        IOUtils.copy(is, os);
        stream.finish();

        Log.i(TAG, activity.getString(R.string.share_messsage_success));
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, R.string.share_messsage_success, Toast.LENGTH_LONG).show();
            }
        });
        return null;
    } catch (IOException | VException e) {
        final String errorMessage = activity.getString(R.string.share_messsage_error) + ": " + e.toString();
        Log.e(TAG, errorMessage);

        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(activity, errorMessage, Toast.LENGTH_LONG).show();
            }
        });

        throw new RuntimeException(e);
    }
}

From source file:com.intropro.prairie.format.seq.SequenceFormatReader.java

private void copyDataToTmpFile() throws IOException {
    tmpFile = File.createTempFile("seq-", ".dat");
    OutputStream outputStream = new FileOutputStream(tmpFile);
    IOUtils.copy(inputStream, outputStream);
    outputStream.close();//from w  ww . j a v  a2s  .  c  o m
    inputStream.close();
}

From source file:cn.suishen.email.mail.store.imap.ImapTempFileLiteral.java

ImapTempFileLiteral(FixedLengthInputStream stream) throws IOException {
    mSize = stream.getLength();//  ww w.j a v  a2s . c o  m
    mFile = File.createTempFile("imap", ".tmp", TempDirectory.getTempDirectory());

    // Unfortunately, we can't really use deleteOnExit(), because temp filenames are random
    // so it'd simply cause a memory leak.
    // deleteOnExit() simply adds filenames to a static list and the list will never shrink.
    // mFile.deleteOnExit();
    OutputStream out = new FileOutputStream(mFile);
    IOUtils.copy(stream, out);
    out.close();
}

From source file:com.t3.persistence.FileUtil.java

public static void saveResource(String resource, File destDir, String filename) throws IOException {
    File outFilename = new File(destDir + File.separator + filename);
    try (InputStream inStream = FileUtil.class.getClassLoader().getResourceAsStream(resource);
            BufferedOutputStream outStream = new BufferedOutputStream(new FileOutputStream(outFilename));) {
        IOUtils.copy(inStream, outStream);
    }// w w  w .  j a  v  a2s  .c o m
}

From source file:eu.creatingfuture.propeller.webLoader.servlets.WebRequestServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.addHeader("Access-Control-Allow-Origin", "*");
    String getUrl = req.getParameter("get");
    if (getUrl == null) {
        resp.sendError(400);/* w ww .  j  a v  a2 s .  co  m*/
        return;
    }
    logger.info(getUrl);
    URL url;
    HttpURLConnection connection = null;
    try {
        url = new URL(getUrl);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.setUseCaches(false);

        resp.setContentType(connection.getContentType());
        resp.setContentLengthLong(connection.getContentLengthLong());
        logger.log(Level.INFO, "Content length: {0} response code: {1} content type: {2}", new Object[] {
                connection.getContentLengthLong(), connection.getResponseCode(), connection.getContentType() });
        resp.setStatus(connection.getResponseCode());
        IOUtils.copy(connection.getInputStream(), resp.getOutputStream());
    } catch (IOException ioe) {
        resp.sendError(500, ioe.getMessage());
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
    }
}

From source file:com.dianping.phoenix.router.ResponseFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    if (response instanceof HttpServletResponse && request instanceof HttpServletRequest) {
        HttpServletRequest hRequest = (HttpServletRequest) request;
        if (StringUtils.equalsIgnoreCase(SCRIPT_NAME, hRequest.getRequestURI())) {
            IOUtils.copy(this.getClass().getResourceAsStream(SCRIPT_NAME), response.getOutputStream());
            return;
        }//from   ww w .j a va2  s .  c o  m

        StatusAwareServletResponse statusAwareServletResponse = new StatusAwareServletResponse(
                (HttpServletResponse) response);

        chain.doFilter(request, statusAwareServletResponse);

        if (ACCEPTED_STATUS_CODE.contains(statusAwareServletResponse.getStatus())
                && (StringUtils.isBlank(statusAwareServletResponse.getContentType()) || ACCEPTED_CONTENT_TYPE
                        .contains(statusAwareServletResponse.getContentType().toLowerCase()))
                && hRequest.getHeader("x-requested-with") == null) {
            statusAwareServletResponse.getOutputStream().write(RESPONSE_APPEND_TEXT);
        }

    } else {
        chain.doFilter(request, response);
    }

}

From source file:com.groupdocs.ui.Utils.java

public static int copyStream(InputStream input, OutputStream output) {
    try {/*from   w  w w .ja v  a 2 s.co m*/
        return IOUtils.copy(input, output);
    } catch (IOException x) {
        throw new UncheckedIOException(x);
    }
}

From source file:eu.europa.ec.markt.dss.ws.WSDocument.java

/**
 * //from w  w  w . j av  a2 s  .  c o  m
 * The default constructor for WSDocument.
 * 
 * @param doc
 * @throws IOException
 */
public WSDocument(Document doc) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();
    IOUtils.copy(doc.openStream(), buffer);
    binary = buffer.toByteArray();
}