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:net.ymate.framework.unpack.Unpackers.java

private boolean __unpack(JarFile jarFile, String prefixPath) throws Exception {
    boolean _results = false;
    Enumeration<JarEntry> _entriesEnum = jarFile.entries();
    for (; _entriesEnum.hasMoreElements();) {
        JarEntry _entry = _entriesEnum.nextElement();
        if (StringUtils.startsWith(_entry.getName(), prefixPath)) {
            if (!_entry.isDirectory()) {
                _LOG.info("Synchronizing resource file: " + _entry.getName());
                //
                String _entryName = StringUtils.substringAfter(_entry.getName(), prefixPath);
                File _targetFile = new File(RuntimeUtils.getRootPath(false), _entryName);
                _targetFile.getParentFile().mkdirs();
                IOUtils.copyLarge(jarFile.getInputStream(_entry), new FileOutputStream(_targetFile));
                _results = true;//from   www  .j a va 2s. co m
            }
        }
    }
    return _results;
}

From source file:net.ymate.module.webproxy.WebProxy.java

private String __doParseContentBody(HttpURLConnection _conn, byte[] _content, String charset) throws Exception {
    if (StringUtils.contains(_conn.getHeaderField("Content-Encoding"), "gzip")) {
        ByteArrayInputStream _input = null;
        GZIPInputStream _gzip = null;
        ByteArrayOutputStream _output = null;
        try {//from  ww  w .ja  v  a2s.co  m
            _input = new ByteArrayInputStream(_content);
            _gzip = new GZIPInputStream(_input);
            _output = new ByteArrayOutputStream();
            //
            IOUtils.copyLarge(_gzip, _output);
            return new String(_output.toByteArray(), charset);
        } finally {
            IOUtils.closeQuietly(_output);
            IOUtils.closeQuietly(_gzip);
            IOUtils.closeQuietly(_input);
        }
    }
    return new String(_content, charset);
}

From source file:net.ymate.module.webproxy.WebProxy.java

