samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/MemoryURL.java
changeset 312 0678c9589013
parent 311 cb8db49f9d1c
child 313 d8bb07520edd
     1.1 --- a/samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/MemoryURL.java	Sun Feb 01 16:27:41 2009 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,94 +0,0 @@
     1.4 -
     1.5 -package org.apidesign.exceptions.trycatchredo.usage;
     1.6 -
     1.7 -import java.io.ByteArrayInputStream;
     1.8 -import java.io.ByteArrayOutputStream;
     1.9 -import java.io.IOException;
    1.10 -import java.io.InputStream;
    1.11 -import java.io.OutputStream;
    1.12 -import java.net.MalformedURLException;
    1.13 -import java.net.URL;
    1.14 -import java.net.URLConnection;
    1.15 -import java.net.URLStreamHandler;
    1.16 -import java.net.URLStreamHandlerFactory;
    1.17 -import java.util.HashMap;
    1.18 -import java.util.Map;
    1.19 -
    1.20 -/** Support for special "memory://" URLs. Useful when testing network communication.
    1.21 - *
    1.22 - * @author Jaroslav Tulach
    1.23 - */
    1.24 -public final class MemoryURL extends URLStreamHandler {
    1.25 -    private MemoryURL() {
    1.26 -    }
    1.27 -
    1.28 -    public static void initialize() {
    1.29 -    }
    1.30 -    static {
    1.31 -        class F implements URLStreamHandlerFactory {
    1.32 -            public URLStreamHandler createURLStreamHandler(String protocol) {
    1.33 -                if (protocol.startsWith("memory")) {
    1.34 -                    return new MemoryURL();
    1.35 -                }
    1.36 -                return null;
    1.37 -            }
    1.38 -        }
    1.39 -        F f = new F();
    1.40 -        URL.setURLStreamHandlerFactory(f);
    1.41 -    }
    1.42 -    
    1.43 -    private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
    1.44 -    private static Map<String,OutputStream> outputs = new HashMap<String,OutputStream>();
    1.45 -
    1.46 -    public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException {
    1.47 -        contents.put(u, new ByteArrayInputStream(content.getBytes()));
    1.48 -        outputs.put(u, out);
    1.49 -    }
    1.50 -    
    1.51 -    public static String getOutputForURL(String u) {
    1.52 -        OutputStream out = outputs.get(u);
    1.53 -        return out.toString();
    1.54 -    }
    1.55 -    
    1.56 -    protected URLConnection openConnection(URL u) throws IOException {
    1.57 -        return new MC(u);
    1.58 -    }
    1.59 -    
    1.60 -    private static final class MC extends URLConnection {
    1.61 -        private InputStream values;
    1.62 -        private OutputStream out;
    1.63 -        
    1.64 -        public MC(URL u) {
    1.65 -            super(u);
    1.66 -            out = outputs.get(u.toExternalForm());
    1.67 -            if (out == null) {
    1.68 -                out = new ByteArrayOutputStream();
    1.69 -                outputs.put(u.toExternalForm(), out);
    1.70 -            }
    1.71 -        }
    1.72 -
    1.73 -        public void connect() throws IOException {
    1.74 -            if (values != null) {
    1.75 -                return;
    1.76 -            }
    1.77 -            values = contents.remove(url.toExternalForm());
    1.78 -            if (values == null) {
    1.79 -                throw new IOException("No such content: " + url);
    1.80 -            }
    1.81 -        }
    1.82 -
    1.83 -        @Override
    1.84 -        public InputStream getInputStream() throws IOException {
    1.85 -            connect();
    1.86 -            return values;
    1.87 -        }
    1.88 -
    1.89 -        @Override
    1.90 -        public OutputStream getOutputStream() throws IOException {
    1.91 -            if (out == null) {
    1.92 -                out = new ByteArrayOutputStream();
    1.93 -            }
    1.94 -            return out;
    1.95 -        }
    1.96 -    }
    1.97 -}