diff options
Diffstat (limited to 'src/net/sourceforge/plantuml/code/AsciiEncoder.java')
-rw-r--r-- | src/net/sourceforge/plantuml/code/AsciiEncoder.java | 50 |
1 files changed, 37 insertions, 13 deletions
diff --git a/src/net/sourceforge/plantuml/code/AsciiEncoder.java b/src/net/sourceforge/plantuml/code/AsciiEncoder.java index c804508..34db87e 100644 --- a/src/net/sourceforge/plantuml/code/AsciiEncoder.java +++ b/src/net/sourceforge/plantuml/code/AsciiEncoder.java @@ -37,10 +37,11 @@ package net.sourceforge.plantuml.code; public class AsciiEncoder implements URLEncoder { - final private char encode6bit[] = new char[64]; - final private byte decode6bit[] = new byte[128]; + // Temporary because of AsciiEncoderFinalZeros + final static/* private */char encode6bit[] = new char[64]; + final static/* private */byte decode6bit[] = new byte[128]; - public AsciiEncoder() { + static { for (byte b = 0; b < 64; b++) { encode6bit[b] = encode6bit(b); decode6bit[encode6bit[b]] = b; @@ -51,32 +52,55 @@ public class AsciiEncoder implements URLEncoder { if (data == null) { return ""; } - final StringBuilder resu = new StringBuilder((data.length * 4 + 2) / 3); + final StringBuilder result = new StringBuilder((data.length * 4 + 2) / 3); for (int i = 0; i < data.length; i += 3) { - append3bytes(resu, data[i] & 0xFF, i + 1 < data.length ? data[i + 1] & 0xFF : 0, + append3bytes(result, data[i] & 0xFF, i + 1 < data.length ? data[i + 1] & 0xFF : 0, i + 2 < data.length ? data[i + 2] & 0xFF : 0); } - return resu.toString(); + return result.toString(); } public byte[] decode(String s) { - if (s.length() % 4 != 0) { - throw new IllegalArgumentException("Cannot decode " + s); - } - final byte data[] = new byte[(s.length() * 3 + 3) / 4]; + // if (s.length() % 4 != 0) { + // throw new IllegalArgumentException("Cannot decode " + s); + // } + final byte data[] = new byte[computeSize(s.length())]; int pos = 0; for (int i = 0; i < s.length(); i += 4) { - decode3bytes(data, pos, s.charAt(i), s.charAt(i + 1), s.charAt(i + 2), s.charAt(i + 3)); + decode3bytes(data, pos, scharAt(s, i), scharAt(s, i + 1), scharAt(s, i + 2), scharAt(s, i + 3)); pos += 3; } return data; } - public int decode6bit(char c) { + private int computeSize(int length) { + // while (length % 4 != 0) { + // length++; + // } + final int r = length % 4; + if (r != 0) { + length += 4 - r; + } + // System.err.println("length=" + length); + // System.err.println("length1=" + (length % 4)); + // length += length % 4; + // System.err.println("length2=" + length); + assert length % 4 == 0 : "length=" + length; + return (length * 3 + 3) / 4; + } + + private char scharAt(String s, int i) { + if (i >= s.length()) { + return '0'; + } + return s.charAt(i); + } + + public static int decode6bit(char c) { return decode6bit[c]; } - public char encode6bit(byte b) { + public static char encode6bit(byte b) { assert b >= 0 && b < 64; if (b < 10) { return (char) ('0' + b); |