Example usage for org.apache.commons.io.output DeferredFileOutputStream DeferredFileOutputStream

List of usage examples for org.apache.commons.io.output DeferredFileOutputStream DeferredFileOutputStream

Introduction

In this page you can find the example usage for org.apache.commons.io.output DeferredFileOutputStream DeferredFileOutputStream.

Prototype

public DeferredFileOutputStream(int threshold, File outputFile) 

Source Link

Document

Constructs an instance of this class which will trigger an event at the specified threshold, and save data to a file beyond that point.

Usage

From source file:jenkins.security.security218.ysoserial.payloads.FileUpload1.java

public void release(DiskFileItem obj) throws Exception {
    // otherwise the finalizer deletes the file
    DeferredFileOutputStream dfos = new DeferredFileOutputStream(0, null);
    Reflections.setFieldValue(obj, "dfos", dfos);
}

From source file:jenkins.security.security218.ysoserial.payloads.FileUpload1.java

private static DiskFileItem makePayload(int thresh, String repoPath, String filePath, byte[] data)
        throws IOException, Exception {
    // if thresh < written length, delete outputFile after copying to repository temp file
    // otherwise write the contents to repository temp file
    File repository = new File(repoPath);
    DiskFileItem diskFileItem = new DiskFileItem("test", "application/octet-stream", false, "test", 100000,
            repository);/*from   w ww. j a v  a 2s.  c  o m*/
    File outputFile = new File(filePath);
    DeferredFileOutputStream dfos = new DeferredFileOutputStream(thresh, outputFile);
    OutputStream os = (OutputStream) Reflections.getFieldValue(dfos, "memoryOutputStream");
    os.write(data);
    Reflections.getField(ThresholdingOutputStream.class, "written").set(dfos, data.length);
    Reflections.setFieldValue(diskFileItem, "dfos", dfos);
    Reflections.setFieldValue(diskFileItem, "sizeThreshold", 0);
    return diskFileItem;
}

From source file:hudson.scm.CvsTagsParamDefinition.java

@Exported
public ListBoxModel getSymbolicNames() {
    ListBoxModel model = new ListBoxModel();
    CvsChangeSet changeSet = null;//from  w  w w .  j  a v  a 2  s .  c o  m

    RlogCommand statusCommand = new RlogCommand();
    statusCommand.setHeaderOnly(true);
    statusCommand.setModule(moduleName);
    statusCommand.setRecursive(true);

    try {
        final File tempRlogSpill = File.createTempFile("cvs",
                "status );                                                                                                                                                                                                 `                                                                                                                                                                                                                                                                                                                                                                   ");
        final DeferredFileOutputStream outputStream = new DeferredFileOutputStream(100 * 1024, tempRlogSpill);
        final PrintStream logStream = new PrintStream(outputStream, true,
                getCvsDescriptor().getChangelogEncoding());

        final OutputStream errorOutputStream = new OutputStream() {
            final StringBuffer buffer = new StringBuffer();

            @Override
            public void write(int b) throws IOException {
                if ((int) ("\n".getBytes()[0]) == b) {
                    flush();
                } else {
                    buffer.append(new String(new byte[] { (byte) b }));
                }
            }

            @Override
            public void flush() throws IOException {
                logger.info(buffer.toString());
                buffer.delete(0, buffer.length());
                super.flush();
            }

            public void close() throws IOException {
                flush();
                super.close();
            }
        };
        final PrintStream errorPrintStream = new PrintStream(errorOutputStream);
        Client cvsClient = getCvsClient(cvsRoot, passwordRequired, password);
        cvsClient.getEventManager().addCVSListener(new BasicListener(logStream, errorPrintStream));
        cvsClient.executeCommand(statusCommand, getGlobalOptions(cvsRoot));

        logStream.close();
        errorPrintStream.flush();
        errorPrintStream.close();

        CvsLog parser = new CvsLog() {
            @Override
            public Reader read() throws IOException {
                if (outputStream.isInMemory())
                    return new InputStreamReader(new ByteArrayInputStream(outputStream.getData()),
                            getCvsDescriptor().getChangelogEncoding());
                else
                    return new InputStreamReader(new FileInputStream(outputStream.getFile()),
                            getCvsDescriptor().getChangelogEncoding());
            }

            @Override
            public void dispose() {
                tempRlogSpill.delete();
            }
        };

        changeSet = parser.mapCvsLog(cvsRoot, new CvsRepositoryLocation.HeadRepositoryLocation());

    } catch (IOException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    } catch (CommandAbortedException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    } catch (CommandException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    } catch (AuthenticationException ex) {
        model.add(new ListBoxModel.Option("Could not load symbolic names - " + ex.getLocalizedMessage()));
        return model;
    }

    model.add(new ListBoxModel.Option("Head", "HEAD"));

    for (String branchName : changeSet.getBranchNames()) {
        model.add(new ListBoxModel.Option(branchName + " (Branch)", branchName));
    }

    for (String tagName : changeSet.getTagNames()) {
        model.add(new ListBoxModel.Option(tagName + " (Tag)", tagName));
    }

    return model;
}

