1 31 package com.imagero.uio.io; 32 33 import java.io.FilterInputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 37 43 public class ASCII85InputStream extends FilterInputStream { 44 private int index; 45 private int n; 46 private boolean eof; 47 48 private byte[] ascii; 49 private byte[] b; 50 51 56 public ASCII85InputStream(InputStream is) { 57 super(is); 58 ascii = new byte[5]; 59 b = new byte[4]; 60 } 61 62 69 public final int read() throws IOException { 70 if (index >= n) { 71 if (eof) { 72 return -1; 73 } 74 index = 0; 75 int k; 76 byte z; 77 do { 78 int zz = (byte) in.read(); 79 if (zz == -1) { 80 eof = true; 81 return -1; 82 } 83 z = (byte) zz; 84 } 85 while (z == '\n' || z == '\r' || z == ' '); 86 87 if (z == '~' || z == 'x') { 88 eof = true; 89 ascii = b = null; 90 n = 0; 91 return -1; 92 } 93 else if (z == 'z') { 94 b[0] = b[1] = b[2] = b[3] = 0; 95 n = 4; 96 } 97 else { 98 ascii[0] = z; for (k = 1; k < 5; ++k) { 100 do { 101 int zz = (byte) in.read(); 102 if (zz == -1) { 103 eof = true; 104 return -1; 105 } 106 z = (byte) zz; 107 } 108 while (z == '\n' || z == '\r' || z == ' '); 109 ascii[k] = z; 110 if (z == '~' || z == 'x') { 111 break; 112 } 113 } 114 n = k - 1; 115 if (n == 0) { 116 eof = true; 117 ascii = null; 118 b = null; 119 return -1; 120 } 121 if (k < 5) { 122 for (++k; k < 5; ++k) { 123 ascii[k] = 0x21; 124 } 125 eof = true; 126 } 127 long t = 0; 129 for (k = 0; k < 5; ++k) { 130 z = (byte) (ascii[k] - 0x21); 131 if (z < 0 || z > 93) { 132 n = 0; 133 eof = true; 134 ascii = null; 135 b = null; 136 throw new IOException ("Invalid data in Ascii85 stream"); 137 } 138 t = (t * 85L) + z; 139 } 140 for (k = 3; k >= 0; --k) { 141 b[k] = (byte) (t & 0xFFL); 142 t >>>= 8; 143 } 144 } 145 } 146 return b[index++] & 0xFF; 147 } 148 149 160 public final int read(byte[] data, int offset, int len) throws IOException { 161 if (eof && index >= n) { 162 return -1; 163 } 164 for (int i = 0; i < len; i++) { 165 if (index < n) { 166 data[i + offset] = b[index++]; 167 } 168 else { 169 int t = read(); 170 if (t == -1) { 171 return i; 172 } 173 data[i + offset] = (byte) t; 174 } 175 } 176 return len; 177 } 178 179 184 public void close() throws IOException { 185 ascii = null; 186 eof = true; 187 b = null; 188 super.close(); 189 } 190 191 196 public boolean markSupported() { 197 return false; 198 } 199 200 207 public long skip(long nValue) { 208 return 0; 209 } 210 211 216 public int available() { 217 if (eof) { 218 return 0; 219 } 220 else { 221 return 1; 222 } 223 } 224 225 230 public void mark(int readlimit) { 231 } 232 233 238 public void reset() throws IOException { 239 throw new IOException ("Reset is not supported"); 240 } 241 } 242 | Popular Tags |