View Javadoc

    1   /* RsyncProtocolHandler.java
    2    *
    3    * $Id: Handler.java 3766 2005-08-17 23:36:54Z stack-sf $
    4    *
    5    * Created Jul 15, 2005
    6    *
    7    * Copyright (C) 2005 Internet Archive.
    8    *
    9    * 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)
    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          // Write download to stdout.
    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  }