1 7 package javax.swing.text.rtf; 8 9 import java.io.*; 10 import java.lang.*; 11 12 31 abstract class AbstractFilter extends OutputStream 32 { 33 34 protected char translationTable[]; 35 37 protected boolean specialsTable[]; 38 39 40 static final char latin1TranslationTable[]; 41 42 static final boolean noSpecialsTable[]; 43 44 static final boolean allSpecialsTable[]; 45 46 static { 47 int i; 48 49 noSpecialsTable = new boolean[256]; 50 for (i = 0; i < 256; i++) 51 noSpecialsTable[i] = false; 52 53 allSpecialsTable = new boolean[256]; 54 for (i = 0; i < 256; i++) 55 allSpecialsTable[i] = true; 56 57 latin1TranslationTable = new char[256]; 58 for (i = 0; i < 256; i++) 59 latin1TranslationTable[i] = (char)i; 60 } 61 62 75 public void readFromStream(InputStream in) 76 throws IOException 77 { 78 byte buf[]; 79 int count; 80 81 buf = new byte[16384]; 82 83 while(true) { 84 count = in.read(buf); 85 if (count < 0) 86 break; 87 88 this.write(buf, 0, count); 89 } 90 } 91 92 public void readFromReader(Reader in) 93 throws IOException 94 { 95 char buf[]; 96 int count; 97 98 buf = new char[2048]; 99 100 while(true) { 101 count = in.read(buf); 102 if (count < 0) 103 break; 104 for (int i = 0; i < count; i++) { 105 this.write(buf[i]); 106 } 107 } 108 } 109 110 public AbstractFilter() 111 { 112 translationTable = latin1TranslationTable; 113 specialsTable = noSpecialsTable; 114 } 115 116 120 public void write(int b) 121 throws IOException 122 { 123 if (b < 0) 124 b += 256; 125 if (specialsTable[b]) 126 writeSpecial(b); 127 else { 128 char ch = translationTable[b]; 129 if (ch != (char)0) 130 write(ch); 131 } 132 } 133 134 142 public void write(byte[] buf, int off, int len) 143 throws IOException 144 { 145 StringBuffer accumulator = null; 146 while (len > 0) { 147 short b = (short)buf[off]; 148 149 if (b < 0) 151 b += 256; 152 153 if (specialsTable[b]) { 154 if (accumulator != null) { 155 write(accumulator.toString()); 156 accumulator = null; 157 } 158 writeSpecial(b); 159 } else { 160 char ch = translationTable[b]; 161 if (ch != (char)0) { 162 if (accumulator == null) 163 accumulator = new StringBuffer (); 164 accumulator.append(ch); 165 } 166 } 167 168 len --; 169 off ++; 170 } 171 172 if (accumulator != null) 173 write(accumulator.toString()); 174 } 175 176 184 public void write(String s) 185 throws IOException 186 { 187 int index, length; 188 189 length = s.length(); 190 for(index = 0; index < length; index ++) { 191 write(s.charAt(index)); 192 } 193 } 194 195 201 protected abstract void write(char ch) throws IOException; 202 203 210 protected abstract void writeSpecial(int b) throws IOException; 211 } 212 213 214 | Popular Tags |