Skip to content

Commit 2a993b0

Browse files
committed
Optionally let ObjectFileWriter append to existing file instead of overwriting.
1 parent 878d05a commit 2a993b0

File tree

2 files changed

+55
-6
lines changed

2 files changed

+55
-6
lines changed

metafacture-io/src/main/java/org/metafacture/io/ObjectFileWriter.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public final class ObjectFileWriter<T> extends AbstractObjectWriter<T> {
4646
private String path;
4747
private int count;
4848
private Writer writer;
49+
private boolean appendIfFileExists;
4950
private boolean firstObject = true;
5051
private boolean closed;
5152

@@ -129,11 +130,26 @@ public void closeStream() {
129130
}
130131
}
131132

133+
/**
134+
* Controls whether to open files in append mode if they exist.
135+
* <p>
136+
* The default value is {@code false}.
137+
* <p>
138+
* This property can be changed anytime during processing. It becomes
139+
* effective the next time a new output file is opened.
140+
*
141+
* @param appendIfFileExists true if new data should be appended,
142+
* false to overwrite the existing file.
143+
*/
144+
public void setAppendIfFileExists(final boolean appendIfFileExists) {
145+
this.appendIfFileExists = appendIfFileExists;
146+
}
147+
132148
private void startNewFile() {
133149
final Matcher matcher = VAR_PATTERN.matcher(this.path);
134150
final String currentPath = matcher.replaceAll(String.valueOf(count));
135151
try {
136-
final OutputStream file = new FileOutputStream(currentPath);
152+
final OutputStream file = new FileOutputStream(currentPath, appendIfFileExists);
137153
try {
138154
final OutputStream compressor = compression.createCompressor(file, currentPath);
139155
try {

metafacture-io/src/test/java/org/metafacture/io/ObjectFileWriterTest.java

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,21 +53,44 @@ public final class ObjectFileWriterTest
5353
@Before
5454
public void setup() throws IOException {
5555
file = tempFolder.newFile();
56-
writer = new ObjectFileWriter<String>(file.getAbsolutePath());
56+
setWriter();
5757
}
5858

5959
@Test
6060
public void shouldWriteUTF8EncodedOutput() throws IOException {
6161
assumeFalse("Default encoding is UTF-8: It is not possible to test whether " +
62-
"ObjectFileWriter sets the encoding to UTF-8 correctly.",
62+
"ObjectFileWriter sets the encoding to UTF-8 correctly.",
6363
StandardCharsets.UTF_8.equals(Charset.defaultCharset()));
6464

6565
writer.process(DATA);
6666
writer.closeStream();
6767

68-
final byte[] bytesWritten = Files.readAllBytes(file.toPath());
69-
assertArrayEquals((DATA + "\n").getBytes(StandardCharsets.UTF_8),
70-
bytesWritten); // FileObjectWriter appends new lines
68+
assertOutput(DATA + "\n");
69+
}
70+
71+
@Test
72+
public void shouldOverwriteExistingFileByDefault() throws IOException {
73+
writer.process(DATA);
74+
writer.closeStream();
75+
76+
setWriter();
77+
writer.process(DATA);
78+
writer.closeStream();
79+
80+
assertOutput(DATA + "\n");
81+
}
82+
83+
@Test
84+
public void shouldAppendToExistingFile() throws IOException {
85+
writer.process(DATA);
86+
writer.closeStream();
87+
88+
setWriter();
89+
writer.setAppendIfFileExists(true);
90+
writer.process(DATA);
91+
writer.closeStream();
92+
93+
assertOutput(DATA + "\n" + DATA + "\n");
7194
}
7295

7396
@Override
@@ -83,4 +106,14 @@ protected String getOutput() throws IOException {
83106
}
84107
}
85108

109+
private void setWriter() {
110+
writer = new ObjectFileWriter<String>(file.getAbsolutePath());
111+
}
112+
113+
private void assertOutput(final String expected) throws IOException {
114+
final byte[] bytesWritten = Files.readAllBytes(file.toPath());
115+
assertArrayEquals(expected.getBytes(StandardCharsets.UTF_8),
116+
bytesWritten); // FileObjectWriter appends new lines
117+
}
118+
86119
}

0 commit comments

Comments
 (0)