1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.waarp.openr66.context.task;
21
22 import org.waarp.common.file.AbstractDir;
23 import org.waarp.common.file.FileUtils;
24 import org.waarp.common.logging.WaarpLogger;
25 import org.waarp.common.logging.WaarpLoggerFactory;
26 import org.waarp.common.utility.ParametersChecker;
27 import org.waarp.common.utility.WaarpStringUtils;
28 import org.waarp.openr66.context.R66Session;
29 import org.waarp.openr66.context.task.exception.OpenR66RunnerException;
30
31 import java.io.File;
32 import java.io.FileOutputStream;
33 import java.io.IOException;
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class UnzeroedFileTask extends AbstractTask {
49
50
51
52 private static final WaarpLogger logger =
53 WaarpLoggerFactory.getLogger(UnzeroedFileTask.class);
54
55
56
57
58
59
60
61 public UnzeroedFileTask(final String argRule, final int delay,
62 final String argTransfer, final R66Session session) {
63 super(TaskType.UNZEROED, delay, argRule, argTransfer, session);
64 }
65
66 @Override
67 public final void run() {
68 final File currentFile = session.getFile().getTrueFile();
69 final String toWrite = ParametersChecker.isEmpty(argRule)? " " : argRule;
70 final String curpath =
71 AbstractDir.normalizePath(currentFile.getAbsolutePath());
72 if (currentFile.exists() && currentFile.length() == 0) {
73 FileOutputStream out = null;
74 try {
75 out = new FileOutputStream(currentFile);
76 out.write(toWrite.getBytes(WaarpStringUtils.UTF8));
77 if (delay > 0) {
78 if (delay > 1) {
79 logger.warn("Unzeroed File: " + curpath + " from " + session);
80 } else {
81 logger.info("Unzeroed File: {} from {}", curpath, session);
82 }
83 }
84 futureCompletion.setSuccess();
85 } catch (final IOException e) {
86 logger.error("Cannot unzeroed File: " + curpath + " from " + session);
87 futureCompletion.setFailure(
88 new OpenR66RunnerException("File not Unzeroed"));
89 } finally {
90 FileUtils.close(out);
91 }
92 return;
93 }
94 if (delay > 0) {
95 if (delay > 1) {
96 logger.warn(
97 "Unzeroed File not applicable to " + curpath + " from " + session);
98 } else {
99 logger.info("Unzeroed File not applicable to {} from {}", curpath,
100 session);
101 }
102 }
103 futureCompletion.setSuccess();
104 }
105
106 }