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:org.eclipse.packagedrone.utils.deb.build.DebianPackageWriter.java

@Override
public void addFile(final ContentProvider contentProvider, String fileName, EntryInformation entryInformation)
        throws IOException {
    if (entryInformation == null) {
        entryInformation = EntryInformation.DEFAULT_FILE;
    }/*from  w  w  w. j a va2  s.  c om*/

    try {
        fileName = cleanupPath(fileName);

        if (entryInformation.isConfigurationFile()) {
            this.confFiles.add(fileName.substring(1)); // without the leading dot
        }

        final TarArchiveEntry entry = new TarArchiveEntry(fileName);
        entry.setSize(contentProvider.getSize());
        applyInfo(entry, entryInformation);

        checkCreateParents(fileName);

        this.dataStream.putArchiveEntry(entry);

        final Map<String, byte[]> results = new HashMap<>();
        try (final ChecksumInputStream in = new ChecksumInputStream(contentProvider.createInputStream(),
                results, MessageDigest.getInstance("MD5"))) {
            this.installedSize += IOUtils.copyLarge(in, this.dataStream);
        }

        this.dataStream.closeArchiveEntry();

        // record the checksum
        recordChecksum(fileName, results.get("MD5"));
    } catch (final Exception e) {
        throw new IOException(e);
    }
}

From source file:org.efaps.webdav4vfs.handler.GetHandler.java

@Override()
public void service(final HttpServletRequest _request, final HttpServletResponse _response) throws IOException {
    FileObject object = VFSBackend.resolveFile(_request.getPathInfo());

    if (object.exists()) {
        if (FileType.FOLDER.equals(object.getType())) {
            _response.sendError(HttpServletResponse.SC_FORBIDDEN);
            return;
        }//ww w  .  ja v a  2  s . co  m

        setHeader(_response, object.getContent());

        final InputStream is = object.getContent().getInputStream();
        final OutputStream os = _response.getOutputStream();
        IOUtils.copyLarge(is, os);
        is.close();
    } else {
        _response.sendError(HttpServletResponse.SC_NOT_FOUND);
    }
}

From source file:org.efaps.webdav4vfs.handler.PutHandler.java

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    FileObject object = VFSBackend.resolveFile(request.getPathInfo());

    try {//  w  ww  . j  a  va2s .c  om
        if (!LockManager.getInstance().evaluateCondition(object, getIf(request)).result) {
            response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
            return;
        }
    } catch (LockException e) {
        response.sendError(SC_LOCKED);
        return;
    } catch (ParseException e) {
        response.sendError(HttpServletResponse.SC_PRECONDITION_FAILED);
        return;
    }
    // it is forbidden to write data on a folder
    if (object.exists() && FileType.FOLDER.equals(object.getType())) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    FileObject parent = object.getParent();
    if (!parent.exists()) {
        response.sendError(HttpServletResponse.SC_FORBIDDEN);
        return;
    }

    if (!FileType.FOLDER.equals(parent.getType())) {
        response.sendError(HttpServletResponse.SC_CONFLICT);
        return;
    }

    InputStream is = request.getInputStream();
    OutputStream os = object.getContent().getOutputStream();
    long bytesCopied = IOUtils.copyLarge(is, os);
    String contentLengthHeader = request.getHeader("Content-length");
    LOG.debug(String.format("sent %d/%s bytes", bytesCopied,
            contentLengthHeader == null ? "unknown" : contentLengthHeader));
    os.flush();
    object.close();

    response.setStatus(HttpServletResponse.SC_CREATED);
}

From source file:org.exist.mongodb.xquery.gridfs.Stream.java

/**
 * Stream document to HTTP agent//from  www.  j a  v a2  s . c  o m
 */
