1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
30
31
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
44
45
46
47
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
57 throw new UncheckedIOException(ex);
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 }