01: /* RsyncProtocolHandler.java
02: *
03: * $Id: Handler.java 3766 2005-08-17 23:36:54Z stack-sf $
04: *
05: * Created Jul 15, 2005
06: *
07: * Copyright (C) 2005 Internet Archive.
08: *
09: * This file is part of the Heritrix web crawler (crawler.archive.org).
10: *
11: * Heritrix is free software; you can redistribute it and/or modify
12: * it under the terms of the GNU Lesser Public License as published by
13: * the Free Software Foundation; either version 2.1 of the License, or
14: * any later version.
15: *
16: * Heritrix is distributed in the hope that it will be useful,
17: * but WITHOUT ANY WARRANTY; without even the implied warranty of
18: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19: * GNU Lesser Public License for more details.
20: *
21: * You should have received a copy of the GNU Lesser Public License
22: * along with Heritrix; if not, write to the Free Software
23: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24: */
25: package org.archive.net.rsync;
26:
27: import java.io.IOException;
28: import java.io.InputStream;
29: import java.net.URL;
30: import java.net.URLConnection;
31: import java.net.URLStreamHandler;
32:
33: /**
34: * A protocol handler that uses native rsync client to do copy.
35: * You need to define the system property
36: * <code>-Djava.protocol.handler.pkgs=org.archive.net</code> to add this handler
37: * to the java.net.URL set. Assumes rsync is in path. Define
38: * system property
39: * <code>-Dorg.archive.net.rsync.RsyncUrlConnection.path=PATH_TO_RSYNC</code> to
40: * pass path to rsync. Downloads to <code>java.io.tmpdir</code>.
41: * @author stack
42: */
43: public class Handler extends URLStreamHandler {
44: protected URLConnection openConnection(URL u) {
45: return new RsyncURLConnection(u);
46: }
47:
48: /**
49: * Main dumps rsync file to STDOUT.
50: * @param args
51: * @throws IOException
52: */
53: public static void main(String[] args) throws IOException {
54: if (args.length != 1) {
55: System.out.println("Usage: java java "
56: + "-Djava.protocol.handler.pkgs=org.archive.net "
57: + "org.archive.net.rsync.Handler RSYNC_URL");
58: System.exit(1);
59: }
60: URL u = new URL(args[0]);
61: URLConnection connect = u.openConnection();
62: // Write download to stdout.
63: final int bufferlength = 4096;
64: byte[] buffer = new byte[bufferlength];
65: InputStream is = connect.getInputStream();
66: try {
67: for (int count = is.read(buffer, 0, bufferlength); (count = is
68: .read(buffer, 0, bufferlength)) != -1;) {
69: System.out.write(buffer, 0, count);
70: }
71: System.out.flush();
72: } finally {
73: is.close();
74: }
75: }
76: }
|