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 */
017package org.apache.commons.compress.parallel;
018
019import java.io.File;
020import java.io.FileNotFoundException;
021import java.io.IOException;
022import java.io.InputStream;
023import java.io.OutputStream;
024import java.io.UncheckedIOException;
025import java.nio.file.Files;
026import java.nio.file.Path;
027
028/**
029 * ScatterGatherBackingStore that is backed by a path.
030 *
031 * @since 1.10
032 */
033public class FileBasedScatterGatherBackingStore implements ScatterGatherBackingStore {
034    private final Path target;
035    private final OutputStream outputStream;
036    private boolean closed;
037
038    public FileBasedScatterGatherBackingStore(final File target) throws FileNotFoundException {
039        this(target.toPath());
040    }
041
042    /**
043     * Constructs a new instance for the given path.
044     *
045     * @param target The path to offload compressed data into.
046     * @throws FileNotFoundException if the file doesn't exist
047     * @since 1.22
048     */
049    public FileBasedScatterGatherBackingStore(final Path target) throws FileNotFoundException {
050        this.target = target;
051        try {
052            outputStream = Files.newOutputStream(target);
053        } catch (final FileNotFoundException ex) {
054            throw ex;
055        } catch (final IOException ex) {
056            // must convert exception to stay backwards compatible with Compress 1.10 to 1.13
057            throw new UncheckedIOException(ex); // NOSONAR
058        }
059    }
060
061    @Override
062    public void close() throws IOException {
063        try {
064            closeForWriting();
065        } finally {
066            Files.deleteIfExists(target);
067        }
068    }
069
070    @Override
071    public void closeForWriting() throws IOException {
072        if (!closed) {
073            outputStream.close();
074            closed = true;
075        }
076    }
077
078    @Override
079    public InputStream getInputStream() throws IOException {
080        return Files.newInputStream(target);
081    }
082
083    @Override
084    public void writeOut(final byte[] data, final int offset, final int length) throws IOException {
085        outputStream.write(data, offset, length);
086    }
087}