| 1 |
// Copyright 2001, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.FileInputStream; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.InputStream; |
| 7 |
|
| 8 |
/** |
| 9 |
* The ASCII85InputStream decodes ASCII base-85 encoded data. The exact |
| 10 |
* definition of ASCII base-85 encoding can be found in the PostScript Language |
| 11 |
* Reference (3rd ed.) chapter 3.13.3. |
| 12 |
* |
| 13 |
* IMPORTANT: inherits from InputStream rather than FilterInputStream so that |
| 14 |
* the correct read(byte[], int, int) method is used. |
| 15 |
* |
| 16 |
* @author Mark Donszelmann |
| 17 |
* @version $Id: ASCII85InputStream.java 8584 2006-08-10 23:06:37Z duns $ |
| 18 |
*/ |
| 19 |
public class ASCII85InputStream extends InputStream implements ASCII85 { |
| 20 |
|
| 21 |
private boolean endReached; |
| 22 |
|
| 23 |
private int b[] = new int[4]; |
| 24 |
|
| 25 |
private int bIndex; |
| 26 |
|
| 27 |
private int bLength; |
| 28 |
|
| 29 |
private int c[] = new int[5]; |
| 30 |
|
| 31 |
private int lineNo; |
| 32 |
|
| 33 |
private int prev; |
| 34 |
|
| 35 |
private InputStream in; |
| 36 |
|
| 37 |
/** |
| 38 |
* Create an ASCII85 Input Stream from given stream. |
| 39 |
* |
| 40 |
* @param input input to use |
| 41 |
*/ |
| 42 |
public ASCII85InputStream(InputStream input) { |
| 43 |
super(); |
| 44 |
in = input; |
| 45 |
bIndex = 0; |
| 46 |
bLength = 0; |
| 47 |
endReached = false; |
| 48 |
prev = -1; |
| 49 |
lineNo = 1; |
| 50 |
} |
| 51 |
|
| 52 |
public int read() throws IOException { |
| 53 |
|
| 54 |
if (bIndex >= bLength) { |
| 55 |
if (endReached) |
| 56 |
return -1; |
| 57 |
bLength = readTuple(); |
| 58 |
if (bLength < 0) |
| 59 |
return -1; |
| 60 |
bIndex = 0; |
| 61 |
} |
| 62 |
int a = b[bIndex]; |
| 63 |
bIndex++; |
| 64 |
|
| 65 |
return a; |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* @return current line number |
| 70 |
*/ |
| 71 |
public int getLineNo() { |
| 72 |
return lineNo; |
| 73 |
} |
| 74 |
|
| 75 |
private int readTuple() throws IOException, EncodingException { |
| 76 |
int cIndex = 0; |
| 77 |
int ch = -1; |
| 78 |
while ((!endReached) && (cIndex < 5)) { |
| 79 |
prev = ch; |
| 80 |
ch = in.read(); |
| 81 |
switch (ch) { |
| 82 |
case -1: |
| 83 |
throw new EncodingException( |
| 84 |
"missing '~>' at end of ASCII85 stream"); |
| 85 |
case 'z': |
| 86 |
b[0] = b[1] = b[2] = b[3] = '!'; |
| 87 |
return 4; |
| 88 |
case '~': |
| 89 |
if (in.read() != '>') |
| 90 |
throw new EncodingException("Invalid ASCII85 EOD"); |
| 91 |
endReached = true; |
| 92 |
break; |
| 93 |
case '\r': |
| 94 |
lineNo++; |
| 95 |
break; |
| 96 |
case '\n': |
| 97 |
if (prev != '\r') { |
| 98 |
lineNo++; |
| 99 |
} |
| 100 |
break; |
| 101 |
case ' ': |
| 102 |
case '\t': |
| 103 |
case '\f': |
| 104 |
case 0: |
| 105 |
// ignored |
| 106 |
break; |
| 107 |
default: |
| 108 |
c[cIndex] = ch; |
| 109 |
cIndex++; |
| 110 |
break; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if (cIndex > 0) { |
| 115 |
// fill the rest |
| 116 |
for (int i = 0; i < c.length; i++) { |
| 117 |
if (i >= cIndex) { |
| 118 |
c[i] = '!'; |
| 119 |
} else { |
| 120 |
c[i] -= '!'; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
// convert |
| 125 |
long d = ((c[0] * a85p4) + (c[1] * a85p3) + (c[2] * a85p2) |
| 126 |
+ (c[3] * a85p1) + c[4]) & 0x00000000FFFFFFFFL; |
| 127 |
|
| 128 |
b[0] = (int) ((d >> 24) & 0x00FF); |
| 129 |
b[1] = (int) ((d >> 16) & 0x00FF); |
| 130 |
b[2] = (int) ((d >> 8) & 0x00FF); |
| 131 |
b[3] = (int) (d & 0x00FF); |
| 132 |
} |
| 133 |
return cIndex - 1; |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Print out ASCII85 of a file |
| 138 |
* |
| 139 |
* @param args filename |
| 140 |
* @throws Exception when file does not exist |
| 141 |
*/ |
| 142 |
public static void main(String[] args) throws Exception { |
| 143 |
if (args.length < 1) { |
| 144 |
System.err.println("Usage: ASCII85InputStream filename"); |
| 145 |
System.exit(1); |
| 146 |
} |
| 147 |
ASCII85InputStream in = new ASCII85InputStream(new FileInputStream(args[0])); |
| 148 |
int b = in.read(); |
| 149 |
while (b != -1) { |
| 150 |
System.out.write(b); |
| 151 |
b = in.read(); |
| 152 |
} |
| 153 |
in.close(); |
| 154 |
System.out.flush(); |
| 155 |
} |
| 156 |
} |