01: package net.javacoding.jspider.tool.impl;
02:
03: import net.javacoding.jspider.api.event.JSpiderEvent;
04: import net.javacoding.jspider.api.event.resource.ResourceFetchedEvent;
05:
06: import java.io.*;
07:
08: /**
09: * $Id: DownloadTool.java,v 1.3 2003/04/01 19:44:42 vanrogu Exp $
10: */
11: public class DownloadTool extends BaseToolImpl {
12:
13: public String getName() {
14: return "download";
15: }
16:
17: public String getUsage() {
18: return "[filename]";
19: }
20:
21: public int getParameterCount() {
22: return 1;
23: }
24:
25: public void notify(JSpiderEvent event) {
26:
27: String fileName = parameters.getValues()[0];
28:
29: if (event instanceof ResourceFetchedEvent) {
30: ResourceFetchedEvent rfe = (ResourceFetchedEvent) event;
31: int counter = 0;
32: try {
33: InputStream is = rfe.getResource().getInputStream();
34: OutputStream os = new FileOutputStream(fileName);
35: int read = is.read();
36: while (read != -1) {
37: counter++;
38: os.write(read);
39: read = is.read();
40: }
41: os.flush();
42: os.close();
43: is.close();
44: System.out.println("Downloaded resource to '"
45: + fileName + "' (" + counter + " bytes)");
46: } catch (IOException e) {
47: e.printStackTrace();
48: }
49: }
50: }
51:
52: }
|