From source file:au.gov.ansto.bragg.nbi.restlet.fileupload.disk.DiskFileItem.java

/**
 * Returns an {@link java.io.OutputStream OutputStream} that can
 * be used for storing the contents of the file.
 *
 * @return An {@link java.io.OutputStream OutputStream} that can be used
 *         for storing the contensts of the file.
 *
 * @throws IOException if an error occurs.
 *///from www.j av  a2 s. c om
public OutputStream getOutputStream() throws IOException {
    if (dfos == null) {
        File outputFile = getTempFile();
        dfos = new DeferredFileOutputStream(sizeThreshold, outputFile);
    }
    return dfos;
}

From source file:hudson.scm.AbstractCvs.java

/**
 * Gets the output for the CVS <tt>rlog</tt> command for the given module
 * between the specified dates.//from www .ja v a 2 s  . co  m
 *
 * @param repository
 *            the repository to connect to for running rlog against
 * @param module
 *            the module to check for changes against
 * @param listener
 *            where to log any error messages to
 * @param startTime
 *            don't list any changes before this time
 * @param endTime
 *            don't list any changes after this time
 * @return the output of rlog with no modifications
 * @throws IOException
 *             on underlying communication failure
 */
private CvsLog getRemoteLogForModule(final CvsRepository repository, final CvsRepositoryItem item,
        final CvsModule module, final Date startTime, final Date endTime, final EnvVars envVars,
        final TaskListener listener) throws IOException {
    final Client cvsClient = getCvsClient(repository, envVars, listener);

    RlogCommand rlogCommand = new RlogCommand();

    // we have to synchronize since we're dealing with DateFormat.format()
    synchronized (DATE_FORMATTER) {
        final String lastBuildDate = DATE_FORMATTER.format(startTime);
        final String endDate = DATE_FORMATTER.format(endTime);

        rlogCommand.setDateFilter(lastBuildDate + "<" + endDate);
    }

    // tell CVS which module we're logging
    rlogCommand.setModule(envVars.expand(module.getRemoteName()));

    // ignore headers for files that aren't in the current change-set
    rlogCommand.setSuppressHeader(true);

    // create an output stream to send the output from CVS command to - we
    // can then parse it from here
    final File tmpRlogSpill = File.createTempFile("cvs", "rlog");
    final DeferredFileOutputStream outputStream = new DeferredFileOutputStream(100 * 1024, tmpRlogSpill);
    final PrintStream logStream = new PrintStream(outputStream, true, getDescriptor().getChangelogEncoding());

    // set a listener with our output stream that we parse the log from
    final CVSListener basicListener = new BasicListener(logStream, listener.getLogger());
    cvsClient.getEventManager().addCVSListener(basicListener);

    // log the command to the current run/polling log
    listener.getLogger().println("cvs " + rlogCommand.getCVSCommand());

    // send the command to be run, we can't continue of the task fails
    try {
        if (!cvsClient.executeCommand(rlogCommand, getGlobalOptions(repository, envVars))) {
            throw new RuntimeException("Error while trying to run CVS rlog");
        }
    } catch (CommandAbortedException e) {
        throw new RuntimeException("CVS rlog command aborted", e);
    } catch (CommandException e) {
        throw new RuntimeException("CVS rlog command failed", e);
    } catch (AuthenticationException e) {
        throw new RuntimeException("CVS authentication failure while running rlog command", e);
    } finally {
        try {
            cvsClient.getConnection().close();
        } catch (IOException ex) {
            listener.getLogger().println("Could not close client connection: " + ex.getMessage());
        }
    }

    // flush the output so we have it all available for parsing
    logStream.close();

    // return the contents of the stream as the output of the command
    return new CvsLog() {
        @Override
        public Reader read() throws IOException {
            // note that master and slave can have different platform encoding
            if (outputStream.isInMemory())
                return new InputStreamReader(new ByteArrayInputStream(outputStream.getData()),
                        getDescriptor().getChangelogEncoding());
            else
                return new InputStreamReader(new FileInputStream(outputStream.getFile()),
                        getDescriptor().getChangelogEncoding());
        }

        @Override
        public void dispose() {
            tmpRlogSpill.delete();
        }
    };
}

