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:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java

protected File createFile(File typeDir, FileItem item) throws IOException, FileNotFoundException {
    File f = new File(typeDir, item.getName());
    FileOutputStream fos = null;//from   ww  w  .ja va2  s. c  o m
    InputStream is = item.getInputStream();
    try {
        fos = new FileOutputStream(f);
        IOUtils.copyLarge(is, fos);
        return f;
    } finally {
        is.close();
        if (null != fos)
            fos.close();
    }
}

From source file:com.dotmarketing.portlets.cmsmaintenance.ajax.IndexAjaxAction.java

public void downloadIndex(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, DotDataException {
    Map<String, String> map = getURIParams();
    response.setContentType("application/zip");

    String indexName = getIndexNameOrAlias(map);

    if (!UtilMethods.isSet(indexName))
        return;/*from www. ja  va2s  .  c  om*/

    if (indexName.equalsIgnoreCase("live") || indexName.equalsIgnoreCase("working")) {
        IndiciesInfo info = APILocator.getIndiciesAPI().loadIndicies();
        if (indexName.equalsIgnoreCase("live")) {
            indexName = info.live;
        }
        if (indexName.equalsIgnoreCase("working")) {
            indexName = info.working;
        }
    }

    File f = APILocator.getESIndexAPI().backupIndex(indexName);
    response.setContentLength((int) f.length());
    OutputStream out = response.getOutputStream();
    InputStream in = new FileInputStream(f);

    response.setHeader("Content-Type", "application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=" + indexName + ".zip");

    IOUtils.copyLarge(in, out);

    f.delete();
    return;
}

From source file:com.carolinarollergirls.scoreboard.jetty.MediaServlet.java

protected void processZipFileItem(FileItemFactory factory, FileItem zip, List<FileItem> fileItems)
        throws IOException {
    ZipInputStream ziS = new ZipInputStream(zip.getInputStream());
    ZipEntry zE;/*from   w  w w.j a va  2  s. co m*/
    try {
        while (null != (zE = ziS.getNextEntry())) {
            if (zE.isDirectory() || !uploadFileNameFilter.accept(null, zE.getName()))
                continue;
            FileItem item = factory.createItem(null, null, false, zE.getName());
            OutputStream oS = item.getOutputStream();
            IOUtils.copyLarge(ziS, oS);
            oS.close();
            fileItems.add(item);
        }
    } finally {
        ziS.close();
    }
}

From source file:com.fujitsu.dc.core.bar.BarFileInstaller.java

/**
 * Http?bar??????.// w ww .  ja v  a2 s.  co  m
 * @param inStream HttpInputStream
 * @return ????bar?File
 */
private File storeTemporaryBarFile(InputStream inStream) {

    // bar?????????
    String unitUserName = BarFileUtils.getUnitUserName(this.cell.getOwner());
    File barFileDir = new File(new File(barTempDir, unitUserName), "bar");
    if (!barFileDir.exists() && !barFileDir.mkdirs()) {
        String message = "unable create directory: " + barFileDir.getAbsolutePath();
        throw DcCoreException.Server.FILE_SYSTEM_ERROR.params(message);
    }

    // barNFS???
    String prefix = this.cell.getId() + "_" + this.boxName;
    File barFile = null;
    OutputStream outStream = null;
    try {
        barFile = File.createTempFile(prefix, ".bar", barFileDir);
        barFile.deleteOnExit(); // VM??
        outStream = new FileOutputStream(barFile);
        IOUtils.copyLarge(inStream, outStream);
    } catch (IOException e) {
        String message = "unable save bar file: %s";
        if (barFile == null) {
            message = String.format(message, barFileDir + prefix + "XXX.bar");
        } else {
            message = String.format(message, barFile.getAbsolutePath());
        }
        throw DcCoreException.Server.FILE_SYSTEM_ERROR.params(message);
    } finally {
        if (null != outStream && DcCoreConfig.getFsyncEnabled()) {
            try {
                sync(((FileOutputStream) outStream).getFD());
            } catch (Exception e) {
                throw DcCoreException.Server.FILE_SYSTEM_ERROR.params(e.getMessage());
            }
        }
        IOUtils.closeQuietly(outStream);
    }
    return barFile;
}

From source file:io.personium.core.bar.BarFileInstaller.java

/**
 * Http?bar??????.//from   ww w. jav a2s .c om
 * @param inStream HttpInputStream
 * @return ????bar?File
 */
private File storeTemporaryBarFile(InputStream inStream) {

    // bar?????????
    String unitUserName = BarFileUtils.getUnitUserName(this.cell.getOwner());
    File barFileDir = new File(new File(barTempDir, unitUserName), "bar");
    if (!barFileDir.exists() && !barFileDir.mkdirs()) {
        String message = "unable create directory: " + barFileDir.getAbsolutePath();
        throw PersoniumCoreException.Server.FILE_SYSTEM_ERROR.params(message);
    }

    // barNFS???
    String prefix = this.cell.getId() + "_" + this.boxName;
    File barFile = null;
    OutputStream outStream = null;
    try {
        barFile = File.createTempFile(prefix, ".bar", barFileDir);
        barFile.deleteOnExit(); // VM??
        outStream = new FileOutputStream(barFile);
        IOUtils.copyLarge(inStream, outStream);
    } catch (IOException e) {
        String message = "unable save bar file: %s";
        if (barFile == null) {
            message = String.format(message, barFileDir + prefix + "XXX.bar");
        } else {
            message = String.format(message, barFile.getAbsolutePath());
        }
        throw PersoniumCoreException.Server.FILE_SYSTEM_ERROR.params(message);
    } finally {
        if (null != outStream && PersoniumUnitConfig.getFsyncEnabled()) {
            try {
                sync(((FileOutputStream) outStream).getFD());
            } catch (Exception e) {
                throw PersoniumCoreException.Server.FILE_SYSTEM_ERROR.params(e.getMessage());
            }
        }
        IOUtils.closeQuietly(outStream);
    }
    return barFile;
}

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

private void extractCatalogFiles(final File packageSourceFile) throws IOException {
    final File tempDirectory = packageSourceFile.getParentFile();

    // 1) extract TAR
    final File packageTarFile = File.createTempFile("rsb-install.", ".tar", tempDirectory);
    final GzipCompressorInputStream gzIn = new GzipCompressorInputStream(
            new FileInputStream(packageSourceFile));
    FileOutputStream output = new FileOutputStream(packageTarFile);
    IOUtils.copyLarge(gzIn, output);
    IOUtils.closeQuietly(output);/*  ww w  . j a va  2 s . co m*/
    IOUtils.closeQuietly(gzIn);

    // 2) parse TAR and drop files in catalog
    final TarArchiveInputStream tarIn = new TarArchiveInputStream(new FileInputStream(packageTarFile));
    TarArchiveEntry tarEntry = null;
    while ((tarEntry = tarIn.getNextTarEntry()) != null) {
        if (!tarEntry.isFile()) {
            continue;
        }

        final Matcher matcher = TAR_CATALOG_FILE_PATTERN.matcher(tarEntry.getName());
        if (matcher.matches()) {
            final byte[] data = IOUtils.toByteArray(tarIn, tarEntry.getSize());

            final String catalogFile = matcher.group(1);
            final File targetCatalogFile = new File(getConfiguration().getCatalogRootDirectory(), catalogFile);
            output = new FileOutputStream(targetCatalogFile);
            IOUtils.write(data, output);
            IOUtils.closeQuietly(output);

            getLogger().info("Wrote " + data.length + " bytes in catalog file: " + targetCatalogFile);
        }
    }
    IOUtils.closeQuietly(tarIn);
}

From source file:com.gitblit.manager.FilestoreManager.java

@Override
public FilestoreModel.Status downloadBlob(String oid, UserModel user, RepositoryModel repo,
        OutputStream streamOut) {

    //Access control and object logic
    Status status = canGetObject(oid, user, repo);

    if (status != Status.Available) {
        return status;
    }/*  ww w  .jav  a  2  s.  com*/

    FilestoreModel item = fileCache.get(oid);

    if (streamOut != null) {
        try (FileInputStream streamIn = new FileInputStream(getStoragePath(oid))) {

            IOUtils.copyLarge(streamIn, streamOut);

            streamOut.flush();
            streamIn.close();
        } catch (EOFException e) {
            logger.error(MessageFormat.format("Client aborted connection for {0}", oid), e);
            return Status.Error_Unexpected_Stream_End;
        } catch (Exception e) {
            logger.error(MessageFormat.format("Failed to download blob {0}", oid), e);
            return Status.Error_Unknown;
        }
    }

    return item.getStatus();
}

From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java

private static void copyStream(InputStream input, OutputStream output) throws IOException {
    IOUtils.copyLarge(input, output);
}

From source file:net.siegmar.japtproxy.misc.IOHandler.java

/**
 * This method is responsible for fetching remote data (if needed) and
 * sending the data (locally stored, or remotely fetched) to the client.
 *
 * @param requestedData  the requested data
 * @param poolObject     the pool object
 * @param targetResource the remote resource link
 * @param res            the HttpServletResponse object
 * @return true if the file was sent from cache, false otherwise
 * @throws IOException is thrown if a problem occured while sending data
 * @throws net.siegmar.japtproxy.exception.ResourceUnavailableException is thrown if the resource was not found
 *//*from  ww w  . j ava 2 s  . com*/
public boolean sendAndSave(final RequestedData requestedData, final PoolObject poolObject,
        final URL targetResource, final HttpServletResponse res)
        throws IOException, ResourceUnavailableException, InitializationException {
    final String lockIdentifier = requestedData.getRequestedResource();
    final ReadWriteLock lock = ResourceLock.obtainLocker(lockIdentifier);
    final Lock readLock = lock.readLock();

    LockStatus lockStatus;

    readLock.lock();
    lockStatus = LockStatus.READ;

    LOG.debug("Obtained readLock for '{}'", lockIdentifier);

    final long poolModification = poolObject.getLastModified();

    FetchedResource fetchedResource = null;
    OutputStream saveOs = null;
    InputStream is = null;

    try {
        if (poolModification != 0) {
            if (!isNewVersionCheckRequired(poolObject, requestedData.getRequestedResource())) {
                LOG.debug("Local object exists and no need to do a version check - sending local object");
                sendLocalFile(poolObject, requestedData.getRequestModifiedSince(), res);
                return true;
            }

            LOG.debug("Local object exists but new version check is required");
        } else {
            LOG.debug("No local object exists - requesting remote host");
        }

        // Get a fetcher (http, ftp) for the current targetResource
        final Fetcher fetcher = fetcherPool.getInstance(targetResource);

        if (fetcher == null) {
            throw new InitializationException("No fetcher found for resource '" + targetResource + "'");
        }

        fetchedResource = fetcher.fetch(targetResource, poolModification, requestedData.getUserAgent());

        final String contentType = fetchedResource.getContentType();
        final long remoteModification = fetchedResource.getLastModified();
        final long contentLength = fetchedResource.getContentLength();

        if (remoteModification != 0 && poolModification > remoteModification) {
            LOG.warn(
                    "Remote object is older than local pool object "
                            + "(Remote timestamp: {} - Local timestamp: {}). "
                            + "Object won't get updated! Check this manually!",
                    Util.getSimpleDateFromTimestamp(remoteModification),
                    Util.getSimpleDateFromTimestamp(poolModification));
        }

        setHeader(res, fetchedResource);

        if (!fetchedResource.isModified()) {
            LOG.debug("Remote resource has no new version - sending local object");
            sendLocalFile(poolObject, requestedData.getRequestModifiedSince(), res);
            return true;
        }

        if (LOG.isDebugEnabled()) {
            if (poolModification != 0) {
                // Pool file exists, but it is out of date
                LOG.debug(
                        "Newer version found (old Last-Modified: {}) - Request '{}', Last-Modified: {}, "
                                + "Content-Type: {}, Content-Length: {}",
                        Util.getSimpleDateFromTimestamp(poolModification), targetResource,
                        Util.getSimpleDateFromTimestamp(remoteModification), contentType, contentLength);
            } else {
                // No pool file exists
                LOG.debug("Request '{}', Last-Modified: {}, Content-Type: {}, Content-Length: {}",
                        targetResource, Util.getSimpleDateFromTimestamp(remoteModification), contentType,
                        contentLength);
            }
        }

        readLock.unlock();
        lock.writeLock().lock();
        lockStatus = LockStatus.WRITE;

        LOG.debug("Obtained writeLock for '{}'", lockIdentifier);

        is = fetchedResource.getInputStream();

        LOG.info("Sending remote object '{}'", poolObject.getName());

        saveOs = new TeeOutputStream(poolObject.getOutputStream(), res.getOutputStream());
        final long bytesCopied = IOUtils.copyLarge(is, saveOs);

        LOG.debug("Data sent to file and client");

        poolObject.setLastModified(remoteModification);

        if (contentLength != -1 && bytesCopied != contentLength) {
            throw new IOException(
                    String.format("Received file has invalid file size - " + "only %d of %d were downloaded",
                            bytesCopied, contentLength));
        }

        poolObject.store();

        return false;
    } catch (final IOException e) {
        // Remove pool file if it was created by this thread
        if (poolModification == 0) {
            poolObject.remove();
        }

        throw e;
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(saveOs);

        if (fetchedResource != null) {
            fetchedResource.close();
        }

        if (lockStatus == LockStatus.WRITE) {
            LOG.debug("Released writeLock for '{}'", lockIdentifier);
            lock.writeLock().unlock();
        } else {
            LOG.debug("Released readLock for '{}'", lockIdentifier);
            readLock.unlock();
        }
        ResourceLock.releaseLocker(lockIdentifier);
    }
}

From source file:com.elasticgrid.examples.video.VideoConverterJSB.java

private File download(String key) throws IOException, S3ServiceException {
    logger.log(Level.FINE, "Downloading from bucket ''{0}'' file ''{1}''...",
            new Object[] { srcBucketName, key });
    downloadWatch.startTiming();//from  ww  w .j  ava  2s  . co m
    S3Bucket bucket = s3.createBucket(new S3Bucket(srcBucketName));
    S3Object object;
    FileOutputStream fileStream = null;
    InputStream s3Stream = null;
    try {
        object = s3.getObject(bucket, key);
        File tmpFile = new File(temporaryStorageLocation, key);
        long start = System.currentTimeMillis();
        s3Stream = object.getDataInputStream();
        fileStream = new FileOutputStream(tmpFile);
        IOUtils.copyLarge(s3Stream, fileStream);
        long end = System.currentTimeMillis();
        logger.log(Level.INFO, "Downloaded from bucket ''{0}'' file ''{1}'' in {2}s...",
                new Object[] { srcBucketName, key, ((end - start) / 1000) });
        return tmpFile;
    } catch (S3ServiceException e) {
        if (!"NoSuchKey".equals(e.getS3ErrorCode())) {
            throw e;
        } else {
            logger.log(Level.WARNING, "No such video: {0}. Skipping it.", e.getS3ErrorCode());
            return null;
        }
    } finally {
        IOUtils.closeQuietly(s3Stream);
        IOUtils.closeQuietly(fileStream);
        downloadWatch.stopTiming();
    }
}