/
ObjectFileWriter.java
188 lines (166 loc) · 5.19 KB
/
ObjectFileWriter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* Copyright 2013, 2014 Deutsche Nationalbibliothek
*
* Licensed under the Apache License, Version 2.0 the "License";
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.metafacture.io;
import org.metafacture.framework.FluxCommand;
import org.metafacture.framework.MetafactureException;
import org.metafacture.framework.annotations.In;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @param <T>
* object type
*
* @author Markus Geipel
* @author Christoph Böhme
*
*/
@In(Object.class)
@FluxCommand("write-files")
public final class ObjectFileWriter<T> extends AbstractObjectWriter<T> {
private static final String VAR = "${i}";
private static final Pattern VAR_PATTERN = Pattern.compile(VAR, Pattern.LITERAL);
private String path;
private int count;
private Writer writer;
private boolean appendIfFileExists;
private boolean firstObject = true;
private boolean closed;
private String encoding = "UTF-8";
private FileCompression compression = FileCompression.AUTO;
/**
* Sets the destination of a file to write objects to.
*
* @param path the path to be written to
*/
public ObjectFileWriter(final String path) {
this.path = path;
}
@Override
public String getEncoding() {
return encoding;
}
@Override
public void setEncoding(final String encoding) {
this.encoding = encoding;
}
@Override
public FileCompression getCompression() {
return compression;
}
@Override
public void setCompression(final FileCompression compression) {
this.compression = compression;
}
@Override
public void setCompression(final String compression) {
setCompression(FileCompression.valueOf(compression.toUpperCase()));
}
@Override
public void process(final T obj) {
assert !closed;
try {
if (firstObject) {
getWriter().write(getHeader());
firstObject = false;
}
else {
getWriter().write(getSeparator());
}
getWriter().write(obj.toString());
}
catch (final IOException e) {
throw new MetafactureException(e);
}
}
@Override
public void resetStream() {
closeStream();
++count;
startNewFile();
}
@Override
public void closeStream() {
if (!closed) {
try {
if (!firstObject) {
getWriter().write(getFooter());
}
getWriter().close();
}
catch (final IOException e) {
throw new MetafactureException(e);
}
finally {
closed = true;
}
}
}
/**
* Controls whether to open files in append mode if they exist.
* <p>
* The default value is {@code false}.
* <p>
* This property can be changed anytime during processing. It becomes
* effective the next time a new output file is opened.
*
* @param appendIfFileExists true if new data should be appended,
* false to overwrite the existing file
*/
public void setAppendIfFileExists(final boolean appendIfFileExists) {
this.appendIfFileExists = appendIfFileExists;
}
private void startNewFile() {
final Matcher matcher = VAR_PATTERN.matcher(this.path);
final String currentPath = matcher.replaceAll(String.valueOf(count));
try {
final OutputStream file = new FileOutputStream(currentPath, appendIfFileExists);
try {
final OutputStream compressor = compression.createCompressor(file, currentPath);
try {
writer = new OutputStreamWriter(compressor, encoding);
firstObject = true;
closed = false;
}
catch (final IOException e) {
compressor.close();
throw e;
}
}
catch (final IOException e) {
file.close();
throw e;
}
}
catch (final IOException e) {
throw new MetafactureException("Error creating file '" + currentPath + "'.", e);
}
}
private Writer getWriter() {
if (writer == null) {
startNewFile();
final Matcher matcher = VAR_PATTERN.matcher(this.path);
if (!matcher.find()) {
this.path = this.path + VAR;
}
}
return writer;
}
}