001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: /* $Id: ASCII85OutputStream.java 426584 2006-07-28 16:01:47Z jeremias $ */
019:
020: package org.apache.xmlgraphics.util.io;
021:
022: import java.io.OutputStream;
023: import java.io.FilterOutputStream;
024: import java.io.IOException;
025:
026: /**
027: * This class applies a ASCII85 encoding to the stream.
028: *
029: * @version $Id: ASCII85OutputStream.java 426584 2006-07-28 16:01:47Z jeremias $
030: */
031: public class ASCII85OutputStream extends FilterOutputStream implements
032: ASCII85Constants, Finalizable {
033:
034: private static final boolean DEBUG = false;
035:
036: private int pos = 0;
037: private long buffer = 0;
038: private int posinline = 0;
039: private int bw = 0;
040:
041: /** @see java.io.FilterOutputStream **/
042: public ASCII85OutputStream(OutputStream out) {
043: super (out);
044: }
045:
046: /** @see java.io.FilterOutputStream **/
047: public void write(int b) throws IOException {
048: if (pos == 0) {
049: buffer += (b << 24) & 0xff000000L;
050: } else if (pos == 1) {
051: buffer += (b << 16) & 0xff0000L;
052: } else if (pos == 2) {
053: buffer += (b << 8) & 0xff00L;
054: } else {
055: buffer += b & 0xffL;
056: }
057: pos++;
058:
059: if (pos > 3) {
060: checkedWrite(convertWord(buffer));
061: buffer = 0;
062: pos = 0;
063: }
064: }
065:
066: /* UNUSED ATM
067: private void checkedWrite(int b) throws IOException {
068: if (posinline == 80) {
069: out.write(EOL); bw++;
070: posinline = 0;
071: }
072: checkedWrite(b);
073: posinline++;
074: bw++;
075: }*/
076:
077: private void checkedWrite(byte[] buf) throws IOException {
078: checkedWrite(buf, buf.length, false);
079: }
080:
081: private void checkedWrite(byte[] buf, boolean nosplit)
082: throws IOException {
083: checkedWrite(buf, buf.length, nosplit);
084: }
085:
086: private void checkedWrite(byte[] buf, int len) throws IOException {
087: checkedWrite(buf, len, false);
088: }
089:
090: private void checkedWrite(byte[] buf, int len, boolean nosplit)
091: throws IOException {
092: if (posinline + len > 80) {
093: int firstpart = (nosplit ? 0 : len - (posinline + len - 80));
094: if (firstpart > 0) {
095: out.write(buf, 0, firstpart);
096: }
097: out.write(EOL);
098: bw++;
099: int rest = len - firstpart;
100: if (rest > 0) {
101: out.write(buf, firstpart, rest);
102: }
103: posinline = rest;
104: } else {
105: out.write(buf, 0, len);
106: posinline += len;
107: }
108: bw += len;
109: }
110:
111: /**
112: * This converts a 32 bit value (4 bytes) into 5 bytes using base 85.
113: * each byte in the result starts with zero at the '!' character so
114: * the resulting base85 number fits into printable ascii chars
115: *
116: * @param word the 32 bit unsigned (hence the long datatype) word
117: * @return 5 bytes (or a single byte of the 'z' character for word
118: * values of 0)
119: */
120: private byte[] convertWord(long word) {
121: word = word & 0xffffffff;
122:
123: if (word == 0) {
124: return ZERO_ARRAY;
125: } else {
126: if (word < 0) {
127: word = -word;
128: }
129: byte c1 = (byte) ((word / POW85[0]) & 0xFF);
130: byte c2 = (byte) (((word - (c1 * POW85[0])) / POW85[1]) & 0xFF);
131: byte c3 = (byte) (((word - (c1 * POW85[0]) - (c2 * POW85[1])) / POW85[2]) & 0xFF);
132: byte c4 = (byte) (((word - (c1 * POW85[0])
133: - (c2 * POW85[1]) - (c3 * POW85[2])) / POW85[3]) & 0xFF);
134: byte c5 = (byte) (((word - (c1 * POW85[0])
135: - (c2 * POW85[1]) - (c3 * POW85[2]) - (c4 * POW85[3]))) & 0xFF);
136:
137: byte[] ret = { (byte) (c1 + START), (byte) (c2 + START),
138: (byte) (c3 + START), (byte) (c4 + START),
139: (byte) (c5 + START) };
140:
141: if (DEBUG) {
142: for (int i = 0; i < ret.length; i++) {
143: if (ret[i] < 33 || ret[i] > 117) {
144: System.out.println("Illegal char value "
145: + new Integer(ret[i]));
146: }
147: }
148: }
149: return ret;
150: }
151: }
152:
153: /** @see Finalizable **/
154: public void finalizeStream() throws IOException {
155: // now take care of the trailing few bytes.
156: // with n leftover bytes, we append 0 bytes to make a full group of 4
157: // then convert like normal (except not applying the special zero rule)
158: // and write out the first n+1 bytes from the result
159: if (pos > 0) {
160: int rest = pos;
161: /*
162: byte[] lastdata = new byte[4];
163: int i = 0;
164: for (int j = 0; j < 4; j++) {
165: if (j < rest) {
166: lastdata[j] = data[i++];
167: } else {
168: lastdata[j] = 0;
169: }
170: }
171:
172: long val = ((lastdata[0] << 24) & 0xff000000L)
173: + ((lastdata[1] << 16) & 0xff0000L)
174: + ((lastdata[2] << 8) & 0xff00L)
175: + (lastdata[3] & 0xffL);
176: */
177:
178: byte[] conv;
179: // special rule for handling zeros at the end
180: if (buffer != 0) {
181: conv = convertWord(buffer);
182: } else {
183: conv = new byte[5];
184: for (int j = 0; j < 5; j++) {
185: conv[j] = (byte) '!';
186: }
187: }
188: // assert rest+1 <= 5
189: checkedWrite(conv, rest + 1);
190: }
191: // finally write the two character end of data marker
192: checkedWrite(EOD, true);
193:
194: flush();
195: if (out instanceof Finalizable) {
196: ((Finalizable) out).finalizeStream();
197: }
198: }
199:
200: /** @see java.io.FilterOutputStream **/
201: public void close() throws IOException {
202: finalizeStream();
203: super.close();
204: }
205:
206: }
|