Example usage for org.apache.http.impl.io SessionOutputBufferImpl flush

List of usage examples for org.apache.http.impl.io SessionOutputBufferImpl flush

Introduction

In this page you can find the example usage for org.apache.http.impl.io SessionOutputBufferImpl flush.

Prototype

public void flush() throws IOException 

Source Link

Usage

From source file:it.tidalwave.bluemarine2.downloader.impl.SimpleHttpCacheStorage.java

/*******************************************************************************************************************
 *
 * {@inheritDoc}//w  w  w.jav a  2s . c  o  m
 *
 ******************************************************************************************************************/
@Override
public void putEntry(final @Nonnull String key, final @Nonnull HttpCacheEntry entry) throws IOException {
    try {
        log.debug("putEntry({}, {})", key, entry);
        final Path cachePath = getCacheItemPath(new URL(key));
        createDirectories(cachePath);
        final Path cacheHeadersPath = cachePath.resolve(PATH_HEADERS);
        final Path cacheContentPath = cachePath.resolve(PATH_CONTENT);

        @Cleanup
        final OutputStream os = newOutputStream(cacheHeadersPath, CREATE);
        final SessionOutputBufferImpl sob = sessionOutputBufferFrom(os);
        final DefaultHttpResponseWriter writer = new DefaultHttpResponseWriter(sob);
        writer.write(responseFrom(entry));
        sob.flush();

        if (entry.getResource().length() > 0) {
            copy(entry.getResource().getInputStream(), cacheContentPath, REPLACE_EXISTING);
        }
    } catch (HttpException e) {
        throw new IOException(e);
    }
}