Skip to content

Commit 878d05a

Browse files
committed
Fix ObjectFileWriter configuration for first file.
Previously, setters would only take effect for subsequently opened files. Regarding `ObjectFileWriterCompressionTest`: It only passed due to file names automatically triggering the expected compression.
1 parent 6e955f7 commit 878d05a

File tree

2 files changed

+23
-26
lines changed

2 files changed

+23
-26
lines changed

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

Lines changed: 20 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +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 firstObject;
49+
private boolean firstObject = true;
5050
private boolean closed;
5151

5252
private String encoding = "UTF-8";
@@ -59,12 +59,6 @@ public final class ObjectFileWriter<T> extends AbstractObjectWriter<T> {
5959
*/
6060
public ObjectFileWriter(final String path) {
6161
this.path = path;
62-
startNewFile();
63-
64-
final Matcher matcher = VAR_PATTERN.matcher(this.path);
65-
if (!matcher.find()) {
66-
this.path = this.path + VAR;
67-
}
6862
}
6963

7064
@Override
@@ -97,13 +91,13 @@ public void process(final T obj) {
9791
assert !closed;
9892
try {
9993
if (firstObject) {
100-
writer.write(getHeader());
94+
getWriter().write(getHeader());
10195
firstObject = false;
10296
}
10397
else {
104-
writer.write(getSeparator());
98+
getWriter().write(getSeparator());
10599
}
106-
writer.write(obj.toString());
100+
getWriter().write(obj.toString());
107101
}
108102
catch (final IOException e) {
109103
throw new MetafactureException(e);
@@ -112,20 +106,7 @@ public void process(final T obj) {
112106

113107
@Override
114108
public void resetStream() {
115-
if (!closed) {
116-
try {
117-
if (!firstObject) {
118-
writer.write(getFooter());
119-
}
120-
writer.close();
121-
}
122-
catch (final IOException e) {
123-
throw new MetafactureException(e);
124-
}
125-
finally {
126-
closed = true;
127-
}
128-
}
109+
closeStream();
129110
startNewFile();
130111
++count;
131112
}
@@ -135,9 +116,9 @@ public void closeStream() {
135116
if (!closed) {
136117
try {
137118
if (!firstObject) {
138-
writer.write(getFooter());
119+
getWriter().write(getFooter());
139120
}
140-
writer.close();
121+
getWriter().close();
141122
}
142123
catch (final IOException e) {
143124
throw new MetafactureException(e);
@@ -175,4 +156,17 @@ private void startNewFile() {
175156
}
176157
}
177158

159+
private Writer getWriter() {
160+
if (writer == null) {
161+
startNewFile();
162+
163+
final Matcher matcher = VAR_PATTERN.matcher(this.path);
164+
if (!matcher.find()) {
165+
this.path = this.path + VAR;
166+
}
167+
}
168+
169+
return writer;
170+
}
171+
178172
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public final class ObjectFileWriterCompressionTest {
4747
private static final String FILENAME_BZ2 = "compressed.txt.bz2";
4848
private static final String FILENAME_BZIP2 = "compressed.txt.bzip2";
4949
private static final String FILENAME_GZ = "compressed.txt.gz";
50+
private static final String FILENAME_GZ_NOAUTO = "compressed.txt.gz.noauto";
5051
private static final String FILENAME_GZIP = "compressed.txt.gzip";
5152
private static final String FILENAME_XZ = "compressed.txt.xz";
5253

@@ -76,11 +77,13 @@ public static Iterable<Object[]> data() {
7677
{ FILENAME_BZ2, FileCompression.AUTO, MAGIC_BYTES_BZIP2 },
7778
{ FILENAME_BZIP2, FileCompression.AUTO, MAGIC_BYTES_BZIP2 },
7879
{ FILENAME_GZ, FileCompression.AUTO, MAGIC_BYTES_GZIP },
80+
{ FILENAME_GZ_NOAUTO, FileCompression.AUTO, MAGIC_BYTES_NONE },
7981
{ FILENAME_GZIP, FileCompression.AUTO, MAGIC_BYTES_GZIP },
8082
{ FILENAME_XZ, FileCompression.AUTO, MAGIC_BYTES_XZ },
8183
{ FILENAME_NONE, FileCompression.NONE, MAGIC_BYTES_NONE },
8284
{ FILENAME_BZ2, FileCompression.BZIP2, MAGIC_BYTES_BZIP2 },
8385
{ FILENAME_GZ, FileCompression.GZIP, MAGIC_BYTES_GZIP },
86+
{ FILENAME_GZ_NOAUTO, FileCompression.GZIP, MAGIC_BYTES_GZIP },
8487
{ FILENAME_XZ, FileCompression.XZ, MAGIC_BYTES_XZ },
8588
});
8689
}

0 commit comments

Comments
 (0)