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

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

Introduction

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

Prototype

public static long copyLarge(Reader input, Writer output) throws IOException 

Source Link

Document

Copy chars from a large (over 2GB) Reader to a Writer.

Usage

From source file:de.uni_luebeck.inb.knowarc.usecases.invocation.UseCaseInvocation.java

@SuppressWarnings("unchecked")
public void setInput(String inputName, ReferenceService referenceService, T2Reference t2Reference)
        throws InvocationException {
    if (t2Reference == null) {
        throw new InvocationException("No input specified for " + inputName);
    }/*from  www.  j  a v a2 s .  c  o  m*/
    ScriptInputUser input = (ScriptInputUser) usecase.getInputs().get(inputName);
    if (input.isList()) {
        IdentifiedList<T2Reference> listOfReferences = (IdentifiedList<T2Reference>) referenceService
                .getListService().getList(t2Reference);

        if (!input.isConcatenate()) {
            // this is a list input (not concatenated)
            // so write every element to its own temporary file
            // and create a filelist file

            // we need to write the list elements to temporary files
            ScriptInputUser listElementTemp = new ScriptInputUser();
            listElementTemp.setBinary(input.isBinary());
            listElementTemp.setTempFile(true);

            String lineEndChar = "\n";
            if (!input.isFile() && !input.isTempFile()) {
                lineEndChar = " ";
            }

            String listFileContent = "";
            String filenamesFileContent = "";
            // create a list of all temp file names
            for (T2Reference cur : listOfReferences) {
                String tmp = setOneInput(referenceService, cur, listElementTemp);
                listFileContent += tmp + lineEndChar;
                int ind = tmp.lastIndexOf('/');
                if (ind == -1) {
                    ind = tmp.lastIndexOf('\\');
                }
                if (ind != -1) {
                    tmp = tmp.substring(ind + 1);
                }
                filenamesFileContent += tmp + lineEndChar;
            }

            // how do we want the listfile to be stored?
            ScriptInputUser listFile = new ScriptInputUser();
            listFile.setBinary(false); // since its a list file
            listFile.setFile(input.isFile());
            listFile.setTempFile(input.isTempFile());
            listFile.setTag(input.getTag());
            T2Reference listFileContentReference = referenceService.register(listFileContent, 0, true,
                    invocationContext);

            tags.put(listFile.getTag(), setOneInput(referenceService, listFileContentReference, listFile));

            listFile.setTag(input.getTag() + "_NAMES");
            T2Reference filenamesFileContentReference = referenceService.register(filenamesFileContent, 0, true,
                    null);
            tags.put(listFile.getTag(), setOneInput(referenceService, filenamesFileContentReference, listFile));
        } else {
            try {
                // first, concatenate all data
                if (input.isBinary()) {
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    BufferedWriter outputWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
                    for (T2Reference cur : listOfReferences) {
                        InputStreamReader inputReader = new InputStreamReader(
                                getAsStream(referenceService, cur));
                        IOUtils.copyLarge(inputReader, outputWriter);
                        inputReader.close();
                    }
                    outputWriter.close();
                    T2Reference binaryReference = referenceService.register(outputStream.toByteArray(), 0, true,
                            invocationContext);
                    tags.put(input.getTag(), setOneInput(referenceService, binaryReference, input));
                } else {
                    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
                    BufferedWriter outputWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
                    for (T2Reference cur : listOfReferences) {
                        InputStreamReader inputReader = new InputStreamReader(
                                getAsStream(referenceService, cur));
                        IOUtils.copyLarge(inputReader, outputWriter);
                        outputWriter.write(" ");
                        inputReader.close();
                    }
                    outputWriter.close();
                    T2Reference binaryReference = referenceService.register(outputStream.toByteArray(), 0, true,
                            invocationContext);
                    tags.put(input.getTag(), setOneInput(referenceService, binaryReference, input));
                }
            } catch (IOException e) {
                throw new InvocationException(e);
            }
        }
    } else {
        tags.put(input.getTag(), setOneInput(referenceService, t2Reference, input));
    }
}

From source file:com.linkedin.pinot.common.utils.FileUploadUtils.java

public static long getFile(String url, File file) throws Exception {
    GetMethod httpget = null;/* w  w w .  j  a  va  2s. c o  m*/
    try {
        httpget = new GetMethod(url);
        int responseCode = FILE_UPLOAD_HTTP_CLIENT.executeMethod(httpget);
        if (responseCode >= 400) {
            long contentLength = httpget.getResponseContentLength();
            if (contentLength > 0) {
                InputStream responseBodyAsStream = httpget.getResponseBodyAsStream();
                // don't read more than 1000 bytes
                byte[] buffer = new byte[(int) Math.min(contentLength, 1000)];
                responseBodyAsStream.read(buffer);
                LOGGER.error("Error response from url:{} \n {}", url, new String(buffer));
            }
            throw new RuntimeException("Received error response from server while downloading file. url:" + url
                    + " response code:" + responseCode);
        } else {
            long ret = httpget.getResponseContentLength();
            BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
            IOUtils.copyLarge(httpget.getResponseBodyAsStream(), output);
            IOUtils.closeQuietly(output);
            return ret;
        }
    } catch (Exception ex) {
        LOGGER.error("Caught exception", ex);
        throw ex;
    } finally {
        if (httpget != null) {
            httpget.releaseConnection();
        }
    }
}

