author | Jaroslav Tulach <jtulach@netbeans.org> |
Sun, 01 Feb 2009 13:27:04 +0100 | |
changeset 308 | 7f38f014244c |
child 310 | fba31e9504a1 |
permissions | -rw-r--r-- |
jtulach@308 | 1 |
|
jtulach@308 | 2 |
package org.apidesign.exceptions.trycatchredo; |
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 |
import junit.framework.Assert; |
jtulach@308 | 17 |
|
jtulach@308 | 18 |
/** |
jtulach@308 | 19 |
* |
jtulach@308 | 20 |
* @author Jaroslav Tulach |
jtulach@308 | 21 |
*/ |
jtulach@308 | 22 |
public final class MemoryURL extends URLStreamHandler { |
jtulach@308 | 23 |
private MemoryURL() { |
jtulach@308 | 24 |
} |
jtulach@308 | 25 |
|
jtulach@308 | 26 |
static void initialize() { |
jtulach@308 | 27 |
} |
jtulach@308 | 28 |
static { |
jtulach@308 | 29 |
class F implements URLStreamHandlerFactory { |
jtulach@308 | 30 |
public URLStreamHandler createURLStreamHandler(String protocol) { |
jtulach@308 | 31 |
if (protocol.startsWith("memory")) { |
jtulach@308 | 32 |
return new MemoryURL(); |
jtulach@308 | 33 |
} |
jtulach@308 | 34 |
return null; |
jtulach@308 | 35 |
} |
jtulach@308 | 36 |
} |
jtulach@308 | 37 |
F f = new F(); |
jtulach@308 | 38 |
URL.setURLStreamHandlerFactory(f); |
jtulach@308 | 39 |
} |
jtulach@308 | 40 |
|
jtulach@308 | 41 |
private static Map<String,InputStream> contents = new HashMap<String,InputStream>(); |
jtulach@308 | 42 |
private static Map<String,MC> outputs = new HashMap<String,MC>(); |
jtulach@308 | 43 |
public static void registerURL(String u, String content, ByteArrayOutputStream out) throws MalformedURLException { |
jtulach@308 | 44 |
contents.put(u, new ByteArrayInputStream(content.getBytes())); |
jtulach@308 | 45 |
if (out != null) { |
jtulach@308 | 46 |
new MC(new URL(u)).out = out; |
jtulach@308 | 47 |
} |
jtulach@308 | 48 |
} |
jtulach@308 | 49 |
|
jtulach@308 | 50 |
public static byte[] getOutputForURL(String u) { |
jtulach@308 | 51 |
MC out = outputs.get(u); |
jtulach@308 | 52 |
Assert.assertNotNull("No output for " + u, out); |
jtulach@308 | 53 |
return out.out.toByteArray(); |
jtulach@308 | 54 |
} |
jtulach@308 | 55 |
|
jtulach@308 | 56 |
protected URLConnection openConnection(URL u) throws IOException { |
jtulach@308 | 57 |
return new MC(u); |
jtulach@308 | 58 |
} |
jtulach@308 | 59 |
|
jtulach@308 | 60 |
private static final class MC extends URLConnection { |
jtulach@308 | 61 |
private InputStream values; |
jtulach@308 | 62 |
private ByteArrayOutputStream out; |
jtulach@308 | 63 |
|
jtulach@308 | 64 |
public MC(URL u) { |
jtulach@308 | 65 |
super(u); |
jtulach@308 | 66 |
outputs.put(u.toExternalForm(), this); |
jtulach@308 | 67 |
} |
jtulach@308 | 68 |
|
jtulach@308 | 69 |
public void connect() throws IOException { |
jtulach@308 | 70 |
if (values != null) { |
jtulach@308 | 71 |
return; |
jtulach@308 | 72 |
} |
jtulach@308 | 73 |
values = contents.remove(url.toExternalForm()); |
jtulach@308 | 74 |
if (values == null) { |
jtulach@308 | 75 |
throw new IOException("No such content: " + url); |
jtulach@308 | 76 |
} |
jtulach@308 | 77 |
} |
jtulach@308 | 78 |
|
jtulach@308 | 79 |
@Override |
jtulach@308 | 80 |
public InputStream getInputStream() throws IOException { |
jtulach@308 | 81 |
connect(); |
jtulach@308 | 82 |
return values; |
jtulach@308 | 83 |
} |
jtulach@308 | 84 |
|
jtulach@308 | 85 |
@Override |
jtulach@308 | 86 |
public OutputStream getOutputStream() throws IOException { |
jtulach@308 | 87 |
if (out == null) { |
jtulach@308 | 88 |
out = new ByteArrayOutputStream(); |
jtulach@308 | 89 |
} |
jtulach@308 | 90 |
return out; |
jtulach@308 | 91 |
} |
jtulach@308 | 92 |
} |
jtulach@308 | 93 |
} |