Java URL Connection byteArrayToURL(final byte[] bytes)

Here you can find the source of byteArrayToURL(final byte[] bytes)

Description

byte Array To URL

License

Apache License

Declaration

static URL byteArrayToURL(final byte[] bytes) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.UncheckedIOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLStreamHandler;

public class Main {
    static URL byteArrayToURL(final byte[] bytes) {
        try {/*from   w w w.j a  v a  2  s  .  c  o  m*/
            return new URL(null, "foobar://foo/bar", new URLStreamHandler() {
                @Override
                protected URLConnection openConnection(final URL u) throws IOException {
                    final ByteArrayInputStream bais = new ByteArrayInputStream(bytes);

                    return new URLConnection(null) {

                        @Override
                        public void connect() throws IOException {
                        }

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return bais;
                        }
                    };
                }
            });
        } catch (MalformedURLException e) {
            throw new UncheckedIOException(e);
        }
    }
}

Related

  1. addHeaders(Map headers, URLConnection c)
  2. addHeaders(URLConnection conn)
  3. addProperty(URLConnection connection)
  4. closeURLClassLoader(URLClassLoader clazzLdr)
  5. createImgUrlConnection(String url)
  6. createURLClassLoader(String dirPath)
  7. createURLConnection(String url)