1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.workflow.io;
18
19 import java.io.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.io.InputStreamReader;
22 import java.io.IOException;
23 import org.apache.commons.workflow.Context;
24 import org.apache.commons.workflow.StepException;
25 import org.apache.commons.workflow.base.BaseStep;
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public class ReadStep extends BaseStep {
48
49
50
51
52
53
54
55
56 public ReadStep() {
57
58 super();
59
60 }
61
62
63
64
65
66
67
68 public ReadStep(String id) {
69
70 super();
71 setId(id);
72
73 }
74
75
76
77
78
79
80
81
82
83 public ReadStep(String id, String encoding, String file) {
84
85 super();
86 setId(id);
87 setEncoding(encoding);
88 setFile(file);
89
90 }
91
92
93
94
95
96
97
98
99 protected String encoding = null;
100
101 public String getEncoding() {
102 return (this.encoding);
103 }
104
105 public void setEncoding(String encoding) {
106 this.encoding = encoding;
107 }
108
109
110
111
112
113 protected String file = null;
114
115 public String getFile() {
116 return (this.file);
117 }
118
119 public void setFile(String file) {
120 this.file = file;
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135 public void execute(Context context) throws StepException {
136
137
138 FileInputStream fis = null;
139 BufferedInputStream bis = null;
140 InputStreamReader isr = null;
141 StringBuffer sb = new StringBuffer();
142 StepException se = null;
143
144 try {
145
146
147 fis = new FileInputStream(file);
148 bis = new BufferedInputStream(fis, 2048);
149 if (encoding == null)
150 isr = new InputStreamReader(bis);
151 else
152 isr = new InputStreamReader(bis, encoding);
153
154
155 while (true) {
156 int ch = isr.read();
157 if (ch < 0)
158 break;
159 sb.append((char) ch);
160 }
161
162
163 isr.close();
164 isr = null;
165 bis = null;
166 fis = null;
167
168 } catch (IOException e) {
169
170 se = new StepException("IOException processing '" + file + "'",
171 e, this);
172
173 } finally {
174
175 if (isr != null) {
176 try {
177 isr.close();
178 } catch (Throwable t) {
179 ;
180 }
181 } else if (bis != null) {
182 try {
183 bis.close();
184 } catch (Throwable t) {
185 ;
186 }
187 } else if (fis != null) {
188 try {
189 fis.close();
190 } catch (Throwable t) {
191 ;
192 }
193 }
194
195 }
196
197
198 if (se != null)
199 throw se;
200 context.push(sb.toString());
201
202 }
203
204
205 }