1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.commons.compress.parallel;
20
21 import java.io.File;
22 import java.io.FileNotFoundException;
23 import java.io.IOException;
24 import java.io.InputStream;
25 import java.io.OutputStream;
26 import java.io.UncheckedIOException;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29
30
31
32
33
34
35 public class FileBasedScatterGatherBackingStore implements ScatterGatherBackingStore {
36 private final Path target;
37 private final OutputStream outputStream;
38 private boolean closed;
39
40
41
42
43
44
45
46 public FileBasedScatterGatherBackingStore(final File target) throws FileNotFoundException {
47 this(target.toPath());
48 }
49
50
51
52
53
54
55
56
57 public FileBasedScatterGatherBackingStore(final Path target) throws FileNotFoundException {
58 this.target = target;
59 try {
60 outputStream = Files.newOutputStream(target);
61 } catch (final FileNotFoundException ex) {
62 throw ex;
63 } catch (final IOException ex) {
64
65 throw new UncheckedIOException(ex);
66 }
67 }
68
69 @Override
70 public void close() throws IOException {
71 try {
72 closeForWriting();
73 } finally {
74 Files.deleteIfExists(target);
75 }
76 }
77
78 @Override
79 public void closeForWriting() throws IOException {
80 if (!closed) {
81 outputStream.close();
82 closed = true;
83 }
84 }
85
86 @Override
87 public InputStream getInputStream() throws IOException {
88 return Files.newInputStream(target);
89 }
90
91 @Override
92 public void writeOut(final byte[] data, final int offset, final int length) throws IOException {
93 outputStream.write(data, offset, length);
94 }
95 }