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