| 1 |
// Copyright 2001-2007, FreeHEP. |
| 2 |
package org.freehep.util.io; |
| 3 |
|
| 4 |
import java.io.FilterOutputStream; |
| 5 |
import java.io.IOException; |
| 6 |
import java.io.OutputStream; |
| 7 |
|
| 8 |
/** |
| 9 |
* The ASCII85InputStream encodes binary data as ASCII base-85 encoding. The |
| 10 |
* exact definition of ASCII base-85 encoding can be found in the PostScript |
| 11 |
* Language Reference (3rd ed.) chapter 3.13.3. |
| 12 |
* |
| 13 |
* @author Mark Donszelmann |
| 14 |
* @version $Id: ASCII85OutputStream.java 10259 2007-01-05 22:52:09Z duns $ |
| 15 |
*/ |
| 16 |
public class ASCII85OutputStream extends FilterOutputStream implements ASCII85, |
| 17 |
FinishableOutputStream { |
| 18 |
|
| 19 |
private boolean end; |
| 20 |
|
| 21 |
private int characters; |
| 22 |
|
| 23 |
private int b[] = new int[4]; |
| 24 |
|
| 25 |
private int bIndex; |
| 26 |
|
| 27 |
private int c[] = new int[5]; |
| 28 |
|
| 29 |
private String newline = "\n"; |
| 30 |
|
| 31 |
/** |
| 32 |
* Create an ASCII85 Output Stream from given stream |
| 33 |
* |
| 34 |
* @param out output stream to use |
| 35 |
*/ |
| 36 |
public ASCII85OutputStream(OutputStream out) { |
| 37 |
super(out); |
| 38 |
characters = MAX_CHARS_PER_LINE; |
| 39 |
end = false; |
| 40 |
bIndex = 0; |
| 41 |
try { |
| 42 |
newline = System.getProperty("line.separator"); |
| 43 |
} catch (SecurityException e) { |
| 44 |
// ignored; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public void write(int a) throws IOException { |
| 49 |
b[bIndex] = a & 0x00FF; |
| 50 |
bIndex++; |
| 51 |
if (bIndex >= b.length) { |
| 52 |
writeTuple(); |
| 53 |
bIndex = 0; |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public void finish() throws IOException { |
| 58 |
if (!end) { |
| 59 |
end = true; |
| 60 |
if (bIndex > 0) { |
| 61 |
writeTuple(); |
| 62 |
} |
| 63 |
writeEOD(); |
| 64 |
flush(); |
| 65 |
if (out instanceof FinishableOutputStream) { |
| 66 |
((FinishableOutputStream) out).finish(); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public void close() throws IOException { |
| 72 |
finish(); |
| 73 |
super.close(); |
| 74 |
} |
| 75 |
|
| 76 |
private void writeTuple() throws IOException { |
| 77 |
// fill the rest |
| 78 |
for (int i = bIndex; i < b.length; i++) { |
| 79 |
b[i] = 0; |
| 80 |
} |
| 81 |
|
| 82 |
// convert |
| 83 |
long d = ((b[0] << 24) | (b[1] << 16) | (b[2] << 8) | b[3]) & 0x00000000FFFFFFFFL; |
| 84 |
|
| 85 |
c[0] = (int) (d / a85p4 + '!'); |
| 86 |
d = d % a85p4; |
| 87 |
c[1] = (int) (d / a85p3 + '!'); |
| 88 |
d = d % a85p3; |
| 89 |
c[2] = (int) (d / a85p2 + '!'); |
| 90 |
d = d % a85p2; |
| 91 |
c[3] = (int) (d / a85p1 + '!'); |
| 92 |
c[4] = (int) (d % a85p1 + '!'); |
| 93 |
|
| 94 |
// convert !!!!! to z |
| 95 |
if ((bIndex >= b.length) && (c[0] == '!') && (c[1] == '!') |
| 96 |
&& (c[2] == '!') && (c[3] == '!') && (c[4] == '!')) { |
| 97 |
writeChar('z'); |
| 98 |
} else { |
| 99 |
for (int i = 0; i < bIndex + 1; i++) { |
| 100 |
writeChar(c[i]); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Fix for IO-7 |
| 106 |
private void writeEOD() throws IOException { |
| 107 |
if (characters <= 1) { |
| 108 |
characters = MAX_CHARS_PER_LINE; |
| 109 |
writeNewLine(); |
| 110 |
} |
| 111 |
characters -= 2; |
| 112 |
super.write('~'); |
| 113 |
super.write('>'); |
| 114 |
} |
| 115 |
|
| 116 |
private void writeChar(int b) throws IOException { |
| 117 |
if (characters == 0) { |
| 118 |
characters = MAX_CHARS_PER_LINE; |
| 119 |
writeNewLine(); |
| 120 |
} |
| 121 |
characters--; |
| 122 |
super.write(b); |
| 123 |
} |
| 124 |
|
| 125 |
private void writeNewLine() throws IOException { |
| 126 |
// write a newline |
| 127 |
for (int i=0; i < newline.length(); i++) { |
| 128 |
super.write(newline.charAt(i)); |
| 129 |
} |
| 130 |
} |
| 131 |
} |