1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package wjhk.jupload2.upload;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.util.Date;
28
29 import wjhk.jupload2.exception.JUploadException;
30 import wjhk.jupload2.exception.JUploadIOException;
31 import wjhk.jupload2.exception.JUploadInterrupted;
32 import wjhk.jupload2.filedata.FileData;
33 import wjhk.jupload2.gui.filepanel.treeview.TreeFileDataNode;
34 import wjhk.jupload2.policies.UploadPolicy;
35 import wjhk.jupload2.upload.helper.ByteArrayEncoder;
36
37
38
39
40
41
42 public class UploadFileData implements FileData {
43
44
45
46
47 private FileData fileData = null;
48
49
50
51
52
53 InputStream uploadInputStream = null;
54
55
56
57
58
59 int numOfFileInCurrentUpload = -1;
60
61
62
63
64
65
66
67
68 private FileUploadManagerThread fileUploadManagerThread = null;
69
70
71
72
73 private long uploadRemainingLength = -1;
74
75
76
77
78 private UploadPolicy uploadPolicy = null;
79
80 private final static int BUFLEN = 4096;
81
82
83
84
85 private final byte readBuffer[] = new byte[BUFLEN];
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100 public UploadFileData(FileData fileDataParam, int numOfFileInCurrentUpload,
101 FileUploadManagerThread fileUploadManagerThreadParam, UploadPolicy uploadPolicyParam) {
102 if (fileDataParam == null && !(this instanceof UploadFileDataPoisonned)) {
103 throw new NullPointerException(
104 "fileData is null in UploadFileData(FileData, FileUploadManagerThread, UploadPolicy) constructor");
105 }
106 this.fileData = fileDataParam;
107 this.numOfFileInCurrentUpload = numOfFileInCurrentUpload;
108 this.fileUploadManagerThread = fileUploadManagerThreadParam;
109 this.uploadPolicy = uploadPolicyParam;
110 }
111
112
113
114
115
116
117
118
119 public UploadFileData(boolean poisonned) {
120 if (!poisonned) {
121 throw new IllegalArgumentException("poisonned must be true in UploadFileData(boolean) constructor");
122 }
123 }
124
125
126
127
128
129
130
131
132
133
134
135 long getRemainingLength() {
136 return this.uploadRemainingLength;
137 }
138
139
140
141
142
143
144
145
146
147
148 void uploadFile(OutputStream outputStream, long amount) throws JUploadException, JUploadInterrupted {
149 if (this.uploadPolicy.getDebugLevel() >= 30) {
150 this.uploadPolicy.displayDebug("in UploadFileData.uploadFile (amount:" + amount + ", getUploadLength(): "
151 + getUploadLength() + ")", 30);
152 }
153
154
155
156 InputStream inputStream = getInputStream();
157
158 while (amount > 0 && !this.fileUploadManagerThread.isUploadFinished()) {
159
160 if (Thread.interrupted()) {
161 throw new JUploadInterrupted(getClass().getName() + ".uploadFile [" + this.getFileName() + "]",
162 this.uploadPolicy);
163 }
164
165 int toread = (amount > BUFLEN) ? BUFLEN : (int) amount;
166 int towrite = 0;
167
168 try {
169 towrite = inputStream.read(this.readBuffer, 0, toread);
170 } catch (IOException e) {
171 throw new JUploadIOException(e);
172 }
173 if (towrite > 0) {
174 try {
175 outputStream.write(this.readBuffer, 0, towrite);
176 this.fileUploadManagerThread.nbBytesUploaded(towrite, this);
177 amount -= towrite;
178 this.uploadRemainingLength -= towrite;
179
180
181
182
183
184 if (this.uploadPolicy.getDebugLevel() > 100) {
185 try {
186 Thread.sleep(20);
187 } catch (InterruptedException e) {
188
189
190 }
191 }
192 } catch (IOException ioe) {
193 throw new JUploadIOException(this.getClass().getName() + ".uploadFile()", ioe);
194 } catch (Exception e) {
195
196
197 throw new JUploadException(this.getClass().getName()
198 + ".uploadFile() (check the user permission on the server)", e);
199 }
200 }
201 }
202 }
203
204
205
206
207
208
209 public void afterUpload() {
210 if (this.uploadInputStream != null) {
211 try {
212 this.uploadInputStream.close();
213 } catch (IOException ioe) {
214
215 }
216 this.uploadInputStream = null;
217 }
218
219 this.fileData.afterUpload();
220 }
221
222
223
224
225 public void beforeRetry() {
226
227 this.uploadRemainingLength = this.fileData.getUploadLength();
228
229 if (this.uploadInputStream != null) {
230 try {
231 this.uploadInputStream.close();
232 } catch (IOException ioe) {
233
234 this.uploadPolicy.displayDebug("[Warning] Ignoring " + ioe.getClass().getName()
235 + " in UploadFileData.beforeRetry() [" + ioe.getMessage() + "]", 30);
236 }
237 this.uploadInputStream = null;
238 }
239 }
240
241
242 public void appendFileProperties(ByteArrayEncoder bae, int index) throws JUploadIOException {
243 this.fileData.appendFileProperties(bae, index);
244 }
245
246
247 public void beforeUpload(String uploadFileRoot) throws JUploadException {
248 this.fileData.beforeUpload(uploadFileRoot);
249
250
251 this.uploadRemainingLength = this.fileData.getUploadLength();
252 }
253
254
255 public boolean canRead() {
256 return this.fileData.canRead();
257 }
258
259
260 public String getDirectory() {
261 return this.fileData.getDirectory();
262 }
263
264
265 public File getFile() {
266 throw new IllegalAccessError("Internal error: getFile is deprecated and should not be called from "
267 + this.getClass().getName());
268 }
269
270
271 public String getFileExtension() {
272 return this.fileData.getFileExtension();
273 }
274
275
276 public long getFileLength() {
277 return this.fileData.getFileLength();
278 }
279
280
281 public String getFileName() {
282 return this.fileData.getFileName();
283 }
284
285
286 public InputStream getInputStream() throws JUploadException {
287 if (this.uploadInputStream == null) {
288 this.uploadInputStream = this.fileData.getInputStream();
289 }
290 return this.uploadInputStream;
291 }
292
293
294 public Date getLastModified() {
295 return this.fileData.getLastModified();
296 }
297
298
299 public String getMD5() throws JUploadException {
300 return this.fileData.getMD5();
301 }
302
303
304 public String getMimeType() {
305 return this.fileData.getMimeType();
306 }
307
308
309 public String getRelativeDir() {
310 return this.fileData.getRelativeDir();
311 }
312
313
314 public String getAbsolutePath() {
315 return this.fileData.getAbsolutePath();
316 }
317
318
319
320
321
322
323
324
325
326 public String getUploadFilename(int index) throws JUploadException {
327 return this.uploadPolicy.getUploadFilename(this.fileData, index);
328 }
329
330
331
332
333
334
335
336
337
338
339
340 public String getUploadName(int index) throws JUploadException {
341 return this.uploadPolicy.getUploadName(this.fileData, index);
342 }
343
344
345
346
347
348
349
350
351 public long getUploadLength() {
352 return this.fileData.getUploadLength();
353 }
354
355
356 public boolean isPreparedForUpload() {
357 return this.fileData.isPreparedForUpload();
358 }
359
360
361
362
363
364
365 public boolean isPoisonned() {
366 return false;
367 }
368
369
370
371
372 public int getNumOfFileInCurrentUpload() {
373 return numOfFileInCurrentUpload;
374 }
375
376
377 public boolean getUploadFlag() {
378
379 return false;
380 }
381
382
383
384
385
386
387 public void setUploadFlag(boolean uploadFlag) {
388 if (!uploadFlag) {
389 throw new IllegalArgumentException(
390 "This method should not be called. At least, it may not be called with uploadeFlag=false");
391 }
392 }
393
394 public TreeFileDataNode getTreeFileDataNode() {
395 return fileData.getTreeFileDataNode();
396 }
397
398 public void setTreeFileDataNode(TreeFileDataNode node) {
399 throw new IllegalStateException("setTreeFileDataNode may not be called againts a " + this.getClass().getName());
400 }
401 }