void stream(GridFSDBFile gfsFile, String documentId, Boolean setDisposition)
        throws IOException, XPathException {
    if (gfsFile == null) {
        throw new XPathException(this, GridfsModule.GRFS0004,
                String.format("Document '%s' could not be found.", documentId));
    }

    DBObject metadata = gfsFile.getMetaData();

    // Determine actual size
    String compression = (metadata == null) ? null : (String) metadata.get(EXIST_COMPRESSION);
    Long originalSize = (metadata == null) ? null : (Long) metadata.get(EXIST_ORIGINAL_SIZE);

    long length = gfsFile.getLength();
    if (originalSize != null) {
        length = originalSize;
    }

    // Stream response stream
    ResponseWrapper rw = getResponseWrapper(context);

    // Set HTTP Headers
    rw.addHeader(Constants.CONTENT_LENGTH, String.format("%s", length));

    // Set filename when required
    String filename = determineFilename(documentId, gfsFile);
    if (setDisposition && StringUtils.isNotBlank(filename)) {
        rw.addHeader(Constants.CONTENT_DISPOSITION, String.format("attachment;filename=%s", filename));
    }

    String contentType = getMimeType(gfsFile.getContentType(), filename);
    if (contentType != null) {
        rw.setContentType(contentType);
    }

    boolean isGzipSupported = isGzipEncodingSupported(context);

    // Stream data
    if ((StringUtils.isBlank(compression))) {
        // Write data as-is, no marker available that data is stored compressed
        try (OutputStream os = rw.getOutputStream()) {
            gfsFile.writeTo(os);
            os.flush();
        }

    } else {

        if (isGzipSupported && StringUtils.contains(compression, GZIP)) {
            // Write compressend data as-is, since data is stored as gzipped data and
            // the agent suports it.
            rw.addHeader(Constants.CONTENT_ENCODING, GZIP);
            try (OutputStream os = rw.getOutputStream()) {
                gfsFile.writeTo(os);
                os.flush();
            }

        } else {
            // Write data uncompressed
            try (OutputStream os = rw.getOutputStream()) {
                InputStream is = gfsFile.getInputStream();
                try (GZIPInputStream gzis = new GZIPInputStream(is)) {
                    IOUtils.copyLarge(gzis, os);
                    os.flush();
                }
            }
        }
    }
}

From source file:org.exoplatform.document.util.FileUtils.java

/**
 * Returns the size of the specified file or directory. If the provided File
 * is a regular file, then the file's length is returned. If the argument is
 * a directory, then the size of the directory is calculated recursively. If
 * a directory or subdirectory is security restricted, its size will not be
 * included.//  www .j av a 2 s  . c  o  m
 * 
 * @param inputStream
 *            - the input stream
 * @param fileName
 *            - the file's name
 * 
 * @return the length of the file, or recursive size of the directory,
 *         provided (in bytes).
 * @throws NullPointerException
 *             - if the file is null
 * @throws IllegalArgumentException
 *             - if the file does not exist.
 */
public static long sizeOf(InputStream inputStream, String fileName) throws FileException {
    FileOutputStream fileOutputStream = null;
    CountingInputStream countingInputStream = null;

    long sizeOfFile = 0;
    try {
        fileName = FileNameUtils.getName(fileName);
        fileOutputStream = new FileOutputStream(new File(FilePathUtils.ROOT_PATH + fileName));
        countingInputStream = new CountingInputStream(inputStream);

        IOUtils.copyLarge(countingInputStream, fileOutputStream);
        sizeOfFile = countingInputStream.getByteCount();
    } catch (Exception ex) {
        sizeOfFile = 0;
    } finally {
        IOUtils.closeQuietly(countingInputStream);
        IOUtils.closeQuietly(fileOutputStream);
    }

    return sizeOfFile;
}

From source file:org.fcrepo.akubra.glacier.fcrepo.GlacierLowlevelStorage.java

private static long copy(InputStream source, OutputStream sink) {
    try {/* w ww  . j av a2  s . c  o  m*/
        return IOUtils.copyLarge(source, sink);
    } catch (IOException e) {
        logger.error(e.toString(), e);
        throw new FaultException("System error copying stream", e);
    } finally {
        IOUtils.closeQuietly(source);
        IOUtils.closeQuietly(sink);
    }
}

From source file:org.geoserver.wps.ppio.GeoTiffPPIO.java

