001    /*
002     * To change this template, choose Tools | Templates
003     * and open the template in the editor.
004     */
005    
006    package com.kitfox.svg.app.data;
007    
008    import java.io.ByteArrayInputStream;
009    import java.io.IOException;
010    import java.io.InputStream;
011    import java.net.URL;
012    import java.net.URLConnection;
013    import java.net.URLStreamHandler;
014    
015    /**
016     *
017     * @author kitfox
018     */
019    public class Handler extends URLStreamHandler
020    {
021        class Connection extends URLConnection
022        {
023            String mime;
024            byte[] buf;
025    
026            public Connection(URL url)
027            {
028                super(url);
029    
030                String path = url.getPath();
031                int idx = path.indexOf(';');
032                mime = path.substring(0, idx);
033                String content = path.substring(idx + 1);
034    
035                if (content.startsWith("base64,"))
036                {
037                    content = content.substring(7);
038                    try {
039                        buf = new sun.misc.BASE64Decoder().decodeBuffer(content);
040                    } catch (IOException ex) {
041                        ex.printStackTrace();
042                    }
043                }
044            }
045            
046            public void connect() throws IOException
047            {
048            }
049    
050            public String getHeaderField(String name)
051            {
052                if ("content-type".equals(name))
053                {
054                    return mime;
055                }
056    
057                return super.getHeaderField(name);
058            }
059    
060            public InputStream getInputStream() throws IOException
061            {
062                return new ByteArrayInputStream(buf);
063            }
064    
065    //        public Object getContent() throws IOException
066    //        {
067    //            BufferedImage img = ImageIO.read(getInputStream());
068    //        }
069        }
070    
071        protected URLConnection openConnection(URL u) throws IOException
072        {
073            return new Connection(u);
074        }
075    
076    }