1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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)
54 throws IOException {
55 if (args.length != 1) {
56 System.out.println("Usage: java java " +
57 "-Djava.protocol.handler.pkgs=org.archive.net " +
58 "org.archive.net.rsync.Handler RSYNC_URL");
59 System.exit(1);
60 }
61 URL u = new URL(args[0]);
62 URLConnection connect = u.openConnection();
63
64 final int bufferlength = 4096;
65 byte [] buffer = new byte [bufferlength];
66 InputStream is = connect.getInputStream();
67 try {
68 for (int count = is.read(buffer, 0, bufferlength);
69 (count = is.read(buffer, 0, bufferlength)) != -1;) {
70 System.out.write(buffer, 0, count);
71 }
72 System.out.flush();
73 } finally {
74 is.close();
75 }
76 }
77 }