Example usage for com.google.common.io FileBackedOutputStream close

List of usage examples for com.google.common.io FileBackedOutputStream close

Introduction

In this page you can find the example usage for com.google.common.io FileBackedOutputStream close.

Prototype

@Override
    public synchronized void close() throws IOException 

Source Link

Usage

From source file:org.codice.ddf.security.crl.generator.CrlGenerator.java

/** Pulls down the CRL file provided via URL and writes it locally. */
@Override/*from   w ww  .  j  av  a 2s.  com*/
public synchronized void run() {
    if (!crlByUrlEnabled || crlLocationUrl == null) {
        return;
    }

    if (!crlLocationUrl.startsWith(HTTPS)) {
        postErrorEvent("The provided URL was not an HTTPS URL.");
        return;
    }

    Object entity = getRemoteCrl();
    if (!(entity instanceof InputStream)) {
        postErrorEvent("Unable to download the remote CRL.");
        return;
    }

    FileBackedOutputStream fileBackedOutputStream = null;
    try {
        // Read the response content and get the byte source
        ByteSource byteSource;
        try (InputStream entityStream = (InputStream) entity) {
            fileBackedOutputStream = new FileBackedOutputStream(FILE_BACKED_STREAM_THRESHOLD);
            IOUtils.copy(entityStream, fileBackedOutputStream);
            fileBackedOutputStream.close();
            byteSource = fileBackedOutputStream.asByteSource();
        }

        File crlFile = getCrlFile(byteSource);
        // Verify that the CRL is valid
        if (!crlIsValid(byteSource)) {
            postErrorEvent("An error occurred while validating the CRL.");
            return;
        }
        writeCrlToFile(byteSource, crlFile);
    } catch (IOException e) {
        LOGGER.warn("Unable to copy CRL to local CRL. {}", e.getMessage());
        postErrorEvent("An error occurred while downloading the CRL.");
    } finally {
        // Cleanup temp file
        if (fileBackedOutputStream != null) {
            try {
                fileBackedOutputStream.reset();
            } catch (IOException e) {
                LOGGER.warn("Error occurred while deleting the temporary file. {}", e.getMessage());
            }
        }
    }
}