@SuppressWarnings("unchecked")
public void transmission(HttpServletRequest request, HttpServletResponse response, String url,
        Type.HttpMethod method) throws Exception {
    StopWatch _consumeTime = null;/*  ww  w.  j av  a  2s  .co m*/
    long _threadId = 0;
    if (_LOG.isDebugEnabled()) {
        _consumeTime = new StopWatch();
        _consumeTime.start();
        _threadId = Thread.currentThread().getId();
        _LOG.debug("-------------------------------------------------");
        _LOG.debug("--> [" + _threadId + "] URL: " + url);
    }
    //
    HttpURLConnection _conn = null;
    try {
        if (__moduleCfg.isUseProxy()) {
            _conn = (HttpURLConnection) new URL(url).openConnection(__moduleCfg.getProxy());
        } else {
            _conn = (HttpURLConnection) new URL(url).openConnection();
        }
        _conn.setUseCaches(__moduleCfg.isUseCaches());
        _conn.setInstanceFollowRedirects(__moduleCfg.isInstanceFollowRedirects());
        //
        boolean _postFlag = Type.HttpMethod.POST.equals(method);
        boolean _multipartFlag = _postFlag && StringUtils.contains(request.getContentType(), "multipart/");
        if (_postFlag) {
            _conn.setDoOutput(true);
            _conn.setDoInput(true);
            _conn.setRequestMethod(method.name());
        }
        if (__moduleCfg.getConnectTimeout() > 0) {
            _conn.setConnectTimeout(__moduleCfg.getConnectTimeout());
        }
        if (__moduleCfg.getReadTimeout() > 0) {
            _conn.setReadTimeout(__moduleCfg.getReadTimeout());
        }
        //
        if (_LOG.isDebugEnabled()) {
            _LOG.debug("--> [" + _threadId + "] Method: " + method.name());
            _LOG.debug("--> [" + _threadId + "] Request Headers: ");
        }
        //
        Enumeration _header = request.getHeaderNames();
        while (_header.hasMoreElements()) {
            String _name = (String) _header.nextElement();
            String _value = request.getHeader(_name);
            boolean _flag = false;
            if (_postFlag && StringUtils.equalsIgnoreCase(_name, "content-type")
                    || __moduleCfg.isTransferHeaderEnabled()
                            && (!__moduleCfg.getTransferHeaderBlackList().isEmpty()
                                    && !__moduleCfg.getTransferHeaderBlackList().contains(_name)
                                    || !__moduleCfg.getTransferHeaderWhiteList().isEmpty()
                                            && __moduleCfg.getTransferHeaderWhiteList().contains(_name))) {
                _conn.setRequestProperty(_name, _value);
                _flag = true;
            }
            //
            if (_LOG.isDebugEnabled()) {
                _LOG.debug("--> [" + _threadId + "] \t " + (_flag ? " - " : " > ") + _name + ": " + _value);
            }
        }
        _conn.connect();
        //
        if (_postFlag) {
            DataOutputStream _output = new DataOutputStream(_conn.getOutputStream());
            try {
                if (_multipartFlag) {
                    if (_LOG.isDebugEnabled()) {
                        _LOG.debug("--> [" + _threadId + "] Multipart: TRUE");
                    }
                    IOUtils.copyLarge(request.getInputStream(), _output);
                } else {
                    String _charset = request.getCharacterEncoding();
                    String _queryStr = ParamUtils.buildQueryParamStr(request.getParameterMap(), true, _charset);
                    IOUtils.write(_queryStr, _output, _charset);
                    //
                    if (_LOG.isDebugEnabled()) {
                        _LOG.debug("--> [" + _threadId + "] Request Parameters: ");
                        Map<String, String> _paramsMap = ParamUtils.parseQueryParamStr(_queryStr, true,
                                _charset);
                        for (Map.Entry<String, String> _param : _paramsMap.entrySet()) {
                            _LOG.debug("--> [" + _threadId + "] \t - " + _param.getKey() + ": "
                                    + _param.getValue());
                        }
                    }
                }
                _output.flush();
            } finally {
                IOUtils.closeQuietly(_output);
            }
        }
        //
        int _code = _conn.getResponseCode();
        //
        if (_LOG.isDebugEnabled()) {
            _LOG.debug("--> [" + _threadId + "] Response Code: " + _code);
            _LOG.debug("--> [" + _threadId + "] Response Headers: ");
        }
        //
        Map<String, List<String>> _headers = _conn.getHeaderFields();
        for (Map.Entry<String, List<String>> _entry : _headers.entrySet()) {
            if (_entry.getKey() != null) {
                boolean _flag = false;
                String _values = StringUtils.join(_entry.getValue(), ",");
                if (StringUtils.equalsIgnoreCase(_entry.getKey(), "content-type")
                        || __moduleCfg.isTransferHeaderEnabled()
                                && !__moduleCfg.getResponseHeaderWhileList().isEmpty()
                                && __moduleCfg.getResponseHeaderWhileList().contains(_entry.getKey())) {
                    response.setHeader(_entry.getKey(), _values);
                    _flag = true;
                }
                if (_LOG.isDebugEnabled()) {
                    _LOG.debug("--> [" + _threadId + "] \t " + (_flag ? " - " : " > ") + _entry.getKey() + ": "
                            + _values);
                }
            }
        }
        if (HttpURLConnection.HTTP_BAD_REQUEST <= _conn.getResponseCode()) {
            response.sendError(_code);
        } else {
            if (HttpURLConnection.HTTP_OK == _code) {
                InputStream _inputStream = _conn.getInputStream();
                if (_inputStream != null) {
                    if (!_multipartFlag) {
                        byte[] _content = IOUtils.toByteArray(_inputStream);
                        IOUtils.write(_content, response.getOutputStream());
                        //
                        if (_LOG.isDebugEnabled()) {
                            _LOG.debug("--> [" + _threadId + "] Response Content: " + __doParseContentBody(
                                    _conn, _content, WebMVC.get().getModuleCfg().getDefaultCharsetEncoding()));
                        }
                    } else {
                        IOUtils.copyLarge(_conn.getInputStream(), response.getOutputStream());
                        //
                        if (_LOG.isDebugEnabled()) {
                            _LOG.debug("--> [" + _threadId + "] Response Content: MultipartBody");
                        }
                    }
                } else if (_LOG.isDebugEnabled()) {
                    _LOG.debug("--> [" + _threadId + "] Response Content: NULL");
                }
                response.flushBuffer();
            } else {
                InputStream _inputStream = _conn.getInputStream();
                if (_inputStream != null) {
                    byte[] _content = IOUtils.toByteArray(_inputStream);
                    IOUtils.write(_content, response.getOutputStream());
                    //
                    if (_LOG.isDebugEnabled()) {
                        _LOG.debug("--> [" + _threadId + "] Response Content: " + __doParseContentBody(_conn,
                                _content, WebMVC.get().getModuleCfg().getDefaultCharsetEncoding()));
                    }
                } else if (_LOG.isDebugEnabled()) {
                    _LOG.debug("--> [" + _threadId + "] Response Content: NULL");
                }
                response.setStatus(_code);
                response.flushBuffer();
            }
        }
    } catch (Throwable e) {
        _LOG.warn("An exception occurred while processing request mapping '" + url + "': ",
                RuntimeUtils.unwrapThrow(e));
    } finally {
        IOUtils.close(_conn);
        //
        if (_LOG.isDebugEnabled()) {
            if (_consumeTime != null) {
                _consumeTime.stop();
                _LOG.debug("--> [" + _threadId + "] Total execution time: " + _consumeTime.getTime() + "ms");
            }
            _LOG.debug("-------------------------------------------------");
        }
    }
}