@Override
public void encode(Object value, OutputStream os) throws Exception {
    GridCoverage2D coverage = (GridCoverage2D) value;

    CoordinateReferenceSystem crs = coverage.getCoordinateReferenceSystem();
    boolean unreferenced = crs == null || crs instanceof EngineeringCRS;

    // did we get lucky and all we need to do is to copy a file over?
    final Object fileSource = coverage.getProperty(GridCoverage2DReader.FILE_SOURCE_PROPERTY);
    if (fileSource != null && fileSource instanceof String) {
        File file = new File((String) fileSource);
        if (file.exists()) {
            GeoTiffReader reader = null;
            FileInputStream fis = null;
            try {
                // geotiff reader won't read unreferenced tiffs unless we tell it to
                if (unreferenced) {
                    // just check if it has the proper extension for the moment, until
                    // we get a more reliable way to check if it's a tiff
                    String name = file.getName().toLowerCase();
                    if (!name.endsWith(".tiff") && !name.endsWith(".tif")) {
                        throw new IOException("Not a tiff");
                    }/*from   w ww.j  a va  2s .  co m*/
                } else {
                    reader = new GeoTiffReader(file);
                    reader.read(null);
                }
                // ooh, a geotiff already!
                fis = new FileInputStream(file);
                IOUtils.copyLarge(fis, os);
                return;
            } catch (Exception e) {
                // ok, not a geotiff!
            } finally {
                if (reader != null) {
                    reader.dispose();
                }
                if (fis != null) {
                    fis.close();
                }
            }
        }
    }

    // ok, encode in geotiff
    if (unreferenced) {
        new ImageWorker(coverage.getRenderedImage()).writeTIFF(os, "LZW", 0.75f, 256, 256);
    } else {
        GeoTiffFormat format = new GeoTiffFormat();
        final GeoTiffFormat wformat = new GeoTiffFormat();
        final GeoTiffWriteParams wp = new GeoTiffWriteParams();
        wp.setCompressionMode(GeoTiffWriteParams.MODE_EXPLICIT);
        wp.setCompressionType("LZW");
        wp.setTilingMode(GeoToolsWriteParams.MODE_EXPLICIT);
        wp.setTiling(256, 256);
        final ParameterValueGroup wparams = wformat.getWriteParameters();
        wparams.parameter(AbstractGridFormat.GEOTOOLS_WRITE_PARAMS.getName().toString()).setValue(wp);

        final GeneralParameterValue[] wps = (GeneralParameterValue[]) wparams.values()
                .toArray(new GeneralParameterValue[1]);
        // write out the coverage
        AbstractGridCoverageWriter writer = (AbstractGridCoverageWriter) format.getWriter(os);
        if (writer == null)
            throw new WPSException("Could not find the GeoTIFF writer, please check it's in the classpath");
        try {
            writer.write(coverage, wps);
        } finally {
            try {
                writer.dispose();
            } catch (Exception e) {
                // swallow
            }
        }
    }
}

From source file:org.gradle.api.internal.externalresource.transport.sftp.SftpResourceUploader.java

public void upload(Factory<InputStream> sourceFactory, Long contentLength, String destination)
        throws IOException {
    URI uri = toUri(destination);
    SftpClient client = sftpClientFactory.createSftpClient(uri, credentials);
    String path = uri.getPath();// w ww.j a v  a 2  s.  co  m

    OutputStream outputStream = null;
    try {
        ensureParentDirectoryExists(client, path);
        outputStream = client.write(uri.getPath());
        InputStream sourceStream = sourceFactory.create();
        try {
            IOUtils.copyLarge(sourceStream, outputStream);
            outputStream.flush();
        } finally {
            sourceStream.close();
        }
    } finally {
        if (outputStream != null) {
            outputStream.close();
        }
        sftpClientFactory.releaseSftpClient(client);
    }
}

From source file:org.gradle.api.internal.file.AbstractFileTreeElement.java

public void copyTo(OutputStream outstr) {
    try {/*w w  w.java 2 s. co m*/
        InputStream inputStream = open();
        try {
            IOUtils.copyLarge(inputStream, outstr);
        } finally {
            inputStream.close();
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:org.gradle.api.internal.file.CopyVisitor.java

private void copyFileFiltered(File srcFile, File destFile) throws IOException {
    FileReader inReader = new FileReader(srcFile);
    filterChain.findFirstFilterChain().setInputSource(inReader);
    FileWriter fWriter = new FileWriter(destFile);
    try {//from  w w w. j a  va 2  s .c o  m
        IOUtils.copyLarge(filterChain, fWriter);
        fWriter.flush();
    } finally {
        IOUtils.closeQuietly(inReader);
        IOUtils.closeQuietly(fWriter);
    }
}