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:azkaban.utils.GZIPUtils.java

public static byte[] unGzipBytes(byte[] bytes) throws IOException {
    ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
    GZIPInputStream gzipInputStream = new GZIPInputStream(byteInputStream);

    ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
    IOUtils.copy(gzipInputStream, byteOutputStream);

    return byteOutputStream.toByteArray();
}

From source file:com.santiagolizardo.madcommander.util.io.FileOperations.java

public static boolean copy(InputStream is, File file) {
    try {//from   ww w .j av a 2  s. c  o  m
        try (FileOutputStream fos = new FileOutputStream(file)) {
            IOUtils.copy(is, fos);
            is.close();
        }

        return true;
    } catch (IOException e) {
        logger.warning(e.getMessage());
        return false;
    }
}

From source file:be.i8c.sag.util.FileUtils.java

/**
 * Receives the fop.xconf stream and puts it in a temporary file.
 *
 * @param stream The stream tat will be written to the temporary file
 * @return Returns temporary file with the content of the stream
 * @throws IOException when the file couldn't be created
 *//*from  ww w  . ja  v a 2 s  .  c o m*/
public static File createTempFopConf(InputStream stream) throws IOException {
    File temp;
    temp = File.createTempFile("fop", ".xconf");
    temp.deleteOnExit();
    FileOutputStream out = new FileOutputStream(temp);
    IOUtils.copy(stream, out);
    return temp;
}

From source file:com.lushapp.common.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();//from  ww w  .ja  v  a2 s.c om
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    response.reset();
    WebUtils.setNoCacheHeader(response);

    response.setContentType("application/x-download");
    response.setContentLength((int) file.length());

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    WebUtils.setDownloadableHeader(request, response, displayFilename);
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:com.ruenzuo.through.activities.LicensesActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.licenses_activity_layout);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    AssetManager assetManager = getAssets();
    try {/* w w w.j  a  v  a  2  s  . c o m*/
        InputStream inputStream = assetManager.open("licenses/licenses.txt");
        StringWriter stringWriter = new StringWriter();
        IOUtils.copy(inputStream, stringWriter);
        String licenses = stringWriter.toString();
        TextView txtViewLicenses = (TextView) findViewById(R.id.txtViewLicenses);
        txtViewLicenses.setText(licenses);
        txtViewLicenses.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:net.pickapack.io.cmd.CommandLineHelper.java

/**
 *
 * @param cmd//from w ww  .  ja  va  2s.  c o  m
 * @param waitFor
 * @return
 */
public static int invokeNativeCommand(String[] cmd, boolean waitFor) {
    try {
        Runtime r = Runtime.getRuntime();
        final Process ps = r.exec(cmd);
        //            ProcessBuilder pb = new ProcessBuilder(cmd);
        //            Process ps = pb.start();

        new Thread() {
            {
                setDaemon(true);
            }

            @Override
            public void run() {
                try {
                    IOUtils.copy(ps.getInputStream(), System.out);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread() {
            {
                setDaemon(true);
            }

            @Override
            public void run() {
                try {
                    IOUtils.copy(ps.getErrorStream(), System.out);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        if (waitFor) {
            int exitValue = ps.waitFor();
            if (exitValue != 0) {
                System.out.println("WARN: Process exits with non-zero code: " + exitValue);
            }

            ps.destroy();

            return exitValue;
        }

        return 0;
    } catch (Exception e) {
        e.printStackTrace();
        return -1;
    }
}

From source file:de.thischwa.pmcms.server.ServletUtils.java

public static boolean writeFile(HttpServletResponse resp, File reqFile) {
    boolean retVal = false;
    InputStream in = null;//from   w ww .  ja v  a2s .com
    try {
        in = new BufferedInputStream(new FileInputStream(reqFile));
        IOUtils.copy(in, resp.getOutputStream());
        logger.debug("File successful written to servlet response: " + reqFile.getAbsolutePath());
    } catch (FileNotFoundException e) {
        logger.error("Resource not found: " + reqFile.getAbsolutePath());
    } catch (IOException e) {
        logger.error(String.format("Error while rendering [%s]: %s", reqFile.getAbsolutePath(), e.getMessage()),
                e);
    } finally {
        IOUtils.closeQuietly(in);
    }
    return retVal;
}

From source file:ch.algotrader.esper.EsperTestBase.java

protected static Module load(final Reader in, final String name) throws IOException, ParseException {
    StringWriter buffer = new StringWriter();
    IOUtils.copy(in, buffer);
    return EPLModuleUtil.parseInternal(buffer.toString(), name);
}

From source file:jfix.zk.Medias.java

public static File asFile(Media media) {
    try {/*from   ww  w  . j a v a2s  .c  o  m*/
        File directory = Files.createTempDirectory("jfix-media").toFile();
        directory.deleteOnExit();
        File file = new File(directory.getPath() + File.separator + media.getName());
        OutputStream output = new FileOutputStream(file);
        if (media.isBinary()) {
            InputStream input = Medias.asStream(media);
            IOUtils.copy(input, output);
            input.close();
        } else {
            Reader input = Medias.asReader(media);
            IOUtils.copy(input, output);
            input.close();
        }
        output.close();
        return file;
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

From source file:com.textocat.textokit.commons.io.ProcessIOUtils.java

/**
 * @param proc process which input stream will receive bytes from the
 *             argument input stream/*from   www  .j a va  2s. co  m*/
 * @param in   input stream. Note that it is closed at the end.
 */
public static void feedProcessInput(Process proc, final InputStream in, final boolean closeStdIn)
        throws IOException {
    final OutputStream procStdIn = proc.getOutputStream();
    final List<Exception> exceptions = Lists.newLinkedList();
    Thread writerThread = new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                IOUtils.copy(in, procStdIn);
                if (closeStdIn) {
                    procStdIn.flush();
                    closeQuietly(procStdIn);
                }
            } catch (Exception e) {
                exceptions.add(e);
            } finally {
                closeQuietly(in);
            }
        }
    });
    writerThread.start();
    try {
        writerThread.join();
    } catch (InterruptedException e) {
        // do nothing, just set flag
        Thread.currentThread().interrupt();
    }
    if (!exceptions.isEmpty()) {
        Exception ex = exceptions.get(0);
        throw ex instanceof IOException ? (IOException) ex
                : new IOException("Unexpected exception in writing thread", ex);
    }
}