From source file:nl.kpmg.lcm.server.data.azure.AzureFileAdapter.java

@Override
public void write(InputStream stream, Long size) throws IOException {
    if (size != null && size <= 0) {
        return;/*from  w w w .  j  a v a  2 s.  com*/
    }

    if (stream == null) {
        return;
    }

    OutputStream os = getOutputStream();
    try {
        IOUtils.copyLarge(stream, os);
    } finally {
        if (os != null) {
            os.close();
        }
    }
}

From source file:nl.vu.psy.relic.resolvers.implementations.LocalFileSystemResolver.java

@Override
public void resolveInternal(Relic r, ResolverDescriptor rd, File cacheDirectory) throws RelicException {
    File internalFile = new File(cacheDirectory, r.getFileName());
    File externalFile = new File(rd.getProperty(DescriptorKeys.ABSOLUTEPATH.getKey()));
    try {/*from  ww w .  ja v a  2s.  c o m*/
        FileInputStream fis = new FileInputStream(externalFile);
        FileOutputStream fos = new FileOutputStream(internalFile);
        IOUtils.copyLarge(fis, fos);
        fis.close();
        fos.flush();
        fos.close();
    } catch (Exception e) {
        throw new RelicException("Could not retrieve the file to the working directory", e);
    }
}

From source file:nl.vu.psy.relic.resolvers.implementations.LocalFileSystemResolver.java

@Override
public void resolveExternal(Relic r, ResolverDescriptor rd, File cacheDirectory) throws RelicException {
    File internalFile = new File(cacheDirectory, r.getFileName());
    File externalFile = new File(rd.getProperty(DescriptorKeys.ABSOLUTEPATH.getKey()));
    try {/*from   www .j a  v a2  s.c  o  m*/
        FileInputStream fis = new FileInputStream(internalFile);
        FileOutputStream fos = new FileOutputStream(externalFile);
        IOUtils.copyLarge(fis, fos);
        fis.close();
        fos.flush();
        fos.close();
    } catch (Exception e) {
        throw new RelicException("Could not copy the file to the external directory", e);
    }
}

From source file:no.kantega.publishing.common.util.InputStreamHandler.java

public void handleInputStream(InputStream in) throws IOException {
    IOUtils.copyLarge(in, out);
}

