
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

/**
 * @author Hal Hildebrand
 *         Date: Apr 8, 2005
 *         Time: 8:20:44 PM
 */
public class MyUrlHandler extends URLStreamHandler {
    private InputStream in;


    public static void main(String[] argv) throws Exception {
        InputStream is = new ByteArrayInputStream(argv[0].getBytes());
        URL url = new URL(null, "file://test", new MyUrlHandler(is));
        is = url.openConnection().getInputStream();
        byte[] buf = new byte[100];
        int read = is.read(buf);
        while (read > 0) {
            System.out.println(new String(buf, 0, read));
            read = is.read(buf);
        }
    }


    public MyUrlHandler(InputStream in) {
        this.in = in;
    }


    protected URLConnection openConnection(URL u) throws IOException {
        return new MyUrlConnection(u, in);
    }


    private static class MyUrlConnection extends URLConnection {
        private InputStream in;


        public MyUrlConnection(URL url, InputStream in) {
            super(url);
            this.in = in;
        }


        public void connect() throws IOException {
        }


        public InputStream getInputStream() {
            return in;
        }
    }
}
