001/* 002 * SVG Salamander 003 * Copyright (c) 2004, Mark McKay 004 * All rights reserved. 005 * 006 * Redistribution and use in source and binary forms, with or 007 * without modification, are permitted provided that the following 008 * conditions are met: 009 * 010 * - Redistributions of source code must retain the above 011 * copyright notice, this list of conditions and the following 012 * disclaimer. 013 * - Redistributions in binary form must reproduce the above 014 * copyright notice, this list of conditions and the following 015 * disclaimer in the documentation and/or other materials 016 * provided with the distribution. 017 * 018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 019 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 020 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 021 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 022 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 023 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 025 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 026 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 027 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 028 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 029 * OF THE POSSIBILITY OF SUCH DAMAGE. 030 * 031 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 032 * projects can be found at http://www.kitfox.com 033 */ 034 035package com.kitfox.svg.app.data; 036 037import com.kitfox.svg.SVGConst; 038import com.kitfox.svg.util.Base64InputStream; 039import java.io.ByteArrayInputStream; 040import java.io.ByteArrayOutputStream; 041import java.io.IOException; 042import java.io.InputStream; 043import java.net.URL; 044import java.net.URLConnection; 045import java.net.URLStreamHandler; 046import java.util.logging.Level; 047import java.util.logging.Logger; 048 049/** 050 * 051 * @author kitfox 052 */ 053public class Handler extends URLStreamHandler 054{ 055 class Connection extends URLConnection 056 { 057 String mime; 058 byte[] buf; 059 060 public Connection(URL url) 061 { 062 super(url); 063 064 String path = url.getPath(); 065 int idx = path.indexOf(';'); 066 mime = path.substring(0, idx); 067 String content = path.substring(idx + 1); 068 069 if (content.startsWith("base64,")) 070 { 071 content = content.substring(7); 072 try 073 { 074//byte[] buf2 = new sun.misc.BASE64Decoder().decodeBuffer(content); 075//buf = new sun.misc.BASE64Decoder().decodeBuffer(content); 076 077 ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes()); 078 Base64InputStream b64is = new Base64InputStream(bis); 079 080 ByteArrayOutputStream bout = new ByteArrayOutputStream(); 081 byte[] tmp = new byte[2056]; 082 for (int size = b64is.read(tmp); size != -1; size = b64is.read(tmp)) 083 { 084 bout.write(tmp, 0, size); 085 } 086 buf = bout.toByteArray(); 087 } 088 catch (IOException e) 089 { 090 Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, null, e); 091 } 092 } 093 } 094 095 @Override 096 public void connect() throws IOException 097 { 098 } 099 100 @Override 101 public String getHeaderField(String name) 102 { 103 if ("content-type".equals(name)) 104 { 105 return mime; 106 } 107 108 return super.getHeaderField(name); 109 } 110 111 @Override 112 public InputStream getInputStream() throws IOException 113 { 114 return new ByteArrayInputStream(buf); 115 } 116 117// public Object getContent() throws IOException 118// { 119// BufferedImage img = ImageIO.read(getInputStream()); 120// } 121 } 122 123 @Override 124 protected URLConnection openConnection(URL u) throws IOException 125 { 126 return new Connection(u); 127 } 128 129}