27
27
import org .culturegraph .mf .framework .ObjectReceiver ;
28
28
import org .culturegraph .mf .util .FileCompression ;
29
29
30
-
31
30
/**
32
31
* @param <T>
33
32
* object type
@@ -44,7 +43,8 @@ public final class ObjectFileWriter<T> implements ObjectReceiver<T> {
44
43
private String path ;
45
44
private int count ;
46
45
private Writer writer ;
47
-
46
+ private boolean closed ;
47
+
48
48
private String encoding = "UTF-8" ;
49
49
private FileCompression compression = FileCompression .AUTO ;
50
50
@@ -57,7 +57,7 @@ public ObjectFileWriter(final String path) {
57
57
this .path = this .path + VAR ;
58
58
}
59
59
}
60
-
60
+
61
61
/**
62
62
* Returns the encoding used to open the resource.
63
63
*
@@ -80,15 +80,15 @@ public void setEncoding(final String encoding) {
80
80
public FileCompression getCompression () {
81
81
return compression ;
82
82
}
83
-
83
+
84
84
public void setCompression (final FileCompression compression ) {
85
- this .compression = compression ;
85
+ this .compression = compression ;
86
86
}
87
-
87
+
88
88
public void setCompression (final String compression ) {
89
89
setCompression (FileCompression .valueOf (compression .toUpperCase ()));
90
90
}
91
-
91
+
92
92
private void startNewFile () {
93
93
final Matcher matcher = VAR_PATTERN .matcher (this .path );
94
94
final String path = matcher .replaceAll (String .valueOf (count ));
@@ -98,6 +98,7 @@ private void startNewFile() {
98
98
final OutputStream compressor = compression .createCompressor (file , path );
99
99
try {
100
100
writer = new OutputStreamWriter (compressor , encoding );
101
+ closed = false ;
101
102
} catch (IOException e ) {
102
103
compressor .close ();
103
104
throw e ;
@@ -113,6 +114,7 @@ private void startNewFile() {
113
114
114
115
@ Override
115
116
public void process (final T obj ) {
117
+ assert !closed ;
116
118
try {
117
119
writer .write (obj .toString ());
118
120
writer .append ('\n' );
@@ -123,22 +125,29 @@ public void process(final T obj) {
123
125
124
126
@ Override
125
127
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
+ }
130
136
}
131
137
startNewFile ();
132
138
++count ;
133
139
}
134
140
135
141
@ Override
136
142
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
+ }
141
151
}
142
152
}
143
-
144
153
}
0 commit comments