Skip to content

Commit 62b0627

Browse files
committed
[update][plugin][txtfilewriter] Made minor code changes and removed unused code.
1 parent 7aa16da commit 62b0627

File tree

1 file changed

+17
-36
lines changed
  • plugin/writer/txtfilewriter/src/main/java/com/wgzhao/addax/plugin/writer/txtfilewriter

1 file changed

+17
-36
lines changed

Diff for: plugin/writer/txtfilewriter/src/main/java/com/wgzhao/addax/plugin/writer/txtfilewriter/TxtFileWriter.java

+17-36
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,11 @@
3434
import org.slf4j.LoggerFactory;
3535

3636
import java.io.File;
37-
import java.io.FileOutputStream;
3837
import java.io.FilenameFilter;
3938
import java.io.IOException;
4039
import java.io.OutputStream;
40+
import java.nio.file.Files;
41+
import java.nio.file.Paths;
4142
import java.util.ArrayList;
4243
import java.util.Arrays;
4344
import java.util.HashSet;
@@ -56,6 +57,7 @@
5657
import static com.wgzhao.addax.common.spi.ErrorCode.EXECUTE_FAIL;
5758
import static com.wgzhao.addax.common.spi.ErrorCode.ILLEGAL_VALUE;
5859
import static com.wgzhao.addax.common.spi.ErrorCode.IO_ERROR;
60+
import static com.wgzhao.addax.common.spi.ErrorCode.NOT_SUPPORT_TYPE;
5961
import static com.wgzhao.addax.common.spi.ErrorCode.PERMISSION_ERROR;
6062
import static com.wgzhao.addax.common.spi.ErrorCode.REQUIRED_VALUE;
6163
import static com.wgzhao.addax.common.spi.ErrorCode.RUNTIME_ERROR;
@@ -84,7 +86,7 @@ public void init()
8486
this.writerSliceConfig.set(DATE_FORMAT, dateFormatOld);
8587
}
8688
if (null != dateFormatOld) {
87-
LOG.warn("You are using format to configure date format, this is not recommended, please use dateFormat to configure. If both dateFormat and format exist, dateFormat will be used.");
89+
LOG.warn("The `format` item has been deprecated; please use `dateFormat` for configuration.");
8890
}
8991
StorageWriterUtil.validateParameter(this.writerSliceConfig);
9092
}
@@ -103,12 +105,15 @@ private void validateParameter()
103105
String.format("The path [%s] is a file, not a directory.", path));
104106
}
105107

106-
if (!dir.exists()) {
107-
if (!dir.mkdirs()) {
108-
throw AddaxException.asAddaxException(
109-
EXECUTE_FAIL,
110-
String.format("Failed to create directory [%s].", path));
111-
}
108+
if (!dir.exists() && !dir.mkdirs()) {
109+
throw AddaxException.asAddaxException(
110+
EXECUTE_FAIL,
111+
"Failed to create directory: " + path);
112+
}
113+
else if (!dir.canWrite()) {
114+
throw AddaxException.asAddaxException(
115+
PERMISSION_ERROR,
116+
"No write permission on directory: " + path);
112117
}
113118
}
114119

@@ -119,7 +124,6 @@ public void prepare()
119124
String fileName = this.writerSliceConfig.getString(FILE_NAME);
120125
String writeMode = this.writerSliceConfig.getString(WRITE_MODE);
121126

122-
assert FileHelper.checkDirectoryWritable(path);
123127
File dir = new File(path);
124128
// truncate option handler
125129
if ("truncate".equalsIgnoreCase(writeMode) || "overwrite".equalsIgnoreCase(writeMode)) {
@@ -138,10 +142,6 @@ public void prepare()
138142
throw AddaxException.asAddaxException(RUNTIME_ERROR,
139143
String.format("NullPointException occurred when clean history files under this path [%s].", path), npe);
140144
}
141-
catch (IllegalArgumentException iae) {
142-
throw AddaxException.asAddaxException(PERMISSION_ERROR,
143-
String.format("IllegalArgumentException occurred when clean history files under this path [%s].", path), iae);
144-
}
145145
catch (IOException e) {
146146
throw AddaxException.asAddaxException(IO_ERROR,
147147
String.format("IOException occurred when clean history files under this path [%s].", path), e);
@@ -156,7 +156,7 @@ else if ("nonConflict".equals(writeMode)) {
156156
if (dir.exists()) {
157157
if (!dir.canRead()) {
158158
throw AddaxException.asAddaxException(PERMISSION_ERROR,
159-
String.format("Permission denied to access path [%s].", path));
159+
"Permission denied to access path " + path);
160160
}
161161
// fileName is not null
162162
FilenameFilter filter = new PrefixFileFilter(fileName);
@@ -172,26 +172,13 @@ else if ("nonConflict".equals(writeMode)) {
172172
String.format("The directory [%s] contains files.", path));
173173
}
174174
}
175-
else {
176-
boolean createdOk = dir.mkdirs();
177-
if (!createdOk) {
178-
throw AddaxException.asAddaxException(EXECUTE_FAIL,
179-
String.format("Failed to create the file [%s].", path));
180-
}
181-
}
182175
}
183176
else {
184-
throw AddaxException.asAddaxException(ILLEGAL_VALUE,
185-
String.format("ONLY support truncate, append and nonConflict as writeMode, but you give [%s].", writeMode));
177+
throw AddaxException.asAddaxException(NOT_SUPPORT_TYPE,
178+
"Only 'truncate', 'append', and 'nonConflict' are supported as write modes, but you provided " + writeMode);
186179
}
187180
}
188181

189-
@Override
190-
public void post()
191-
{
192-
//
193-
}
194-
195182
@Override
196183
public void destroy()
197184
{
@@ -258,7 +245,7 @@ public void startWrite(RecordReceiver lineReceiver)
258245
try {
259246
File newFile = new File(fileFullPath);
260247
assert newFile.createNewFile();
261-
outputStream = new FileOutputStream(newFile);
248+
outputStream = Files.newOutputStream(newFile.toPath());
262249
StorageWriterUtil.writeToStream(lineReceiver, outputStream, this.writerSliceConfig, this.fileName,
263250
this.getTaskPluginCollector());
264251
}
@@ -276,12 +263,6 @@ public void startWrite(RecordReceiver lineReceiver)
276263
LOG.info("end do write");
277264
}
278265

279-
@Override
280-
public void post()
281-
{
282-
//
283-
}
284-
285266
@Override
286267
public void destroy()
287268
{

0 commit comments

Comments
 (0)