View Javadoc
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  
19  import java.io.File;
20  import java.io.FileNotFoundException;
21  import java.io.IOException;
22  import java.io.InputStream;
23  import java.io.OutputStream;
24  import java.io.UncheckedIOException;
25  import java.nio.file.Files;
26  import java.nio.file.Path;
27  
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      public FileBasedScatterGatherBackingStore(final File target) throws FileNotFoundException {
39          this(target.toPath());
40      }
41  
42      /**
43       * Constructs a new instance for the given path.
44       *
45       * @param target The path to offload compressed data into.
46       * @throws FileNotFoundException if the file doesn't exist
47       * @since 1.22
48       */
49      public FileBasedScatterGatherBackingStore(final Path target) throws FileNotFoundException {
50          this.target = target;
51          try {
52              outputStream = Files.newOutputStream(target);
53          } catch (final FileNotFoundException ex) {
54              throw ex;
55          } catch (final IOException ex) {
56              // must convert exception to stay backwards compatible with Compress 1.10 to 1.13
57              throw new UncheckedIOException(ex); // NOSONAR
58          }
59      }
60  
61      @Override
62      public void close() throws IOException {
63          try {
64              closeForWriting();
65          } finally {
66              Files.deleteIfExists(target);
67          }
68      }
69  
70      @Override
71      public void closeForWriting() throws IOException {
72          if (!closed) {
73              outputStream.close();
74              closed = true;
75          }
76      }
77  
78      @Override
79      public InputStream getInputStream() throws IOException {
80          return Files.newInputStream(target);
81      }
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  }