1 35 package com.lowagie.text.pdf; 36 import java.io.IOException ; 37 import java.io.OutputStream ; 38 39 import com.lowagie.text.ExceptionConverter; 40 45 public class LZWDecoder { 46 47 byte stringTable[][]; 48 byte data[] = null; 49 OutputStream uncompData; 50 int tableIndex, bitsToGet = 9; 51 int bytePointer, bitPointer; 52 int nextData = 0; 53 int nextBits = 0; 54 55 int andTable[] = { 56 511, 57 1023, 58 2047, 59 4095 60 }; 61 62 public LZWDecoder() { 63 } 64 65 71 public void decode(byte data[], OutputStream uncompData) { 72 73 if(data[0] == (byte)0x00 && data[1] == (byte)0x01) { 74 throw new RuntimeException ("LZW flavour not supported."); 75 } 76 77 initializeStringTable(); 78 79 this.data = data; 80 this.uncompData = uncompData; 81 82 bytePointer = 0; 84 bitPointer = 0; 85 86 nextData = 0; 87 nextBits = 0; 88 89 int code, oldCode = 0; 90 byte string[]; 91 92 while ((code = getNextCode()) != 257) { 93 94 if (code == 256) { 95 96 initializeStringTable(); 97 code = getNextCode(); 98 99 if (code == 257) { 100 break; 101 } 102 103 writeString(stringTable[code]); 104 oldCode = code; 105 106 } else { 107 108 if (code < tableIndex) { 109 110 string = stringTable[code]; 111 112 writeString(string); 113 addStringToTable(stringTable[oldCode], string[0]); 114 oldCode = code; 115 116 } else { 117 118 string = stringTable[oldCode]; 119 string = composeString(string, string[0]); 120 writeString(string); 121 addStringToTable(string); 122 oldCode = code; 123 } 124 } 125 } 126 } 127 128 129 132 public void initializeStringTable() { 133 134 stringTable = new byte[8192][]; 135 136 for (int i=0; i<256; i++) { 137 stringTable[i] = new byte[1]; 138 stringTable[i][0] = (byte)i; 139 } 140 141 tableIndex = 258; 142 bitsToGet = 9; 143 } 144 145 148 public void writeString(byte string[]) { 149 try { 150 uncompData.write(string); 151 } 152 catch (IOException e) { 153 throw new ExceptionConverter(e); 154 } 155 } 156 157 160 public void addStringToTable(byte oldString[], byte newString) { 161 int length = oldString.length; 162 byte string[] = new byte[length + 1]; 163 System.arraycopy(oldString, 0, string, 0, length); 164 string[length] = newString; 165 166 stringTable[tableIndex++] = string; 168 169 if (tableIndex == 511) { 170 bitsToGet = 10; 171 } else if (tableIndex == 1023) { 172 bitsToGet = 11; 173 } else if (tableIndex == 2047) { 174 bitsToGet = 12; 175 } 176 } 177 178 181 public void addStringToTable(byte string[]) { 182 183 stringTable[tableIndex++] = string; 185 186 if (tableIndex == 511) { 187 bitsToGet = 10; 188 } else if (tableIndex == 1023) { 189 bitsToGet = 11; 190 } else if (tableIndex == 2047) { 191 bitsToGet = 12; 192 } 193 } 194 195 198 public byte[] composeString(byte oldString[], byte newString) { 199 int length = oldString.length; 200 byte string[] = new byte[length + 1]; 201 System.arraycopy(oldString, 0, string, 0, length); 202 string[length] = newString; 203 204 return string; 205 } 206 207 public int getNextCode() { 209 try { 214 nextData = (nextData << 8) | (data[bytePointer++] & 0xff); 215 nextBits += 8; 216 217 if (nextBits < bitsToGet) { 218 nextData = (nextData << 8) | (data[bytePointer++] & 0xff); 219 nextBits += 8; 220 } 221 222 int code = 223 (nextData >> (nextBits - bitsToGet)) & andTable[bitsToGet-9]; 224 nextBits -= bitsToGet; 225 226 return code; 227 } catch(ArrayIndexOutOfBoundsException e) { 228 return 257; 230 } 231 } 232 } 233 | Popular Tags |