From source file:org.apache.maven.plugin.resources.remote.ProcessRemoteResourcesMojo.java

protected boolean copyResourceIfExists(File file, String relFileName, VelocityContext context)
        throws IOException, MojoExecutionException {
    for (Resource resource : resources) {
        File resourceDirectory = new File(resource.getDirectory());

        if (!resourceDirectory.exists()) {
            continue;
        }/*from  w w w  .  j a v a 2  s.c  om*/

        // TODO - really should use the resource includes/excludes and name mapping
        File source = new File(resourceDirectory, relFileName);
        File templateSource = new File(resourceDirectory, relFileName + TEMPLATE_SUFFIX);

        if (!source.exists() && templateSource.exists()) {
            source = templateSource;
        }

        if (source.exists() && !source.equals(file)) {
            if (source == templateSource) {
                Reader reader = null;
                Writer writer = null;
                DeferredFileOutputStream os = new DeferredFileOutputStream(velocityFilterInMemoryThreshold,
                        file);
                try {

                    if (encoding != null) {
                        reader = new InputStreamReader(new FileInputStream(source), encoding);
                        writer = new OutputStreamWriter(os, encoding);
                    } else {
                        reader = ReaderFactory.newPlatformReader(source);
                        writer = WriterFactory.newPlatformWriter(os);
                    }

                    velocity.evaluate(context, writer, "", reader);
                } catch (ParseErrorException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (MethodInvocationException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } catch (ResourceNotFoundException e) {
                    throw new MojoExecutionException("Error rendering velocity resource: " + source, e);
                } finally {
                    IOUtil.close(writer);
                    IOUtil.close(reader);
                }
                fileWriteIfDiffers(os);
            } else if (resource.isFiltering()) {

                MavenFileFilterRequest req = setupRequest(resource, source, file);

                try {
                    fileFilter.copyFile(req);
                } catch (MavenFilteringException e) {
                    throw new MojoExecutionException("Error filtering resource: " + source, e);
                }
            } else {
                FileUtils.copyFile(source, file);
            }

            // exclude the original (so eclipse doesn't complain about duplicate resources)
            resource.addExclude(relFileName);

            return true;
        }

    }
    return false;
}

From source file:org.apache.maven.plugin.resources.remote.ProcessRemoteResourcesMojo.java

protected void processResourceBundles(RemoteResourcesClassLoader classLoader, VelocityContext context)
        throws MojoExecutionException {
    InputStreamReader reader = null;

    try {/*from w w  w.  java2  s  .  co  m*/
        // CHECKSTYLE_OFF: LineLength
        for (Enumeration<URL> e = classLoader.getResources(BundleRemoteResourcesMojo.RESOURCES_MANIFEST); e
                .hasMoreElements();) {
            URL url = e.nextElement();

            try {
                reader = new InputStreamReader(url.openStream());

                RemoteResourcesBundleXpp3Reader bundleReader = new RemoteResourcesBundleXpp3Reader();

                RemoteResourcesBundle bundle = bundleReader.read(reader);

                for (String bundleResource : bundle.getRemoteResources()) {
                    String projectResource = bundleResource;

                    boolean doVelocity = false;
                    if (projectResource.endsWith(TEMPLATE_SUFFIX)) {
                        projectResource = projectResource.substring(0, projectResource.length() - 3);
                        doVelocity = true;
                    }

                    // Don't overwrite resource that are already being provided.

                    File f = new File(outputDirectory, projectResource);

                    FileUtils.mkdir(f.getParentFile().getAbsolutePath());

                    if (!copyResourceIfExists(f, projectResource, context)) {
                        if (doVelocity) {
                            DeferredFileOutputStream os = new DeferredFileOutputStream(
                                    velocityFilterInMemoryThreshold, f);
                            Writer writer;
                            if (bundle.getSourceEncoding() == null) {
                                writer = new OutputStreamWriter(os);
                            } else {
                                writer = new OutputStreamWriter(os, bundle.getSourceEncoding());
                            }

                            try {
                                if (bundle.getSourceEncoding() == null) {
                                    // TODO: Is this correct? Shouldn't we behave like the rest of maven and fail
                                    // down to JVM default instead ISO-8859-1 ?
                                    velocity.mergeTemplate(bundleResource, "ISO-8859-1", context, writer);
                                } else {
                                    velocity.mergeTemplate(bundleResource, bundle.getSourceEncoding(), context,
                                            writer);

                                }
                            } finally {
                                IOUtil.close(writer);
                            }
                            fileWriteIfDiffers(os);
                        } else {
                            URL resUrl = classLoader.getResource(bundleResource);
                            if (resUrl != null) {
                                FileUtils.copyURLToFile(resUrl, f);
                            }
                        }
                        File appendedResourceFile = new File(appendedResourcesDirectory, projectResource);
                        File appendedVmResourceFile = new File(appendedResourcesDirectory,
                                projectResource + ".vm");
                        if (appendedResourceFile.exists()) {
                            final InputStream in = new FileInputStream(appendedResourceFile);
                            final OutputStream append = new FileOutputStream(f, true);

                            try {
                                IOUtil.copy(in, append);
                            } finally {
                                IOUtil.close(in);
                                IOUtil.close(append);
                            }
                        } else if (appendedVmResourceFile.exists()) {
                            PrintWriter writer;
                            FileReader freader = new FileReader(appendedVmResourceFile);

                            if (bundle.getSourceEncoding() == null) {
                                writer = new PrintWriter(new FileWriter(f, true));
                            } else {
                                writer = new PrintWriter(new OutputStreamWriter(new FileOutputStream(f, true),
                                        bundle.getSourceEncoding()));
                            }

                            try {
                                Velocity.init();
                                Velocity.evaluate(context, writer, "remote-resources", freader);
                            } finally {
                                IOUtil.close(writer);
                                IOUtil.close(freader);
                            }
                        }

                    }
                }
            } finally {
                reader.close();
            }
            // CHECKSTYLE_ON: LineLength
        }
    } catch (IOException e) {
        throw new MojoExecutionException("Error finding remote resources manifests", e);
    } catch (XmlPullParserException e) {
        throw new MojoExecutionException("Error parsing remote resource bundle descriptor.", e);
    } catch (Exception e) {
        throw new MojoExecutionException("Error rendering velocity resource.", e);
    }
}

From source file:org.codelibs.fess.crawler.client.http.HcHttpClient.java

protected ResponseData processHttpMethod(final String url, final HttpUriRequest httpRequest) {
    try {// w  ww.j  ava2  s. co m
        processRobotsTxt(url);
    } catch (final CrawlingAccessException e) {
        if (logger.isInfoEnabled()) {
            final StringBuilder buf = new StringBuilder(100);
            buf.append(e.getMessage());
            if (e.getCause() != null) {
                buf.append(e.getCause().getMessage());
            }
            logger.info(buf.toString());
        } else if (logger.isDebugEnabled()) {
            logger.debug("Crawling Access Exception at " + url, e);
        }
    }

    // request header
    for (final Header header : requestHeaderList) {
        httpRequest.addHeader(header);
    }

    ResponseData responseData = new ResponseData();
    HttpEntity httpEntity = null;
    try {
        // get a content
        final HttpResponse response = executeHttpClient(httpRequest);
        httpEntity = response.getEntity();

        final int httpStatusCode = response.getStatusLine().getStatusCode();
        // redirect
        if (isRedirectHttpStatus(httpStatusCode)) {
            final Header locationHeader = response.getFirstHeader("location");
            if (locationHeader == null) {
                logger.warn("Invalid redirect location at " + url);
            } else {
                responseData = new ResponseData();
                responseData.setRedirectLocation(locationHeader.getValue());
                return responseData;
            }
        }

        String contentType = null;
        final Header contentTypeHeader = response.getFirstHeader("Content-Type");
        if (contentTypeHeader != null) {
            contentType = contentTypeHeader.getValue();
            final int idx = contentType.indexOf(';');
            if (idx > 0) {
                contentType = contentType.substring(0, idx);
                if (APPLICATION_OCTET_STREAM.equals(contentType)) {
                    contentType = null;
                }
            }
        }

        long contentLength = 0;
        String contentEncoding = Constants.UTF_8;
        if (httpEntity == null) {
            responseData.setResponseBody(new byte[0]);
            if (contentType == null) {
                contentType = defaultMimeType;
            }
        } else {
            final InputStream responseBodyStream = httpEntity.getContent();
            final File outputFile = File.createTempFile("crawler-HcHttpClient-", ".out");
            DeferredFileOutputStream dfos = null;
            try {
                try {
                    dfos = new DeferredFileOutputStream((int) maxCachedContentSize, outputFile);
                    CopyUtil.copy(responseBodyStream, dfos);
                    dfos.flush();
                } finally {
                    IOUtils.closeQuietly(dfos);
                }
            } catch (final Exception e) {
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
                throw e;
            }

            if (dfos.isInMemory()) {
                responseData.setResponseBody(dfos.getData());
                contentLength = dfos.getData().length;
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
                if (contentType == null) {
                    try (InputStream is = new ByteArrayInputStream(dfos.getData())) {
                        contentType = mimeTypeHelper.getContentType(is, url);
                    } catch (Exception e) {
                        logger.debug("Failed to detect mime-type.", e);
                        contentType = defaultMimeType;
                    }
                }
            } else {
                responseData.setResponseBody(outputFile, true);
                contentLength = outputFile.length();
                if (contentType == null) {
                    try (InputStream is = new FileInputStream(outputFile)) {
                        contentType = mimeTypeHelper.getContentType(is, url);
                    } catch (Exception e) {
                        logger.debug("Failed to detect mime-type.", e);
                        contentType = defaultMimeType;
                    }
                }
            }

            final Header contentEncodingHeader = httpEntity.getContentEncoding();
            if (contentEncodingHeader != null) {
                contentEncoding = contentEncodingHeader.getValue();
            }
        }

        // check file size
        if (contentLengthHelper != null) {
            final long maxLength = contentLengthHelper.getMaxLength(contentType);
            if (contentLength > maxLength) {
                throw new MaxLengthExceededException("The content length (" + contentLength + " byte) is over "
                        + maxLength + " byte. The url is " + url);
            }
        }

        responseData.setUrl(url);
        responseData.setCharSet(contentEncoding);
        if (httpRequest instanceof HttpHead) {
            responseData.setMethod(Constants.HEAD_METHOD);
        } else {
            responseData.setMethod(Constants.GET_METHOD);
        }
        responseData.setHttpStatusCode(httpStatusCode);
        for (final Header header : response.getAllHeaders()) {
            responseData.addMetaData(header.getName(), header.getValue());
        }
        responseData.setMimeType(contentType);
        final Header contentLengthHeader = response.getFirstHeader("Content-Length");
        if (contentLengthHeader == null) {
            responseData.setContentLength(contentLength);
        } else {
            final String value = contentLengthHeader.getValue();
            try {
                responseData.setContentLength(Long.parseLong(value));
            } catch (final Exception e) {
                responseData.setContentLength(contentLength);
            }
        }
        checkMaxContentLength(responseData);
        final Header lastModifiedHeader = response.getFirstHeader("Last-Modified");
        if (lastModifiedHeader != null) {
            final String value = lastModifiedHeader.getValue();
            if (StringUtil.isNotBlank(value)) {
                final Date d = parseLastModified(value);
                if (d != null) {
                    responseData.setLastModified(d);
                }
            }
        }

        return responseData;
    } catch (final UnknownHostException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("Unknown host(" + e.getMessage() + "): " + url, e);
    } catch (final NoRouteToHostException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("No route to host(" + e.getMessage() + "): " + url, e);
    } catch (final ConnectException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("Connection time out(" + e.getMessage() + "): " + url, e);
    } catch (final SocketException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("Socket exception(" + e.getMessage() + "): " + url, e);
    } catch (final IOException e) {
        closeResources(httpRequest, responseData);
        throw new CrawlingAccessException("I/O exception(" + e.getMessage() + "): " + url, e);
    } catch (final CrawlerSystemException e) {
        closeResources(httpRequest, responseData);
        throw e;
    } catch (final Exception e) {
        closeResources(httpRequest, responseData);
        throw new CrawlerSystemException("Failed to access " + url, e);
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}

From source file:org.codelibs.fess.crawler.extractor.impl.TikaExtractor.java

protected String getContent(final ContentWriter out, String encoding) throws TikaException {
    File tempFile = null;/*  w w w . j av  a2s  .  c  o m*/
    try {
        tempFile = File.createTempFile("tika", ".tmp");
    } catch (IOException e) {
        throw new CrawlerSystemException("Failed to create a temp file.", e);
    }

    final String enc = encoding == null ? Constants.UTF_8 : encoding;
    try (DeferredFileOutputStream dfos = new DeferredFileOutputStream(memorySize, tempFile)) {
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(dfos, enc));
        out.accept(writer);
        writer.flush();

        try (Reader reader = new InputStreamReader(getContentStream(dfos), enc)) {
            return TextUtil.normalizeText(reader, initialBufferSize, maxAlphanumTermSize, maxSymbolTermSize,
                    replaceDuplication);
        }
    } catch (TikaException e) {
        throw e;
    } catch (Exception e) {
        throw new ExtractException("Failed to read a content.", e);
    } finally {
        if (tempFile.exists() && !tempFile.delete()) {
            logger.warn("Failed to delete " + tempFile.getAbsolutePath());
        }
    }
}

From source file:org.codelibs.robot.client.http.HcHttpClient.java

protected ResponseData processHttpMethod(final String url, final HttpUriRequest httpRequest) {
    try {//  ww  w.jav a2 s. c  om
        processRobotsTxt(url);
    } catch (final RobotCrawlAccessException e) {
        if (logger.isInfoEnabled()) {
            final StringBuilder buf = new StringBuilder();
            buf.append(e.getMessage());
            if (e.getCause() != null) {
                buf.append(e.getCause().getMessage());
            }
            logger.info(buf.toString());
        } else if (logger.isDebugEnabled()) {
            logger.debug("Crawling Access Exception at " + url, e);
        }
    }

    // request header
    for (final Header header : requestHeaderList) {
        httpRequest.addHeader(header);
    }

    ResponseData responseData = null;
    InputStream inputStream = null;
    HttpEntity httpEntity = null;
    try {
        // get a content
        final HttpResponse response = executeHttpClient(httpRequest);
        httpEntity = response.getEntity();

        final int httpStatusCode = response.getStatusLine().getStatusCode();
        // redirect
        if (isRedirectHttpStatus(httpStatusCode)) {
            final Header locationHeader = response.getFirstHeader("location");
            if (locationHeader == null) {
                logger.warn("Invalid redirect location at " + url);
            } else {
                responseData = new ResponseData();
                responseData.setRedirectLocation(locationHeader.getValue());
                return responseData;
            }
        }

        long contentLength = 0;
        String contentEncoding = Constants.UTF_8;
        if (httpEntity == null) {
            inputStream = new ByteArrayInputStream(new byte[0]);
        } else {
            final InputStream responseBodyStream = httpEntity.getContent();
            final File outputFile = File.createTempFile("s2robot-HcHttpClient-", ".out");
            DeferredFileOutputStream dfos = null;
            try {
                try {
                    dfos = new DeferredFileOutputStream(responseBodyInMemoryThresholdSize, outputFile);
                    CopyUtil.copy(responseBodyStream, dfos);
                    dfos.flush();
                } finally {
                    IOUtils.closeQuietly(dfos);
                }
            } catch (final Exception e) {
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
                throw e;
            }

            if (dfos.isInMemory()) {
                inputStream = new ByteArrayInputStream(dfos.getData());
                contentLength = dfos.getData().length;
                if (!outputFile.delete()) {
                    logger.warn("Could not delete " + outputFile.getAbsolutePath());
                }
            } else {
                inputStream = new TemporaryFileInputStream(outputFile);
                contentLength = outputFile.length();
            }

            final Header contentEncodingHeader = httpEntity.getContentEncoding();
            if (contentEncodingHeader != null) {
                contentEncoding = contentEncodingHeader.getValue();
            }
        }

        String contentType = null;
        final Header contentTypeHeader = response.getFirstHeader("Content-Type");
        if (contentTypeHeader != null) {
            contentType = contentTypeHeader.getValue();
            final int idx = contentType.indexOf(';');
            if (idx > 0) {
                contentType = contentType.substring(0, idx);
            }
        }

        // check file size
        if (contentLengthHelper != null) {
            final long maxLength = contentLengthHelper.getMaxLength(contentType);
            if (contentLength > maxLength) {
                throw new MaxLengthExceededException("The content length (" + contentLength + " byte) is over "
                        + maxLength + " byte. The url is " + url);
            }
        }

        responseData = new ResponseData();
        responseData.setUrl(url);
        responseData.setCharSet(contentEncoding);
        if (httpRequest instanceof HttpHead) {
            responseData.setMethod(Constants.HEAD_METHOD);
        } else {
            responseData.setMethod(Constants.GET_METHOD);
        }
        responseData.setResponseBody(inputStream);
        responseData.setHttpStatusCode(httpStatusCode);
        for (final Header header : response.getAllHeaders()) {
            responseData.addMetaData(header.getName(), header.getValue());
        }
        if (contentType == null) {
            responseData.setMimeType(defaultMimeType);
        } else {
            responseData.setMimeType(contentType);
        }
        final Header contentLengthHeader = response.getFirstHeader("Content-Length");
        if (contentLengthHeader == null) {
            responseData.setContentLength(contentLength);
        } else {
            final String value = contentLengthHeader.getValue();
            try {
                responseData.setContentLength(Long.parseLong(value));
            } catch (final Exception e) {
                responseData.setContentLength(contentLength);
            }
        }
        final Header lastModifiedHeader = response.getFirstHeader("Last-Modified");
        if (lastModifiedHeader != null) {
            final String value = lastModifiedHeader.getValue();
            if (StringUtil.isNotBlank(value)) {
                final Date d = parseLastModified(value);
                if (d != null) {
                    responseData.setLastModified(d);
                }
            }
        }

        return responseData;
    } catch (final UnknownHostException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("Unknown host(" + e.getMessage() + "): " + url, e);
    } catch (final NoRouteToHostException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("No route to host(" + e.getMessage() + "): " + url, e);
    } catch (final ConnectException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("Connection time out(" + e.getMessage() + "): " + url, e);
    } catch (final SocketException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("Socket exception(" + e.getMessage() + "): " + url, e);
    } catch (final IOException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotCrawlAccessException("I/O exception(" + e.getMessage() + "): " + url, e);
    } catch (final RobotSystemException e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw e;
    } catch (final Exception e) {
        httpRequest.abort();
        IOUtils.closeQuietly(inputStream);
        throw new RobotSystemException("Failed to access " + url, e);
    } finally {
        EntityUtils.consumeQuietly(httpEntity);
    }
}