samples/exceptions/src/org/apidesign/exceptions/trycatchredo/usage/MemoryURL.java
author Jaroslav Tulach <jtulach@netbeans.org>
Sun, 01 Feb 2009 16:27:41 +0100
changeset 311 cb8db49f9d1c
parent 310 samples/exceptions/test/org/apidesign/exceptions/trycatchredo/MemoryURL.java@fba31e9504a1
permissions -rw-r--r--
Rearranged to support test as well as interactive modes
jtulach@308
     1
jtulach@311
     2
package org.apidesign.exceptions.trycatchredo.usage;
jtulach@308
     3
jtulach@308
     4
import java.io.ByteArrayInputStream;
jtulach@308
     5
import java.io.ByteArrayOutputStream;
jtulach@308
     6
import java.io.IOException;
jtulach@308
     7
import java.io.InputStream;
jtulach@308
     8
import java.io.OutputStream;
jtulach@308
     9
import java.net.MalformedURLException;
jtulach@308
    10
import java.net.URL;
jtulach@308
    11
import java.net.URLConnection;
jtulach@308
    12
import java.net.URLStreamHandler;
jtulach@308
    13
import java.net.URLStreamHandlerFactory;
jtulach@308
    14
import java.util.HashMap;
jtulach@308
    15
import java.util.Map;
jtulach@308
    16
jtulach@311
    17
/** Support for special "memory://" URLs. Useful when testing network communication.
jtulach@308
    18
 *
jtulach@308
    19
 * @author Jaroslav Tulach
jtulach@308
    20
 */
jtulach@308
    21
public final class MemoryURL extends URLStreamHandler {
jtulach@308
    22
    private MemoryURL() {
jtulach@308
    23
    }
jtulach@308
    24
jtulach@311
    25
    public static void initialize() {
jtulach@308
    26
    }
jtulach@308
    27
    static {
jtulach@308
    28
        class F implements URLStreamHandlerFactory {
jtulach@308
    29
            public URLStreamHandler createURLStreamHandler(String protocol) {
jtulach@308
    30
                if (protocol.startsWith("memory")) {
jtulach@308
    31
                    return new MemoryURL();
jtulach@308
    32
                }
jtulach@308
    33
                return null;
jtulach@308
    34
            }
jtulach@308
    35
        }
jtulach@308
    36
        F f = new F();
jtulach@308
    37
        URL.setURLStreamHandlerFactory(f);
jtulach@308
    38
    }
jtulach@308
    39
    
jtulach@308
    40
    private static Map<String,InputStream> contents = new HashMap<String,InputStream>();
jtulach@310
    41
    private static Map<String,OutputStream> outputs = new HashMap<String,OutputStream>();
jtulach@311
    42
jtulach@310
    43
    public static void registerURL(String u, String content, OutputStream out) throws MalformedURLException {
jtulach@308
    44
        contents.put(u, new ByteArrayInputStream(content.getBytes()));
jtulach@310
    45
        outputs.put(u, out);
jtulach@308
    46
    }
jtulach@308
    47
    
jtulach@310
    48
    public static String getOutputForURL(String u) {
jtulach@310
    49
        OutputStream out = outputs.get(u);
jtulach@310
    50
        return out.toString();
jtulach@308
    51
    }
jtulach@308
    52
    
jtulach@308
    53
    protected URLConnection openConnection(URL u) throws IOException {
jtulach@308
    54
        return new MC(u);
jtulach@308
    55
    }
jtulach@308
    56
    
jtulach@308
    57
    private static final class MC extends URLConnection {
jtulach@308
    58
        private InputStream values;
jtulach@310
    59
        private OutputStream out;
jtulach@308
    60
        
jtulach@308
    61
        public MC(URL u) {
jtulach@308
    62
            super(u);
jtulach@310
    63
            out = outputs.get(u.toExternalForm());
jtulach@310
    64
            if (out == null) {
jtulach@310
    65
                out = new ByteArrayOutputStream();
jtulach@310
    66
                outputs.put(u.toExternalForm(), out);
jtulach@310
    67
            }
jtulach@308
    68
        }
jtulach@308
    69
jtulach@308
    70
        public void connect() throws IOException {
jtulach@308
    71
            if (values != null) {
jtulach@308
    72
                return;
jtulach@308
    73
            }
jtulach@308
    74
            values = contents.remove(url.toExternalForm());
jtulach@308
    75
            if (values == null) {
jtulach@308
    76
                throw new IOException("No such content: " + url);
jtulach@308
    77
            }
jtulach@308
    78
        }
jtulach@308
    79
jtulach@308
    80
        @Override
jtulach@308
    81
        public InputStream getInputStream() throws IOException {
jtulach@308
    82
            connect();
jtulach@308
    83
            return values;
jtulach@308
    84
        }
jtulach@308
    85
jtulach@308
    86
        @Override
jtulach@308
    87
        public OutputStream getOutputStream() throws IOException {
jtulach@308
    88
            if (out == null) {
jtulach@308
    89
                out = new ByteArrayOutputStream();
jtulach@308
    90
            }
jtulach@308
    91
            return out;
jtulach@308
    92
        }
jtulach@308
    93
    }
jtulach@308
    94
}