1 51 package org.apache.fop.pdf; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.UnsupportedEncodingException ; 55 56 public class ASCII85Filter extends PDFFilter { 57 private static final char ASCII85_ZERO = 'z'; 58 private static final char ASCII85_START = '!'; 59 private static final String ASCII85_EOD = "~>"; 60 61 private static final long base85_4 = 85; 62 66 67 68 public String getName() { 69 return "/ASCII85Decode"; 70 } 71 72 public String getDecodeParms() { 73 return null; 74 } 75 76 public byte[] encode(byte[] data) { 77 78 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 79 80 int i; 81 int total = 0; 82 int diff = 0; 83 84 for (i = 0; i + 3 < data.length; i += 4) { 87 88 long val = ((data[i] << 24) 89 & 0xff000000L) + ((data[i + 1] << 16) & 0xff0000L) + ((data[i + 2] << 8) & 0xff00L) + (data[i + 3] & 0xffL); byte[] conv = convertWord(val); 94 95 buffer.write(conv, 0, conv.length); 96 97 } 98 99 if (i < data.length) { 104 int n = data.length - i; 105 byte[] lastdata = new byte[4]; 106 for (int j = 0; j < 4; j++) { 107 if (j < n) { 108 lastdata[j] = data[i++]; 109 } else { 110 lastdata[j] = 0; 111 } 112 } 113 114 long val = ((lastdata[0] << 24) & 0xff000000L) 115 + ((lastdata[1] << 16) & 0xff0000L) 116 + ((lastdata[2] << 8) & 0xff00L) 117 + (lastdata[3] & 0xffL); 118 byte[] conv = convertWord(val); 119 120 if (val == 0) { 122 conv = new byte[5]; 123 for (int j = 0; j < 5; j++) { 124 conv[j] = (byte)'!'; 125 } 126 } 127 buffer.write(conv, 0, n + 1); 129 131 } 132 byte[] eod; 134 try { 135 eod = ASCII85_EOD.getBytes(PDFDocument.ENCODING); 136 } catch (UnsupportedEncodingException ue) { 137 eod = ASCII85_EOD.getBytes(); 138 } 139 buffer.write(eod, 0, eod.length); 140 byte[] result = buffer.toByteArray(); 141 142 151 return result; 152 153 } 154 155 164 private byte[] convertWord(long word) { 165 word = word & 0xffffffff; 166 if (word < 0) { 167 word = -word; 168 } 169 170 if (word == 0) { 171 byte[] result = { 172 (byte)ASCII85_ZERO 173 }; 174 return result; 175 } else { 176 188 byte c5 = (byte)((word % base85_4) + ASCII85_START); 193 word = word / base85_4; 194 byte c4 = (byte)((word % base85_4) + ASCII85_START); 195 word = word / base85_4; 196 byte c3 = (byte)((word % base85_4) + ASCII85_START); 197 word = word / base85_4; 198 byte c2 = (byte)((word % base85_4) + ASCII85_START); 199 word = word / base85_4; 200 byte c1 = (byte)((word % base85_4) + ASCII85_START); 201 202 byte[] ret = { 203 c1 , c2, c3, c4, c5 204 }; 205 for (int i = 0; i < ret.length; i++) { 206 if (ret[i] < 33 || ret[i] > 117) { 207 System.out.println("illegal char value " 208 + new Integer (ret[i])); 209 } 210 } 211 212 return ret; 213 214 215 } 216 } 217 218 } 219 | Popular Tags |