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:com.vigglet.oei.service.GetServicePdfServlet.java

@Override
protected void downloadPdf(HttpServletRequest req, HttpServletResponse resp, User user, int modelId)
        throws Exception {
    File file = new ServicePdf(ServiceUtil.getInstance().findById(modelId), user).createServiceReport();
    resp.setContentLength((int) file.length());

    FileInputStream fis = new FileInputStream(file);
    IOUtils.copy(fis, resp.getOutputStream());
    IOUtils.closeQuietly(fis);//from  w  w  w .  ja v  a2s.c om
}

From source file:gov.va.oia.terminology.converters.sharedUtils.Unzip.java

public final static void unzip(File zipFile, File rootDir) throws IOException {
    ZipFile zip = new ZipFile(zipFile);
    @SuppressWarnings("unchecked")
    Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) zip.entries();
    while (entries.hasMoreElements()) {
        ZipEntry entry = entries.nextElement();
        java.io.File f = new java.io.File(rootDir, entry.getName());
        if (entry.isDirectory()) {
            f.mkdirs();/*from   www  . j  a  va 2s .c o m*/
            continue;
        } else {
            f.createNewFile();
        }
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zip.getInputStream(entry);
            os = new FileOutputStream(f);
            IOUtils.copy(is, os);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                    // noop
                }
            }
            if (os != null) {
                try {
                    os.close();
                } catch (Exception e) {
                    // noop
                }
            }
        }
    }
    zip.close();
}

From source file:brut.androlib.res.decoder.ResRawStreamDecoder.java

@Override
public void decode(InputStream in, OutputStream out) throws AndrolibException {
    try {/*from  ww  w.j a  va2 s  . c  o m*/
        IOUtils.copy(in, out);
    } catch (IOException ex) {
        throw new AndrolibException("Could not decode raw stream", ex);
    }
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.licenses_activity_layout);
    ButterKnife.inject(this);
    AssetManager assetManager = getAssets();
    try {//w w  w.  ja  v a2s. c  om
        InputStream inputStream = assetManager.open("licenses/licenses.txt");
        StringWriter stringWriter = new StringWriter();
        IOUtils.copy(inputStream, stringWriter);
        String licenses = stringWriter.toString();
        vwTextLicenses.setText(licenses);
    } catch (IOException e) {
        Toast.makeText(this, getString(R.string.error_while_loading_licenses), Toast.LENGTH_LONG).show();
    }
}

From source file:com.vigglet.oei.vehicle.GetVehiclePdfServlet.java

@Override
protected void downloadPdf(HttpServletRequest req, HttpServletResponse resp, User user, final int modelId)
        throws Exception {
    File file = new ServicereportPdf(VehicleUtil.getInstance().findById(modelId), user).createServiceReport();
    resp.setContentLength((int) file.length());

    FileInputStream fis = new FileInputStream(file);
    IOUtils.copy(fis, resp.getOutputStream());
    IOUtils.closeQuietly(fis);/*ww  w . j  a  v  a 2 s.co  m*/
}

From source file:hu.bme.mit.trainbenchmark.benchmark.fourstore.driver.UnixUtils.java

public static String createTempFileFromResource(final String script, final String extension,
        final boolean executable) throws IOException {
    final InputStream scriptInputStream = UnixUtils.class.getResourceAsStream("/" + script);

    // create a temporary file
    final File scriptTempFile = File.createTempFile("unix-utils-", extension);
    scriptTempFile.deleteOnExit();/*from  w w w. j  a va  2s  .c  o m*/
    try (FileOutputStream out = new FileOutputStream(scriptTempFile)) {
        IOUtils.copy(scriptInputStream, out);
    }
    scriptTempFile.setExecutable(executable);

    final String command = scriptTempFile.getAbsolutePath();
    return command;
}

From source file:com.turn.ttorrent.common.Utils.java

/**
 * Resolves a file from URI and returns its byte array.
 * /*from ww w.j a  v a  2s  . co  m*/
 * @param uri
 * @return
 * @throws ClientProtocolException
 * @throws IOException
 */
public static byte[] resolveUrlFileToByteArray(URI uri) throws ClientProtocolException, IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();

    try {

        HttpGet get = new HttpGet(uri);
        HttpResponse response = httpclient.execute(get);
        HttpEntity entity = response.getEntity();

        if (entity != null) {
            InputStream inputStream = entity.getContent();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            IOUtils.copy(inputStream, outputStream);
            return outputStream.toByteArray();
        }

        throw new IOException("Could not resolve file... [" + uri + "]");

    } finally {
        httpclient.close();
    }

}

From source file:com.ettrema.http.fs.SimpleFileContentService.java

@Override
public void setFileContent(File file, InputStream in) throws FileNotFoundException, IOException {
    FileOutputStream out = null;//w ww . j  a  v a 2 s  . c  o m
    try {
        out = new FileOutputStream(file);
        IOUtils.copy(in, out);
    } finally {
        IOUtils.closeQuietly(out);
    }
}

From source file:net.nicholaswilliams.java.teamcity.plugin.buildNumber.PluginFileUtils.java

public static void copyResource(Class<?> relativeClass, String resourceName, File destinationFile) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    try {/*from w  w w .ja va  2s  . co m*/
        inputStream = relativeClass.getResourceAsStream(resourceName);
        if (inputStream == null) {
            throw new RuntimeException("No resource with name [" + resourceName + "] found relative to class ["
                    + relativeClass.getName() + "].");
        }

        outputStream = new FileOutputStream(destinationFile);

        IOUtils.copy(inputStream, outputStream);
    } catch (IOException e) {
        throw new RuntimeException(
                "Failed to copy resource [" + resourceName + "] relative to class [" + relativeClass.getName()
                        + "] to destination file [" + destinationFile.getAbsolutePath() + "].");
    } finally {
        try {
            if (outputStream != null)
                outputStream.close();
        } catch (IOException ignore) {
        }

        try {
            if (inputStream != null)
                inputStream.close();
        } catch (IOException ignore) {
        }
    }
}

From source file:cz.cas.lib.proarc.authentication.utils.AuthUtils.java

/**
 * Writes the authentication required status to the HTTP response.
 * @param response response/*from w  w w . j  a va2  s.  c o m*/
 * @throws IOException failure
 * @see #HEADER_AUTHENTICATE_TYPE
 * @see <a href='http://www.smartclient.com/smartgwt/javadoc/com/smartgwt/client/docs/Relogin.html'>SmartGWT Relogin</a>
 */
public static void setLoginRequiredResponse(HttpServletResponse response) throws IOException {
    response.setHeader(HEADER_AUTHENTICATE_TYPE, Authenticators.getInstance().getLoginType());
    response.setStatus(HttpServletResponse.SC_FORBIDDEN);
    response.setContentType(MediaType.TEXT_HTML);
    InputStream res = ProarcAuthFilter.class.getResourceAsStream("loginRequiredMarker.html");
    try {
        IOUtils.copy(res, response.getOutputStream());
        res.close();
    } finally {
        IOUtils.closeQuietly(res);
    }
}