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:de.nrw.hbz.regal.sync.ingest.DippDownloader.java

protected void downloadObject(File dir, String pid) {
    try {//w  w  w  . jav a  2  s.c  o m
        logger.debug(pid + " start download!");
        URL url = new URL(getServer() + "get/" + pid + "?xml=true");
        File file = new File(dir.getAbsolutePath() + File.separator + URLEncoder.encode(pid, "utf-8") + ".xml");
        String data = null;
        StringWriter writer = new StringWriter();
        IOUtils.copy(url.openStream(), writer);
        data = writer.toString();
        FileUtils.writeStringToFile(file, data, "utf-8");

        downloadStreams(dir, pid);
        downloadConstituent(dir, pid);
        downloadRelatedObject(dir, pid, "rel:hasPart");

        downloadRelatedObject(dir, pid, "rel:isPartOf");
        downloadRelatedObject(new File(getDownloadLocation()), pid, "rel:isMemberOf");
        downloadRelatedObject(new File(getDownloadLocation()), pid, "rel:isSubsetOf");
        downloadRelatedObject(new File(getDownloadLocation()), pid, "rel:isMemberOfCollection");
    } catch (MalformedURLException e) {
        logger.error(e.getMessage());
    } catch (IOException e) {
        logger.error(e.getMessage());
    }

}

From source file:com.perrier.music.entity.album.AlbumZipper.java

public File zip(Album album) throws IOException {

    Path zipPath = Files.createTempFile(ZIP_PREFIX + album.getId() + "-", ".zip");
    File zipFile = zipPath.toFile();

    try (ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipFile))) {
        List<Track> tracks = album.getTracks();
        for (Track t : tracks) {
            String name = createFileName(t);
            ZipEntry zipEntry = new ZipEntry(name);
            zos.putNextEntry(zipEntry);/*from  ww  w . j av  a 2 s.com*/

            try (InputStream audioStream = this.storageService.getAudioStream(t.getAudioStorageKey())) {
                IOUtils.copy(audioStream, zos);
            }
        }
    }

    return zipFile;
}

From source file:com.scorpio4.util.io.StreamCopy.java

public void process(InputStream input, OutputStream output) throws IOException {
    IOUtils.copy(input, output);
    output.flush();
}

From source file:com.rabbitstewdio.build.maven.tomcat.AbstractContentServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    String filename = request.getPathInfo();

    InputStream in = findResource(filename);
    if (in == null) {
        log.warn("Unable to find [" + filename + "] on " + getServletName());
        response.setStatus(404);/*from  ww w.j a  v a  2s.c o m*/
        return;
    }

    ByteArrayOutputStream bout = new ByteArrayOutputStream();
    IOUtils.copy(in, bout);

    response.setCharacterEncoding("UTF-8");

    String mimeType = getServletContext().getMimeType(filename);
    if (mimeType != null) {
        response.setContentType(mimeType);
    }

    response.addDateHeader("Expires", 0L);
    response.setDateHeader(LAST_MODIFIED, new Date().getTime());
    response.setContentLength(bout.size());
    response.getOutputStream().write(bout.toByteArray());
    response.flushBuffer();
}

From source file:com.thoughtworks.go.util.TestUtils.java

public static void copyAndClose(InputStream fileInputStream, OutputStream fileOutputStream) {
    try (InputStream is = fileInputStream; OutputStream os = fileOutputStream) {
        IOUtils.copy(is, os);
    } catch (IOException e) {
        bomb(e);// w w  w .  ja v  a 2  s .  com
    }
}

From source file:com.opengamma.util.ZipUtils.java

/**
 * Unzips a ZIP archive./*  w ww .  ja  va 2  s . c  o  m*/
 * 
 * @param zipFile  the archive file, not null
 * @param outputDir  the output directory, not null
 */