From source file:com.manydesigns.portofino.pageactions.text.TextAction.java

/**
 * Saves the content of the page to a file (see {@link TextAction#locateTextFile()}).
 * @throws IOException if there's a problem saving the content.
 *//*from www.  j  av  a 2 s .c om*/
protected void saveContent() throws IOException {
    if (content == null) {
        content = EMPTY_STRING;
    }
    content = processContentBeforeSave(content);
    byte[] contentByteArray = content.getBytes(CONTENT_ENCODING);
    textFile = locateTextFile();

    // copy the data
    FileOutputStream fileOutputStream = new FileOutputStream(textFile);
    try {
        long size = IOUtils.copyLarge(new ByteArrayInputStream(contentByteArray), fileOutputStream);
    } catch (IOException e) {
        logger.error("Could not save content", e);
        throw e;
    } finally {
        fileOutputStream.close();
    }
    logger.info("Content saved to: {}", textFile.getAbsolutePath());
}

From source file:com.thinkbiganalytics.discovery.parsers.hadoop.SparkFileSchemaParserService.java

private File toFile(InputStream is) throws IOException {
    File tempFile = File.createTempFile("kylo-spark-parser", ".dat");
    try (FileOutputStream fos = new FileOutputStream(tempFile)) {
        IOUtils.copyLarge(is, fos);
    }/*  ww w.j  ava 2  s.  c o  m*/
    log.info("Created temporary file {} success? {}", tempFile.getAbsoluteFile().toURI(), tempFile.exists());
    return tempFile;
}

From source file:edu.isi.wings.portal.classes.StorageHandler.java

