From 9551fc304e04f5014746eb4631aadf7953b46a83 Mon Sep 17 00:00:00 2001 From: kitfox Date: Thu, 28 Feb 2008 06:46:15 +0000 Subject: Added Base64 stream codecs. Added Main-Class attribute to generated jars. git-svn-id: https://svn.java.net/svn/svgsalamander~svn/trunk/svg-core@52 7dc7fa77-23fb-e6ad-8e2e-c86bd48ed22b --- src/main/java/com/kitfox/svg/ImageSVG.java | 9 ++- .../java/com/kitfox/svg/xml/Base64InputStream.java | 82 ++++++++++++++++++++ .../com/kitfox/svg/xml/Base64OutputStream.java | 90 ++++++++++++++++++++++ src/main/java/com/kitfox/svg/xml/Base64Util.java | 32 ++++++++ .../java/com/kitfox/svg/xml/StyleAttribute.java | 2 +- 5 files changed, 213 insertions(+), 2 deletions(-) create mode 100755 src/main/java/com/kitfox/svg/xml/Base64InputStream.java create mode 100755 src/main/java/com/kitfox/svg/xml/Base64OutputStream.java create mode 100755 src/main/java/com/kitfox/svg/xml/Base64Util.java (limited to 'src') diff --git a/src/main/java/com/kitfox/svg/ImageSVG.java b/src/main/java/com/kitfox/svg/ImageSVG.java index 75aaf79..6f4b301 100644 --- a/src/main/java/com/kitfox/svg/ImageSVG.java +++ b/src/main/java/com/kitfox/svg/ImageSVG.java @@ -80,7 +80,14 @@ public class ImageSVG extends RenderableElement if (getPres(sty.setName("xlink:href"))) { URI src = sty.getURIValue(getXMLBase()); - imageSrc = src.toURL(); + try { + imageSrc = src.toURL(); + } + catch (Exception e) + { + e.printStackTrace(); + imageSrc = null; + } } } catch (Exception e) diff --git a/src/main/java/com/kitfox/svg/xml/Base64InputStream.java b/src/main/java/com/kitfox/svg/xml/Base64InputStream.java new file mode 100755 index 0000000..3851c39 --- /dev/null +++ b/src/main/java/com/kitfox/svg/xml/Base64InputStream.java @@ -0,0 +1,82 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.kitfox.svg.xml; + +import java.io.FilterInputStream; +import java.io.IOException; +import java.io.InputStream; + +/** + * + * @author kitfox + */ +public class Base64InputStream extends FilterInputStream +{ + int buf; //Cached bytes to read + int bufSize; //Number of bytes waiting to be read from buffer + boolean drain = false; //After set, read no more chunks + + public Base64InputStream(InputStream in) + { + super(in); + } + + public int read() throws IOException + { + if (drain && bufSize == 0) + { + return -1; + } + + if (bufSize == 0) + { + //Read next chunk into 4 byte buffer + int chunk = in.read(); + if (chunk == -1) + { + drain = true; + return -1; + } + + //get remaining 3 bytes + for (int i = 0; i < 3; ++i) + { + int value = in.read(); + if (value == -1) + { + throw new IOException("Early termination of base64 stream"); + } + chunk = (chunk << 8) | (value & 0xff); + } + + //Check for special termination characters + if ((chunk & 0xffff) == (((byte)'=' << 8) | (byte)'=')) + { + bufSize = 1; + drain = true; + } + else if ((chunk & 0xff) == (byte)'=') + { + bufSize = 2; + drain = true; + } + else + { + bufSize = 3; + } + + //Fill buffer with decoded characters + for (int i = 0; i < bufSize + 1; ++i) + { + buf = (buf << 6) | Base64Util.decodeByte((chunk >> 24) & 0xff); + chunk <<= 8; + } + } + + //Return nth remaing bte & decrement counter + return (buf >> (--bufSize * 8)) & 0xff; + } +} diff --git a/src/main/java/com/kitfox/svg/xml/Base64OutputStream.java b/src/main/java/com/kitfox/svg/xml/Base64OutputStream.java new file mode 100755 index 0000000..db167de --- /dev/null +++ b/src/main/java/com/kitfox/svg/xml/Base64OutputStream.java @@ -0,0 +1,90 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.kitfox.svg.xml; + +import java.io.FilterOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +/** + * + * @author kitfox + */ +public class Base64OutputStream extends FilterOutputStream +{ + int buf; + int numBytes; + int numChunks; + + public Base64OutputStream(OutputStream out) + { + super(out); + } + + public void flush() throws IOException + { + out.flush(); + } + + public void close() throws IOException + { + switch (numBytes) + { + case 1: + buf <<= 4; + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + out.write('='); + out.write('='); + break; + case 2: + buf <<= 2; + out.write(getBase64Byte(2)); + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + out.write('='); + break; + case 3: + out.write(getBase64Byte(3)); + out.write(getBase64Byte(2)); + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + break; + default: + assert false; + } + + out.close(); + } + + public void write(int b) throws IOException + { + buf = (buf << 8) | (0xff & b); + numBytes++; + + if (numBytes == 3) + { + out.write(getBase64Byte(3)); + out.write(getBase64Byte(2)); + out.write(getBase64Byte(1)); + out.write(getBase64Byte(0)); + + numBytes = 0; + numChunks++; + if (numChunks == 16) + { +// out.write('\r'); +// out.write('\n'); + numChunks = 0; + } + } + } + + public byte getBase64Byte(int index) + { + return Base64Util.encodeByte((buf >> (index * 6)) & 0x3f); + } +} diff --git a/src/main/java/com/kitfox/svg/xml/Base64Util.java b/src/main/java/com/kitfox/svg/xml/Base64Util.java new file mode 100755 index 0000000..a95b9d4 --- /dev/null +++ b/src/main/java/com/kitfox/svg/xml/Base64Util.java @@ -0,0 +1,32 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package com.kitfox.svg.xml; + +/** + * + * @author kitfox + */ +public class Base64Util +{ + static final byte[] valueToBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".getBytes(); + static final byte[] base64ToValue = new byte[128]; + static { + for (int i = 0; i < valueToBase64.length; ++i) + { + base64ToValue[valueToBase64[i]] = (byte)i; + } + } + + static public byte encodeByte(int value) + { + return valueToBase64[value]; + } + + static public byte decodeByte(int base64Char) + { + return base64ToValue[base64Char]; + } +} diff --git a/src/main/java/com/kitfox/svg/xml/StyleAttribute.java b/src/main/java/com/kitfox/svg/xml/StyleAttribute.java index 70cf71a..a3c9e63 100644 --- a/src/main/java/com/kitfox/svg/xml/StyleAttribute.java +++ b/src/main/java/com/kitfox/svg/xml/StyleAttribute.java @@ -226,7 +226,7 @@ public class StyleAttribute implements Serializable { try { String fragment = parseURLFn(); - if (fragment == null) fragment = stringValue; + if (fragment == null) fragment = stringValue.replaceAll("\\s+", ""); if (fragment == null) return null; //====================== -- cgit v1.2.3-55-g7522