# Source code

    001/*
    002 *  Licensed to the Apache Software Foundation (ASF) under one or more
    003 *  contributor license agreements.  See the NOTICE file distributed with
    004 *  this work for additional information regarding copyright ownership.
    005 *  The ASF licenses this file to You under the Apache License, Version 2.0
    006 *  (the "License"); you may not use this file except in compliance with
    007 *  the License.  You may obtain a copy of the License at
    008 *
    009 *      http://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 *  Unless required by applicable law or agreed to in writing, software
    012 *  distributed under the License is distributed on an "AS IS" BASIS,
    013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 *  See the License for the specific language governing permissions and
    015 *  limitations under the License.
    016 *
    017 */
    018package org.apache.commons.compress.parallel;
    019
    020import java.io.File;
    021import java.io.FileNotFoundException;
    022import java.io.FileInputStream;
    023import java.io.FileOutputStream;
    024import java.io.IOException;
    025import java.io.InputStream;
    026
    027/**
    028 * ScatterGatherBackingStore that is backed by a file.
    029 *
    030 * @since 1.10
    031 */
    032public class FileBasedScatterGatherBackingStore implements ScatterGatherBackingStore {
    033    private final File target;
    034    private final FileOutputStream os;
    035    private boolean closed;
    036
    037    public FileBasedScatterGatherBackingStore(final File target) throws FileNotFoundException {
    038        this.target = target;
    039        os = new FileOutputStream(target);
    040    }
    041
    042    @Override
    043    public InputStream getInputStream() throws IOException {
    044        return new FileInputStream(target);
    045    }
    046
    047    @Override
    048    @SuppressWarnings("ResultOfMethodCallIgnored")
    049    public void closeForWriting() throws IOException {
    050        if (!closed) {
    051            os.close();
    052            closed = true;
    053        }
    054    }
    055
    056    @Override
    057    public void writeOut(final byte[] data, final int offset, final int length) throws IOException {
    058        os.write(data, offset, length);
    059    }
    060
    061    @Override
    062    public void close() throws IOException {
    063        closeForWriting();
    064        target.delete();
    065    }
    066}