public void run() {
    try {
        IOUtils.copyLarge(pis, os);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:autoupdater.FileDAO.java

/**
 * Unzips a zip archive.//  w w  w. j  a v a  2  s  .c om
 *
 * @param zip the zipfile to unzip
 * @param fileLocationOnDiskToDownloadTo the folder to unzip in
 * @return true if successful
 * @throws IOException
 */
public boolean unzipFile(ZipFile zip, File fileLocationOnDiskToDownloadTo) throws IOException {
    FileOutputStream dest = null;
    InputStream inStream = null;
    Enumeration<ZipArchiveEntry> zipFileEnum = zip.getEntries();
    while (zipFileEnum.hasMoreElements()) {
        ZipArchiveEntry entry = zipFileEnum.nextElement();
        File destFile = new File(fileLocationOnDiskToDownloadTo, entry.getName());
        if (!destFile.getParentFile().exists()) {
            if (!destFile.getParentFile().mkdirs()) {
                throw new IOException("could not create the folders to unzip in");
            }
        }
        if (!entry.isDirectory()) {
            try {
                dest = new FileOutputStream(destFile);
                inStream = zip.getInputStream(entry);
                IOUtils.copyLarge(inStream, dest);
            } finally {
                if (dest != null) {
                    dest.close();
                }
                if (inStream != null) {
                    inStream.close();
                }
            }
        } else {
            if (!destFile.exists()) {
                if (!destFile.mkdirs()) {
                    throw new IOException("could not create folders to unzip file");
                }
            }
        }
    }
    zip.close();
    return true;
}

From source file:com.alexkli.jhb.Worker.java

private void consumeAndCloseStream(InputStream responseStream) throws IOException {
    try (InputStream ignored = responseStream) {
        IOUtils.copyLarge(responseStream, new OutputStream() {
            @Override//from   w w  w  .  j av  a 2s .  com
            public void write(int b) throws IOException {
                // discard
            }

            @Override
            public void write(byte[] b) throws IOException {
                // discard
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                // discard
            }
        });
    }
}

From source file:eu.openanalytics.rsb.component.AdminResource.java

@Path("/" + SYSTEM_SUBPATH + "/r_packages")
@POST// w  ww . ja v a  2 s.  c o  m
@Consumes({ Constants.GZIP_CONTENT_TYPE })
public void installRPackage(@QueryParam("rServiPoolUri") final String rServiPoolUri,
        @QueryParam("sha1hexsum") final String sha1HexSum, @QueryParam("packageName") final String packageName,
        final InputStream input) throws Exception {
    Validate.notBlank(rServiPoolUri, "missing query param: rServiPoolUri");
    Validate.notBlank(sha1HexSum, "missing query param: sha1hexsum");

    // store the package and tar files in temporary files
    final File tempDirectory = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString());
    FileUtils.forceMkdir(tempDirectory);

    final File packageSourceFile = new File(tempDirectory, packageName);

    try {
        final FileOutputStream output = new FileOutputStream(packageSourceFile);
        IOUtils.copyLarge(input, output);
        IOUtils.closeQuietly(output);

        // validate the checksum
        final FileInputStream packageSourceInputStream = new FileInputStream(packageSourceFile);
        final String calculatedSha1HexSum = DigestUtils.sha1Hex(packageSourceInputStream);
        IOUtils.closeQuietly(packageSourceInputStream);
        Validate.isTrue(calculatedSha1HexSum.equals(sha1HexSum), "Invalid SHA-1 HEX checksum");

        // upload to RServi
        rServiPackageManager.install(packageSourceFile, rServiPoolUri);

        // extract catalog files from $PKG_ROOT/inst/rsb/catalog
        extractCatalogFiles(packageSourceFile);

        getLogger().info("Package with checksum " + sha1HexSum + " installed to " + rServiPoolUri);
    } finally {
        try {
            FileUtils.forceDelete(tempDirectory);
        } catch (final Exception e) {
            getLogger().warn("Failed to delete temporary directory: " + tempDirectory, e);
        }
    }
}

From source file:com.sangupta.httpd.HttpdHandler.java

/**
 * Send file contents back to client/*from   w ww.  ja va2s  .  co m*/
 * 
 * @param request
 * @param response
 * @param uri
 * @throws IOException
 */
private void sendFileContents(HttpServletRequest request, HttpServletResponse response, String uri)
        throws IOException {
    File file = new File(documentRoot, uri);
    if (!file.exists() || file.isHidden()) {
        response.sendError(HttpStatusCode.NOT_FOUND);
        return;
    }

    if (file.isDirectory()) {
        response.sendRedirect("/" + uri + "/");
        return;
    }

    if (!file.isFile()) {
        response.sendError(HttpStatusCode.FORBIDDEN);
        return;
    }

    String etag = null;
    if (!this.httpdConfig.noEtag) {
        // compute the weak ETAG based on file time and size
        final long time = file.lastModified();
        final long size = file.length();
        final String name = file.getName();

        etag = "w/" + HashUtils.getMD5Hex(name + ":" + size + ":" + time);
    }

    // check for if-modified-since header name
    String ifModifiedSince = request.getHeader("If-Modified-Since");
    if (ifModifiedSince != null && !ifModifiedSince.isEmpty()) {
        Date ifModifiedSinceDate = parseDateHeader(ifModifiedSince);

        if (ifModifiedSinceDate != null) {
            // Only compare up to the second because the datetime format we send to the client
            // does not have milliseconds
            long ifModifiedSinceDateSeconds = ifModifiedSinceDate.getTime() / 1000;
            long fileLastModifiedSeconds = file.lastModified() / 1000;

            if (ifModifiedSinceDateSeconds == fileLastModifiedSeconds) {
                response.setStatus(HttpStatusCode.NOT_MODIFIED);
                return;
            }
        }
    }

    // add mime-header - based on the file extension
    response.setContentType(MimeUtils.getMimeTypeForFileExtension(FilenameUtils.getExtension(file.getName())));
    response.setDateHeader("Last-Modified", file.lastModified());

    // check for no cache
    if (this.httpdConfig.noCache) {
        response.addHeader("Cache-Control", "no-store, no-cache, must-revalidate");
    }

    // check for no-sniff
    if (this.httpdConfig.noSniff) {
        response.addHeader("X-Content-Type-Options", "nosniff");
    }

    // etag
    if (!this.httpdConfig.noEtag) {
        response.addHeader("Etag", etag);
    }

    // send back file contents
    response.setContentLength((int) file.length());
    IOUtils.copyLarge(FileUtils.openInputStream(file), response.getOutputStream());
}

From source file:com.github.jinahya.simple.file.back.LocalFileBackTest.java

@Test(enabled = true, invocationCount = 1)
public void write() throws IOException, FileBackException {

    fileContext.fileOperationSupplier(() -> FileOperation.WRITE);

    final ByteBuffer fileKey = randomFileKey();
    fileContext.targetKeySupplier(() -> fileKey);

    final Path leafPath = LocalFileBack.leafPath(rootPath, fileKey, true);

    final byte[] fileBytes = randomFileBytes();
    final boolean fileWritten = Files.isRegularFile(leafPath) || current().nextBoolean();
    if (fileWritten) {
        Files.write(leafPath, fileBytes, StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE);
        logger.trace("file written");
    }//  w  w  w  .j  av  a2 s.co  m

    fileContext.sourceChannelSupplier(() -> Channels.newChannel(new ByteArrayInputStream(fileBytes)));

    fileContext.targetChannelConsumer(v -> {
        try {
            final long copied = IOUtils.copyLarge(new ByteArrayInputStream(fileBytes),
                    Channels.newOutputStream(v));
        } catch (final IOException ioe) {
            logger.error("failed to copy", ioe);
        }
    });

    fileBack.operate(fileContext);

    if (fileWritten) {
        final byte[] actual = Files.readAllBytes(leafPath);
        assertEquals(actual, fileBytes);
    }
}