Skip to content

Commit 31f1858

Browse files
author
mgeipel
committed
ObjectFileWriter now handles multiple closeStream events properly
1 parent 73ed775 commit 31f1858

File tree

1 file changed

+25
-16
lines changed

1 file changed

+25
-16
lines changed

src/main/java/org/culturegraph/mf/stream/sink/ObjectFileWriter.java

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.culturegraph.mf.framework.ObjectReceiver;
2828
import org.culturegraph.mf.util.FileCompression;
2929

30-
3130
/**
3231
* @param <T>
3332
* object type
@@ -44,7 +43,8 @@ public final class ObjectFileWriter<T> implements ObjectReceiver<T> {
4443
private String path;
4544
private int count;
4645
private Writer writer;
47-
46+
private boolean closed;
47+
4848
private String encoding = "UTF-8";
4949
private FileCompression compression = FileCompression.AUTO;
5050

@@ -57,7 +57,7 @@ public ObjectFileWriter(final String path) {
5757
this.path = this.path + VAR;
5858
}
5959
}
60-
60+
6161
/**
6262
* Returns the encoding used to open the resource.
6363
*
@@ -80,15 +80,15 @@ public void setEncoding(final String encoding) {
8080
public FileCompression getCompression() {
8181
return compression;
8282
}
83-
83+
8484
public void setCompression(final FileCompression compression) {
85-
this.compression = compression;
85+
this.compression = compression;
8686
}
87-
87+
8888
public void setCompression(final String compression) {
8989
setCompression(FileCompression.valueOf(compression.toUpperCase()));
9090
}
91-
91+
9292
private void startNewFile() {
9393
final Matcher matcher = VAR_PATTERN.matcher(this.path);
9494
final String path = matcher.replaceAll(String.valueOf(count));
@@ -98,6 +98,7 @@ private void startNewFile() {
9898
final OutputStream compressor = compression.createCompressor(file, path);
9999
try {
100100
writer = new OutputStreamWriter(compressor, encoding);
101+
closed = false;
101102
} catch (IOException e) {
102103
compressor.close();
103104
throw e;
@@ -113,6 +114,7 @@ private void startNewFile() {
113114

114115
@Override
115116
public void process(final T obj) {
117+
assert !closed;
116118
try {
117119
writer.write(obj.toString());
118120
writer.append('\n');
@@ -123,22 +125,29 @@ public void process(final T obj) {
123125

124126
@Override
125127
public void resetStream() {
126-
try {
127-
writer.close();
128-
} catch (IOException e) {
129-
throw new MetafactureException(e);
128+
if (!closed) {
129+
try {
130+
writer.close();
131+
} catch (IOException e) {
132+
throw new MetafactureException(e);
133+
} finally {
134+
closed = true;
135+
}
130136
}
131137
startNewFile();
132138
++count;
133139
}
134140

135141
@Override
136142
public void closeStream() {
137-
try {
138-
writer.close();
139-
} catch (IOException e) {
140-
throw new MetafactureException(e);
143+
if (!closed) {
144+
try {
145+
writer.close();
146+
} catch (IOException e) {
147+
throw new MetafactureException(e);
148+
} finally {
149+
closed = true;
150+
}
141151
}
142152
}
143-
144153
}

0 commit comments

Comments
 (0)