AnyHandler

package cc.vcode.main;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;
import java.net.URLStreamHandlerFactory;

import cc.vcode.torrent.TOTorrent;
import cc.vcode.torrent.TOTorrentFactory;

public class TestMain {

    public static void main(String[] args) throws Exception {
        register();
        test01();
    }

    public static void test01() throws Exception {
        File f = new File("D:\opt\nodejs\ubuntu.torrent");
        String tarPathString = "VCode";
        TOTorrent torrent = TOTorrentFactory.deserialiseFromBEncodedFile(f, tarPathString);
        torrent.serialiseToBEncodedFile(new File("D:\opt\nodejs\ubuntu-target.torrent"));
        System.out.println("success!");
    }

    public static void register() throws MalformedURLException {
        URL.setURLStreamHandlerFactory(new URLStreamHandlerFactory() {

            @Override
            public URLStreamHandler createURLStreamHandler(String protocol) {
                return new AnyHandler();
            }
        });
    }
}

class AnyHandler extends URLStreamHandler {

    /**
     * Shared instance.
     */
    public static final AnyHandler INSTANCE = new AnyHandler();

    /**
     * Returns a new URLConnection for this URL.
     * 
     * @param url
     *            the JavaScript URL
     * @return the connection
     */
    @Override
    protected URLConnection openConnection(final URL url) {
        // throw new UnsupportedOperationException();
        throw new RuntimeException("Unsupported protocol for open connection to " + url);
    }
}
原文地址:https://www.cnblogs.com/jpit/p/8057180.html