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