001/**
002 * The contents of this file are subject to the license and copyright
003 * detailed in the LICENSE and NOTICE files at the root of the source
004 * tree.
005 *
006 */
007package org.fcrepo.migration.foxml;
008
009import java.io.BufferedInputStream;
010import java.io.File;
011import java.io.FileInputStream;
012import java.io.IOException;
013import java.io.InputStream;
014import java.util.Optional;
015
016/**
017 * A CashedContent implementation that exposes content stored in a
018 * file.
019 * @author mdurbin
020 */
021public class FileCachedContent implements CachedContent {
022
023    private File file;
024
025    /**
026     * File cached content
027     * @param file the file
028     */
029    public FileCachedContent(final File file) {
030        this.file = file;
031    }
032
033    @Override
034    public InputStream getInputStream() throws IOException {
035        if (!file.exists()) {
036            throw new IllegalStateException("Cached content is not available.");
037        }
038        return new BufferedInputStream(new FileInputStream(file));
039    }
040
041    @Override
042    public Optional<File> getFile() {
043        return Optional.ofNullable(file);
044    }
045}