01: package com.coldcore.coloradoftp.plugin.webcam;
02:
03: import com.coldcore.coloradoftp.plugin.webcam.task.UploadTask;
04: import com.coldcore.misc5.CByte;
05: import org.apache.log4j.Logger;
06:
07: import java.io.ByteArrayOutputStream;
08: import java.io.InputStream;
09:
10: /**
11: * Executes action when file is ready.
12: */
13: public class UploadAction implements Runnable {
14:
15: private static Logger log = Logger.getLogger(UploadAction.class);
16: protected String filename;
17: protected FileGrabber grabber;
18: protected UploadTask task;
19:
20: public UploadAction(String filename, FileGrabber grabber,
21: UploadTask task) {
22: this .filename = filename;
23: this .grabber = grabber;
24: this .task = task;
25: }
26:
27: public void run() {
28: byte[] data = readFile();
29: if (data != null)
30: task.execute(filename, data);
31: }
32:
33: /** Read file data.
34: * @return File data or NULL in case of error
35: */
36: protected byte[] readFile() {
37: try {
38: InputStream in = grabber.getInputStream();
39: ByteArrayOutputStream out = new ByteArrayOutputStream();
40: CByte.inputToOutput(in, out);
41: return out.toByteArray();
42: } catch (Exception e) {
43: log.error("Error reading file (stopping)", e);
44: return null;
45: }
46: }
47: }
|