001/*
002 * Copyright 2015 DuraSpace, Inc.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.fcrepo.migration.foxml;
017
018import java.io.File;
019import java.io.FileInputStream;
020import java.io.IOException;
021import java.io.InputStream;
022import java.util.Optional;
023
024/**
025 * A CashedContent implementation that exposes content stored in a
026 * file.
027 * @author mdurbin
028 */
029public class FileCachedContent implements CachedContent {
030
031    private File file;
032
033    /**
034     * File cached content
035     * @param file the file
036     */
037    public FileCachedContent(final File file) {
038        this.file = file;
039    }
040
041    @Override
042    public InputStream getInputStream() throws IOException {
043        if (!file.exists()) {
044            throw new IllegalStateException("Cached content is not available.");
045        }
046        return new FileInputStream(file);
047    }
048
049    @Override
050    public Optional<File> getFile() {
051        return Optional.ofNullable(file);
052    }
053}