FileBasedScatterGatherBackingStore.java

  1. /*
  2.  *  Licensed to the Apache Software Foundation (ASF) under one or more
  3.  *  contributor license agreements.  See the NOTICE file distributed with
  4.  *  this work for additional information regarding copyright ownership.
  5.  *  The ASF licenses this file to You under the Apache License, Version 2.0
  6.  *  (the "License"); you may not use this file except in compliance with
  7.  *  the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  *  Unless required by applicable law or agreed to in writing, software
  12.  *  distributed under the License is distributed on an "AS IS" BASIS,
  13.  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  *  See the License for the specific language governing permissions and
  15.  *  limitations under the License.
  16.  */
  17. package org.apache.commons.compress.parallel;

  18. import java.io.File;
  19. import java.io.FileNotFoundException;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.OutputStream;
  23. import java.io.UncheckedIOException;
  24. import java.nio.file.Files;
  25. import java.nio.file.Path;

  26. /**
  27.  * ScatterGatherBackingStore that is backed by a path.
  28.  *
  29.  * @since 1.10
  30.  */
  31. public class FileBasedScatterGatherBackingStore implements ScatterGatherBackingStore {
  32.     private final Path target;
  33.     private final OutputStream outputStream;
  34.     private boolean closed;

  35.     public FileBasedScatterGatherBackingStore(final File target) throws FileNotFoundException {
  36.         this(target.toPath());
  37.     }

  38.     /**
  39.      * Constructs a new instance for the given path.
  40.      *
  41.      * @param target The path to offload compressed data into.
  42.      * @throws FileNotFoundException if the file doesn't exist
  43.      * @since 1.22
  44.      */
  45.     public FileBasedScatterGatherBackingStore(final Path target) throws FileNotFoundException {
  46.         this.target = target;
  47.         try {
  48.             outputStream = Files.newOutputStream(target);
  49.         } catch (final FileNotFoundException ex) {
  50.             throw ex;
  51.         } catch (final IOException ex) {
  52.             // must convert exception to stay backwards compatible with Compress 1.10 to 1.13
  53.             throw new UncheckedIOException(ex); // NOSONAR
  54.         }
  55.     }

  56.     @Override
  57.     public void close() throws IOException {
  58.         try {
  59.             closeForWriting();
  60.         } finally {
  61.             Files.deleteIfExists(target);
  62.         }
  63.     }

  64.     @Override
  65.     public void closeForWriting() throws IOException {
  66.         if (!closed) {
  67.             outputStream.close();
  68.             closed = true;
  69.         }
  70.     }

  71.     @Override
  72.     public InputStream getInputStream() throws IOException {
  73.         return Files.newInputStream(target);
  74.     }

  75.     @Override
  76.     public void writeOut(final byte[] data, final int offset, final int length) throws IOException {
  77.         outputStream.write(data, offset, length);
  78.     }
  79. }