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:pl.psnc.synat.wrdz.zmd.object.DownloadServlet.java

/**
 * Forwards the result data to the given response.
 * /*from  www. ja va2 s. c o  m*/
 * @param result
 *            fetch request result
 * @param resp
 *            servlet response
 * @throws IOException
 *             ...
 */
private void forwardData(AsyncRequestResult result, HttpServletResponse resp) throws IOException {

    if (result.hasContent()) {
        InputStream data = null;
        OutputStream output = null;
        try {
            File file = new File(config.getAsyncCacheHome() + "/" + result.getId());

            long length = file.length();
            if (length > 0) {
                resp.addHeader(H_CONTENT_LENGTH, String.valueOf(length));
            }
            if (result.getFilename() != null) {
                resp.addHeader(H_CONTENT_DISPOSITION, H_CONTENT_DISPOSITION_PREFIX + result.getFilename());
            }

            data = new FileInputStream(file);
            output = resp.getOutputStream();
            IOUtils.copyLarge(data, output);
        } catch (FileNotFoundException e) {
            resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            return;
        } finally {
            IOUtils.closeQuietly(data);
            IOUtils.closeQuietly(output);
        }
    }
}

From source file:pl.psnc.synat.wrdz.zmd.object.ObjectBean.java

/**
 * Handler triggered by completion of file upload.
 * /*  www  . j  ava2s .  c  o  m*/
 * @param event
 *            file upload event.
 */
