FileBasedScatterGatherBackingStore.java

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

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

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

  37.     /**
  38.      * Constructs a new instance.
  39.      *
  40.      * @param target The path to offload compressed data into.
  41.      * @throws FileNotFoundException if the file doesn't exist.
  42.      */
  43.     public FileBasedScatterGatherBackingStore(final File target) throws FileNotFoundException {
  44.         this(target.toPath());
  45.     }

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

  64.     @Override
  65.     public void close() throws IOException {
  66.         try {
  67.             closeForWriting();
  68.         } finally {
  69.             Files.deleteIfExists(target);
  70.         }
  71.     }

  72.     @Override
  73.     public void closeForWriting() throws IOException {
  74.         if (!closed) {
  75.             outputStream.close();
  76.             closed = true;
  77.         }
  78.     }

  79.     @Override
  80.     public InputStream getInputStream() throws IOException {
  81.         return Files.newInputStream(target);
  82.     }

  83.     @Override
  84.     public void writeOut(final byte[] data, final int offset, final int length) throws IOException {
  85.         outputStream.write(data, offset, length);
  86.     }
  87. }