public static void unzipArchive(final ZipFile zipFile, final File outputDir) {
    ArgumentChecker.notNull(zipFile, "zipFile");
    ArgumentChecker.notNull(outputDir, "outputDir");

    try {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry entry = entries.nextElement();
            if (entry.isDirectory()) {
                FileUtils.forceMkdir(new File(outputDir, entry.getName()));
                continue;
            }
            File entryDestination = new File(outputDir, entry.getName());
            entryDestination.getParentFile().mkdirs();
            InputStream in = zipFile.getInputStream(entry);
            OutputStream out = new FileOutputStream(entryDestination);
            IOUtils.copy(in, out);
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(out);
        }
        zipFile.close();
    } catch (IOException ex) {
        throw new OpenGammaRuntimeException(
                "Error while extracting file: " + zipFile.getName() + " to: " + outputDir, ex);
    }
}

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

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

From source file:de.drop_converter.plugins.binary_convert.Base642Data.java

@Override
public boolean importData(TransferSupport support) throws ConverterException {
    try {/*from www . j a  v a 2s  . c  o m*/
        if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            List<File> files = (List<File>) support.getTransferable()
                    .getTransferData(DataFlavor.javaFileListFlavor);
            for (File file : files) {
                FileInputStream fis = null;
                Base64InputStream base64IS = null;
                OutputStream out = null;
                try {
                    fis = new FileInputStream(file);
                    base64IS = new Base64InputStream(fis, false);
                    out = getOutputStream(file, ".bin");
                    IOUtils.copy(base64IS, out);
                } catch (Exception e) {
                    throw new ConverterException(e);
                } finally {
                    IOUtils.closeQuietly(out);
                    IOUtils.closeQuietly(base64IS);
                    IOUtils.closeQuietly(fis);
                }
            }
            return true;
        } else if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            OutputStream out = null;
            try {
                byte[] encode = new Base64().decode(data.getBytes());
                out = getOutputStream(null, ".bin");
                out.write(encode);
            } catch (Exception e) {
                throw new ConverterException(e);
            } finally {
                IOUtils.closeQuietly(out);
            }
        }
    } catch (Exception e) {
        throw new ConverterException(e);
    }

    return false;
}

From source file:de.drop_converter.plugins.binary_convert.Data2Base64.java

@Override
public boolean importData(TransferSupport support) throws ConverterException {
    try {//from   w  ww. j  a  v a  2 s  . c  o m
        if (support.isDataFlavorSupported(DataFlavor.javaFileListFlavor)) {
            List<File> files = (List<File>) support.getTransferable()
                    .getTransferData(DataFlavor.javaFileListFlavor);
            for (File file : files) {
                FileInputStream fis = null;
                Base64InputStream base64IS = null;
                OutputStream out = null;
                try {
                    fis = new FileInputStream(file);
                    base64IS = new Base64InputStream(fis, true);
                    out = getOutputStream(file, ".base64");
                    IOUtils.copy(base64IS, out);
                } catch (Exception e) {
                    throw new ConverterException(e);
                } finally {
                    IOUtils.closeQuietly(out);
                    IOUtils.closeQuietly(base64IS);
                    IOUtils.closeQuietly(fis);
                }
            }
            return true;
        } else if (support.isDataFlavorSupported(DataFlavor.stringFlavor)) {
            String data = (String) support.getTransferable().getTransferData(DataFlavor.stringFlavor);
            OutputStream out = null;
            try {
                byte[] encode = new Base64().encode(data.getBytes());
                out = getOutputStream(null, ".base64");
                out.write(encode);
            } catch (Exception e) {
                throw new ConverterException(e);
            } finally {
                IOUtils.closeQuietly(out);
            }

        }
    } catch (Exception e) {
        throw new ConverterException(e);
    }

    return false;
}

From source file:codes.thischwa.c5c.requestcycle.response.mode.Download.java

@Override
@JsonIgnore/*from   w  ww. j a v a  2 s. c o m*/
public void write(HttpServletResponse resp) throws IOException {
    resp.setHeader("Content-Type", "application/x-download");
    resp.setHeader("Content-Transfer-Encoding", "Binary");
    resp.setHeader("Content-Length", String.valueOf(contentLength));
    resp.setHeader("Content-Disposition",
            String.format("attachment; filename=\"%s\"", FilenameUtils.getName(fullPath)));
    IOUtils.copy(in, resp.getOutputStream());
}