public void uploadFile(FileUploadEvent event) throws IOException {
    object = File.createTempFile("zmd", "zip");
    object.deleteOnExit();
    FileOutputStream stream = new FileOutputStream(object);
    try {
        IOUtils.copyLarge(event.getUploadedFile().getInputStream(), stream);
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

From source file:play.modules.netty.StreamChunkAggregator.java

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {

    Object msg = e.getMessage();//from ww  w . j  a va 2  s  .c  om
    if (!(msg instanceof HttpMessage) && !(msg instanceof HttpChunk)) {
        ctx.sendUpstream(e);
        return;
    }

    HttpMessage currentMessage = this.currentMessage;
    File localFile = this.file;
    if (currentMessage == null) {

        HttpMessage m = (HttpMessage) msg;
        if (m.isChunked()) {
            final String localName = UUID.randomUUID().toString();

            // A chunked message - remove 'Transfer-Encoding' header,
            // initialize the cumulative buffer, and wait for incoming chunks.
            List<String> encodings = m.getHeaders(HttpHeaders.Names.TRANSFER_ENCODING);
            encodings.remove(HttpHeaders.Values.CHUNKED);
            if (encodings.isEmpty()) {
                m.removeHeader(HttpHeaders.Names.TRANSFER_ENCODING);
            }

            this.currentMessage = m;
            this.file = new File(Play.tmpDir, localName);
            this.out = new FileOutputStream(file, true);
        } else {
            // Not a chunked message - pass through.
            ctx.sendUpstream(e);
        }
    } else {
        // TODO: If less that threshold then in memory
        // Merge the received chunk into the content of the current message.
        final HttpChunk chunk = (HttpChunk) msg;
        if (maxContentLength != -1
                && (localFile.length() > (maxContentLength - chunk.getContent().readableBytes()))) {
            currentMessage.setHeader(HttpHeaders.Names.WARNING, "play.module.netty.content.length.exceeded");
        } else {
            IOUtils.copyLarge(new ChannelBufferInputStream(chunk.getContent()), this.out);

            if (chunk.isLast()) {
                this.out.flush();
                this.out.close();

                currentMessage.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(localFile.length()));

                currentMessage.setContent(new FileChannelBuffer(localFile));
                this.out = null;
                this.currentMessage = null;
                this.file = null;
                Channels.fireMessageReceived(ctx, currentMessage, e.getRemoteAddress());
            }
        }
    }
}

From source file:pt.ua.tm.neji.util.ZipGenerator.java

private static void fill(String parentPath, Pair<String, File>[] files, ZipOutputStream zip,
        BufferedOutputStream out) throws IOException {
    for (Pair<String, File> pair : files) {

        if (pair.b() == null)
            continue;

        if (pair.b().isDirectory() && pair.b().list().length > 0) {
            File[] subFiles = pair.b().listFiles();
            int size = subFiles.length;
            Pair<String, File>[] subPairs = new Pair[size];
            for (int i = 0; i < size; i++) {
                subPairs[i] = new Pair<>(subFiles[i].getName(), subFiles[i]);
            }/*from   w  ww. j  a v  a2s.  c  o  m*/

            fill(parentPath + pair.a() + File.separator, subPairs, zip, out);

        } else if (pair.b().isFile()) {
            zip.putNextEntry(new ZipEntry(parentPath + pair.a()));
            FileInputStream fis = new FileInputStream(pair.b());
            IOUtils.copyLarge(fis, out);
            fis.close();
            out.flush();
        }

        System.out.print(".");

        zip.closeEntry();
    }
}

From source file:s3copy.SimpleCopier.java

void run(String[] args) throws IOException {
    SCCmdLine cmdline = new SCCmdLine();
    SCCmdLine.Settings settings = cmdline.parse(args);

    String[] components = settings.input.split("/");
    String bucket = components[0];
    StringBuilder sb = new StringBuilder();
    for (int i = 1; i < components.length; i++) {
        sb.append(components[i]);/*from   w  ww.j a  v a  2  s. c  om*/
        sb.append("/");
    }
    sb.deleteCharAt(sb.length() - 1);

    AmazonS3 s3client = new AmazonS3Client(
            new BasicAWSCredentials(settings.access_key_id, settings.secret_access_key));
    System.out.println("bucket: " + bucket);
    System.out.println("value: " + sb.toString());
    S3Object obj = s3client.getObject(bucket, sb.toString());
    InputStream is = obj.getObjectContent();

    OutputStream out = null;
    if (!settings.isURI()) {
        //local copy
        out = new FileOutputStream(settings.output);
    } else {
        Configuration conf = new Configuration();
        if (settings.conf != null) {
            File _conf = new File(settings.conf);
            if (_conf.exists()) {
                if (_conf.isDirectory()) {
                    conf.addResource(new Path(new File(settings.conf, "core-site.xml").getAbsolutePath()));
                } else {
                    conf.addResource(new Path(settings.conf));
                }
            }
        }

        FileSystem fs = FileSystem.get(conf);
        out = fs.create(new Path(settings.output));
    }
    IOUtils.copyLarge(is, out);
    out.close();
}

From source file:se.inera.axel.shs.processor.SharedDeferredStream.java

public static InputStream toSharedInputStream(InputStream inputStream) throws IOException {

    if (inputStream instanceof SharedInputStream) {
        return inputStream;
    }//from   w  w  w .j a  va2  s . c o  m

    DeferredFileOutputStream outputStream = createDeferredOutputStream();

    try {
        IOUtils.copyLarge(inputStream, outputStream);
        outputStream.flush();
        IOUtils.closeQuietly(outputStream);
        return SharedDeferredStream.toSharedInputStream(outputStream);
    } finally {
        IOUtils.closeQuietly(outputStream);
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:tachyon.client.file.FileSystemUtils.java

/**
 * Persist the given file to the under file system.
 *
 * @param fs {@link FileSystem} to carry out Tachyon operations
 * @param uri the uri of the file to persist
 * @param status the status info of the file
 * @param tachyonConf TachyonConf object
 * @return the size of the file persisted
 * @throws IOException if an I/O error occurs
 * @throws FileDoesNotExistException if the given file does not exist
 * @throws TachyonException if an unexpected Tachyon error occurs
 *//*from   w  w  w . ja  v a2s  .  co m*/
public static long persistFile(FileSystem fs, TachyonURI uri, URIStatus status, TachyonConf tachyonConf)
        throws IOException, FileDoesNotExistException, TachyonException {
    // TODO(manugoyal) move this logic to the worker, as it deals with the under file system
    Closer closer = Closer.create();
    long ret;
    try {
        OpenFileOptions options = OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE);
        FileInStream in = closer.register(fs.openFile(uri, options));
        TachyonURI dstPath = new TachyonURI(status.getUfsPath());
        UnderFileSystem ufs = UnderFileSystem.get(dstPath.toString(), tachyonConf);
        String parentPath = dstPath.getParent().toString();
        if (!ufs.exists(parentPath) && !ufs.mkdirs(parentPath, true)) {
            throw new IOException("Failed to create " + parentPath);
        }
        OutputStream out = closer.register(ufs.create(dstPath.getPath()));
        ret = IOUtils.copyLarge(in, out);
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
    // Tell the master to mark the file as persisted
    fs.setAttribute(uri, SetAttributeOptions.defaults().setPersisted(true));
    return ret;
}

From source file:tachyon.client.file.TachyonFileSystemUtils.java

/**
 * Persist the given file to the under file system.
 *
 * @param tfs {@link TachyonFileSystem} to carry out tachyon operations
 * @param file the file to persist//  w  w w. j  a  v  a  2  s  . c  om
 * @param fileInfo the file info of the file
 * @param tachyonConf TachyonConf object
 * @return the size of the file persisted
 * @throws IOException if an I/O error occurs
 * @throws FileDoesNotExistException if the given file does not exist
 * @throws TachyonException if an unexpected Tachyon error occurs
 */
public static long persistFile(TachyonFileSystem tfs, TachyonFile file, FileInfo fileInfo,
        TachyonConf tachyonConf) throws IOException, FileDoesNotExistException, TachyonException {
    // TODO(manugoyal) move this logic to the worker, as it deals with the under file system
    Closer closer = Closer.create();
    long ret;
    try {
        InStreamOptions inStreamOptions = new InStreamOptions.Builder(tachyonConf)
                .setTachyonStorageType(TachyonStorageType.NO_STORE).build();
        FileInStream in = closer.register(tfs.getInStream(file, inStreamOptions));
        TachyonURI dstPath = new TachyonURI(fileInfo.getUfsPath());
        UnderFileSystem ufs = UnderFileSystem.get(dstPath.getPath(), tachyonConf);
        String parentPath = dstPath.getParent().getPath();
        if (!ufs.exists(parentPath) && !ufs.mkdirs(parentPath, true)) {
            throw new IOException("Failed to create " + parentPath);
        }
        OutputStream out = closer.register(ufs.create(dstPath.getPath()));
        ret = IOUtils.copyLarge(in, out);
    } catch (Exception e) {
        throw closer.rethrow(e);
    } finally {
        closer.close();
    }
    // Tell the master to mark the file as persisted
    tfs.setState(file, (new SetStateOptions.Builder()).setPersisted(true).build());
    return ret;
}

From source file:twitter4j.internal.models4j.TwitterBaseImpl.java

private String downloadContentIntoFileReturningPath(String url) {
    InputStream urlInputStream = null;
    FileOutputStream temporaryFileStream = null;
    try {/*from ww  w  .  j  a  v a  2s  . c  o  m*/
        urlInputStream = new URL(url).openStream();
        File tempMedia = File.createTempFile("tw-upload-temp-" + System.currentTimeMillis(), ".tmp");
        tempMedia.deleteOnExit();
        temporaryFileStream = new FileOutputStream(tempMedia);
        IOUtils.copyLarge(urlInputStream, temporaryFileStream);
        temporaryFileStream.flush();
        return tempMedia.getAbsolutePath();

    } catch (Exception e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(temporaryFileStream);
        IOUtils.closeQuietly(urlInputStream);
    }
}