| 1 | /*
|
|---|
| 2 | * To change this template, choose Tools | Templates
|
|---|
| 3 | * and open the template in the editor.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | package com.kitfox.svg.app.data;
|
|---|
| 7 |
|
|---|
| 8 | import java.io.ByteArrayInputStream;
|
|---|
| 9 | import java.io.IOException;
|
|---|
| 10 | import java.io.InputStream;
|
|---|
| 11 | import java.net.URL;
|
|---|
| 12 | import java.net.URLConnection;
|
|---|
| 13 | import java.net.URLStreamHandler;
|
|---|
| 14 |
|
|---|
| 15 | /**
|
|---|
| 16 | *
|
|---|
| 17 | * @author kitfox
|
|---|
| 18 | */
|
|---|
| 19 | public class Handler extends URLStreamHandler
|
|---|
| 20 | {
|
|---|
| 21 | class Connection extends URLConnection
|
|---|
| 22 | {
|
|---|
| 23 | String mime;
|
|---|
| 24 | byte[] buf;
|
|---|
| 25 |
|
|---|
| 26 | public Connection(URL url)
|
|---|
| 27 | {
|
|---|
| 28 | super(url);
|
|---|
| 29 |
|
|---|
| 30 | String path = url.getPath();
|
|---|
| 31 | int idx = path.indexOf(';');
|
|---|
| 32 | mime = path.substring(0, idx);
|
|---|
| 33 | String content = path.substring(idx + 1);
|
|---|
| 34 |
|
|---|
| 35 | if (content.startsWith("base64,"))
|
|---|
| 36 | {
|
|---|
| 37 | content = content.substring(7);
|
|---|
| 38 | try {
|
|---|
| 39 | buf = new sun.misc.BASE64Decoder().decodeBuffer(content);
|
|---|
| 40 | } catch (IOException ex) {
|
|---|
| 41 | ex.printStackTrace();
|
|---|
| 42 | }
|
|---|
| 43 | }
|
|---|
| 44 | }
|
|---|
| 45 |
|
|---|
| 46 | public void connect() throws IOException
|
|---|
| 47 | {
|
|---|
| 48 | }
|
|---|
| 49 |
|
|---|
| 50 | public String getHeaderField(String name)
|
|---|
| 51 | {
|
|---|
| 52 | if ("content-type".equals(name))
|
|---|
| 53 | {
|
|---|
| 54 | return mime;
|
|---|
| 55 | }
|
|---|
| 56 |
|
|---|
| 57 | return super.getHeaderField(name);
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | public InputStream getInputStream() throws IOException
|
|---|
| 61 | {
|
|---|
| 62 | return new ByteArrayInputStream(buf);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | // public Object getContent() throws IOException
|
|---|
| 66 | // {
|
|---|
| 67 | // BufferedImage img = ImageIO.read(getInputStream());
|
|---|
| 68 | // }
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | protected URLConnection openConnection(URL u) throws IOException
|
|---|
| 72 | {
|
|---|
| 73 | return new Connection(u);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | }
|
|---|