From source file:nu.kelvin.jfileshare.ajax.FileReceiverServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();
    UserItem currentUser = (UserItem) session.getAttribute("user");
    if (currentUser != null && ServletFileUpload.isMultipartContent(req)) {
        Conf conf = (Conf) getServletContext().getAttribute("conf");
        // keep files of up to 10 MiB in memory 10485760
        FileItemFactory factory = new DiskFileItemFactory(10485760, new File(conf.getPathTemp()));
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(conf.getFileSizeMax());

        // set file upload progress listener
        FileUploadListener listener = new FileUploadListener();
        session.setAttribute("uploadListener", listener);
        upload.setProgressListener(listener);

        File tempFile = File.createTempFile(String.format("%05d-", currentUser.getUid()), null,
                new File(conf.getPathTemp()));
        tempFile.deleteOnExit();/*w w  w .  ja v a2s  .  c  o m*/
        try {
            FileItem file = new FileItem();

            /* iterate over all uploaded items */
            FileItemIterator it = upload.getItemIterator(req);
            FileOutputStream filestream = null;

            while (it.hasNext()) {
                FileItemStream item = it.next();
                String name = item.getFieldName();
                InputStream instream = item.openStream();
                DigestOutputStream outstream = null;

                if (item.isFormField()) {
                    String value = Streams.asString(instream);
                    // logger.info(name + " : " + value);
                    /* not the file upload. Maybe the password field? */
                    if (name.equals("password") && !value.equals("")) {
                        logger.info("Uploaded file has password set");
                        file.setPwPlainText(value);
                    }
                    instream.close();
                } else {
                    // This is the file you're looking for
                    file.setName(item.getName());
                    file.setType(
                            item.getContentType() == null ? "application/octet-stream" : item.getContentType());
                    file.setUid(currentUser.getUid());

                    try {
                        filestream = new FileOutputStream(tempFile);
                        MessageDigest md = MessageDigest.getInstance("MD5");
                        outstream = new DigestOutputStream(filestream, md);
                        long filesize = IOUtils.copyLarge(instream, outstream);

                        if (filesize == 0) {
                            throw new Exception("File is empty.");
                        }
                        md = outstream.getMessageDigest();
                        file.setMd5sum(toHex(md.digest()));
                        file.setSize(filesize);

                    } finally {
                        if (outstream != null) {
                            try {
                                outstream.close();
                            } catch (IOException ignored) {
                            }
                        }
                        if (filestream != null) {
                            try {
                                filestream.close();
                            } catch (IOException ignored) {
                            }
                        }
                        if (instream != null) {
                            try {
                                instream.close();
                            } catch (IOException ignored) {
                            }
                        }
                    }
                }
            }
            /* All done. Save the new file */
            if (conf.getDaysFileExpiration() != 0) {
                file.setDaysToKeep(conf.getDaysFileExpiration());
            }
            if (file.create(ds, req.getRemoteAddr())) {
                File finalFile = new File(conf.getPathStore(), Integer.toString(file.getFid()));
                tempFile.renameTo(finalFile);
                logger.log(Level.INFO, "User {0} storing file \"{1}\" in the filestore",
                        new Object[] { currentUser.getUid(), file.getName() });
                req.setAttribute("msg",
                        "File <strong>\"" + Helpers.htmlSafe(file.getName())
                                + "\"</strong> uploaded successfully. <a href='" + req.getContextPath()
                                + "/file/edit/" + file.getFid() + "'>Click here to edit file</a>");
                req.setAttribute("javascript", "parent.uploadComplete('info');");
            } else {
                req.setAttribute("msg", "Unable to contact the database");
                req.setAttribute("javascript", "parent.uploadComplete('critical');");
            }
        } catch (SizeLimitExceededException e) {
            tempFile.delete();
            req.setAttribute("msg", "File is too large. The maximum size of file uploads is "
                    + FileItem.humanReadable(conf.getFileSizeMax()));
            req.setAttribute("javascript", "parent.uploadComplete('warning');");
        } catch (FileUploadException e) {
            tempFile.delete();
            req.setAttribute("msg", "Unable to upload file");
            req.setAttribute("javascript", "parent.uploadComplete('warning');");
        } catch (Exception e) {
            tempFile.delete();
            req.setAttribute("msg",
                    "Unable to upload file. ".concat(e.getMessage() == null ? "" : e.getMessage()));
            req.setAttribute("javascript", "parent.uploadComplete('warning');");
        } finally {
            session.setAttribute("uploadListener", null);
        }
        ServletContext app = getServletContext();
        RequestDispatcher disp = app.getRequestDispatcher("/templates/AjaxDummy.jsp");
        disp.forward(req, resp);
    }
}

From source file:nu.kelvin.jfileshare.servlets.FileDownloadServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, java.io.IOException {
    // By the time we get here, the fileauthfilter has done the sanity
    // checking and authentication already. We can jump right into
    // serving the file.
    Conf conf = (Conf) getServletContext().getAttribute("conf");
    FileItem file = (FileItem) req.getAttribute("file");
    File fileOnDisk = new File(conf.getPathStore() + "/" + file.getFid().toString());

    logger.info("Preparing to stream file");
    resp.setContentType(file.getType());
    String disposition = req.getServletPath().equals("/file/get")
            && "image".equals(file.getType().substring(0, 5)) ? "inline" : "attachment";
    resp.setHeader("Content-disposition", disposition + "; filename=\"" + file.getName() + "\"");
    resp.setHeader("Content-length", Long.toString(fileOnDisk.length()));

    FileInputStream instream = new FileInputStream(fileOnDisk);
    ServletOutputStream outstream = resp.getOutputStream();

    try {/*www.  ja v  a 2s . com*/
        IOUtils.copyLarge(instream, outstream);
    } finally {
        if (instream != null) {
            instream.close();
        }
        if (outstream != null) {
            outstream.close();
        }
    }
    file.logDownload(ds, req.getRemoteAddr());
}

From source file:org.akubraproject.impl.AbstractBlobStoreConnection.java

@Override
public Blob getBlob(InputStream content, long estimatedSize, Map<String, String> hints)
        throws IOException, UnsupportedOperationException {
    Blob blob = getBlob(null, hints);

    boolean success = false;
    try {//from   w  w  w  .  j  av  a 2s  .co m
        OutputStream out = blob.openOutputStream(estimatedSize, true);
        try {
            IOUtils.copyLarge(content, out);
            out.close();
            out = null;
        } finally {
            if (out != null)
                IOUtils.closeQuietly(out);
        }

        success = true;
    } finally {
        if (!success) {
            try {
                blob.delete();
            } catch (Throwable t) {
                log.error("Error deleting blob after blob-creation failure", t);
            }
        }
    }

    return blob;
}