1 51 package org.apache.fop.render.ps; 52 53 import java.io.OutputStream ; 54 import java.io.FilterOutputStream ; 55 import java.io.IOException ; 56 57 63 public class ASCII85OutputStream extends FilterOutputStream 64 implements Finalizable { 65 66 private static final int ZERO = 0x7A; private static final byte[] ZERO_ARRAY = {(byte)ZERO}; 68 private static final int START = 0x21; private static final int EOL = 0x0A; private static final byte[] EOD = {0x7E, 0x3E}; 72 private static final long base85_4 = 85; 73 private static final long base85_3 = base85_4 * base85_4; 74 private static final long base85_2 = base85_3 * base85_4; 75 private static final long base85_1 = base85_2 * base85_4; 76 77 private static final boolean DEBUG = false; 78 79 private int pos = 0; 80 private long buffer = 0; 81 private int posinline = 0; 82 private int bw = 0; 83 84 85 public ASCII85OutputStream(OutputStream out) { 86 super(out); 87 } 88 89 90 public void write(int b) throws IOException { 91 if (pos == 0) { 92 buffer += (b << 24) & 0xff000000L; 93 } else if (pos == 1) { 94 buffer += (b << 16) & 0xff0000L; 95 } else if (pos == 2) { 96 buffer += (b << 8) & 0xff00L; 97 } else { 98 buffer += b & 0xffL; 99 } 100 pos++; 101 102 if (pos > 3) { 103 checkedWrite(convertWord(buffer)); 104 buffer = 0; 105 pos = 0; 106 } 107 } 108 109 110 private void checkedWrite(int b) throws IOException { 111 if (posinline == 80) { 112 out.write(EOL); bw++; 113 posinline = 0; 114 } 115 checkedWrite(b); 116 posinline++; 117 bw++; 118 } 119 120 121 private void checkedWrite(byte[] buf) throws IOException { 122 checkedWrite(buf, buf.length, false); 123 } 124 125 126 private void checkedWrite(byte[] buf, boolean nosplit) throws IOException { 127 checkedWrite(buf, buf.length, nosplit); 128 } 129 130 131 private void checkedWrite(byte[] buf , int len) throws IOException { 132 checkedWrite(buf, len, false); 133 } 134 135 136 private void checkedWrite(byte[] buf , int len, boolean nosplit) throws IOException { 137 if (posinline + len > 80) { 138 int firstpart = (nosplit ? 0 : len - (posinline + len - 80)); 139 if (firstpart > 0) out.write(buf, 0, firstpart); 140 out.write(EOL); bw++; 141 int rest = len - firstpart; 142 if (rest > 0) out.write(buf, firstpart, rest); 143 posinline = rest; 144 } else { 145 out.write(buf, 0, len); 146 posinline += len; 147 } 148 bw += len; 149 } 150 151 152 161 private byte[] convertWord(long word) { 162 word = word & 0xffffffff; 163 164 if (word == 0) { 165 return ZERO_ARRAY; 166 } else { 167 if (word < 0) { 168 word = -word; 169 } 170 byte c1 = (byte)((word / base85_1) & 0xFF); 171 byte c2 = (byte)(((word - (c1 * base85_1)) / base85_2) & 0xFF); 172 byte c3 = 173 (byte)(((word - (c1 * base85_1) - (c2 * base85_2)) / base85_3) 174 & 0xFF); 175 byte c4 = 176 (byte)(((word - (c1 * base85_1) - (c2 * base85_2) - (c3 * base85_3)) / base85_4) 177 & 0xFF); 178 byte c5 = 179 (byte)(((word - (c1 * base85_1) - (c2 * base85_2) - (c3 * base85_3) - (c4 * base85_4))) 180 & 0xFF); 181 182 byte[] ret = { 183 (byte)(c1 + START), (byte)(c2 + START), 184 (byte)(c3 + START), (byte)(c4 + START), 185 (byte)(c5 + START) 186 }; 187 188 if (DEBUG) { 189 for (int i = 0; i < ret.length; i++) { 190 if (ret[i] < 33 || ret[i] > 117) { 191 System.out.println("Illegal char value " 192 + new Integer (ret[i])); 193 } 194 } 195 } 196 return ret; 197 } 198 } 199 200 201 public void finalizeStream() throws IOException { 202 if (pos > 0) { 207 int rest = pos; 208 224 225 byte[] conv; 226 if (buffer != 0) { 228 conv = convertWord(buffer); 229 } else { 230 conv = new byte[5]; 231 for (int j = 0; j < 5; j++) { 232 conv[j] = (byte)'!'; 233 } 234 } 235 checkedWrite(conv, rest + 1); 237 } 238 checkedWrite(EOD, true); 240 241 flush(); 242 if (out instanceof Finalizable) { 243 ((Finalizable)out).finalizeStream(); 244 } 245 } 246 247 248 public void close() throws IOException { 249 finalizeStream(); 250 super.close(); 251 } 252 253 } 254 255 256 | Popular Tags |