1.1 --- a/rt/emul/mini/src/main/java/java/net/URL.java Thu Oct 31 11:23:54 2013 +0100
1.2 +++ b/rt/emul/mini/src/main/java/java/net/URL.java Thu Oct 30 01:50:21 2014 +0100
1.3 @@ -26,6 +26,7 @@
1.4 package java.net;
1.5
1.6 import java.io.ByteArrayInputStream;
1.7 +import java.io.Closeable;
1.8 import java.io.IOException;
1.9 import java.io.InputStream;
1.10 import org.apidesign.bck2brwsr.core.JavaScriptBody;
1.11 @@ -216,7 +217,9 @@
1.12 private int hashCode = -1;
1.13
1.14 /** input stream associated with the URL */
1.15 - private InputStream is;
1.16 + private byte[] arr;
1.17 + /** blob URL, if any */
1.18 + private URL blob;
1.19
1.20 /**
1.21 * Creates a <code>URL</code> object from the specified
1.22 @@ -427,9 +430,9 @@
1.23 this(null, spec);
1.24 }
1.25
1.26 - private URL(String spec, InputStream is) throws MalformedURLException {
1.27 + private URL(String spec, byte[] arr) throws MalformedURLException {
1.28 this(null, spec);
1.29 - this.is = is;
1.30 + this.arr = arr;
1.31 }
1.32
1.33 /**
1.34 @@ -984,8 +987,8 @@
1.35 * @see java.net.URLConnection#getInputStream()
1.36 */
1.37 public final InputStream openStream() throws java.io.IOException {
1.38 - if (is != null) {
1.39 - return is;
1.40 + if (arr != null) {
1.41 + return new ByteArrayInputStream(arr);
1.42 }
1.43 byte[] arr = (byte[]) getContent(new Class[] { byte[].class });
1.44 if (arr == null) {
1.45 @@ -1057,13 +1060,49 @@
1.46 }
1.47 return null;
1.48 }
1.49 +
1.50 + @JavaScriptBody(args = "data", body =
1.51 + "if (typeof Blob !== 'undefined' && typeof Uint8Array !== 'undefined' && typeof URL !== 'undefined' && typeof URL.createObjectURL != 'undefined') {\n" +
1.52 + " var s = new Uint8Array(data);\n" +
1.53 + " var b = new Blob([ s ]);\n" +
1.54 + " return URL.createObjectURL(b);\n" +
1.55 + "} else {\n" +
1.56 + " return null;\n" +
1.57 + "}"
1.58 + )
1.59 + static native String toBlobURL(byte[] data);
1.60 +
1.61 + @JavaScriptBody(args = "url", body = "URL.revokeObjectURL(url);")
1.62 + static native void closeBlob(String url);
1.63
1.64 static URLStreamHandler getURLStreamHandler(final String protocol) {
1.65 URLStreamHandler universal = new URLStreamHandler() {
1.66 @Override
1.67 protected URLConnection openConnection(URL u) throws IOException {
1.68 - return new URLConnection(u) {
1.69 - Object stream = url.is;
1.70 + final ByteArrayInputStream is;
1.71 + if (u.arr != null) {
1.72 + is = new ByteArrayInputStream(u.arr);
1.73 + if (u.blob != null) {
1.74 + u = u.blob;
1.75 + } else {
1.76 + String blob = toBlobURL(u.arr);
1.77 + if (blob != null) {
1.78 + URL blobURL = new URL(null, blob, false);
1.79 + blobURL.blob = blobURL;
1.80 + blobURL.arr = u.arr;
1.81 + u = blobURL;
1.82 + }
1.83 + }
1.84 + } else {
1.85 + is = null;
1.86 + }
1.87 +
1.88 + class ResourceConnection extends URLConnection implements Closeable {
1.89 + public ResourceConnection(URL url) {
1.90 + super(url);
1.91 + }
1.92 +
1.93 + Object stream = is;
1.94
1.95 @Override
1.96 public void connect() throws IOException {
1.97 @@ -1086,9 +1125,15 @@
1.98 }
1.99 return (InputStream)stream;
1.100 }
1.101 -
1.102 -
1.103 - };
1.104 +
1.105 + @Override
1.106 + public void close() throws IOException {
1.107 + if (url.blob != null) {
1.108 + closeBlob(url.blob.toExternalForm());
1.109 + }
1.110 + }
1.111 + }
1.112 + return new ResourceConnection(u);
1.113 }
1.114 };
1.115 return universal;