1 57 58 59 package org.enhydra.apache.xml.serialize; 60 61 import java.io.ByteArrayOutputStream ; 62 import java.io.IOException ; 63 import java.io.OutputStreamWriter ; 64 import java.io.Writer ; 65 66 71 public class SieveEncodingInfo extends EncodingInfo { 72 73 BAOutputStream checkerStream = null; 74 Writer checkerWriter = null; 75 String dangerChars = null; 76 77 82 public SieveEncodingInfo(String mimeName, String javaName, 83 int lastPrintable, String dangers) { 84 super(mimeName, javaName, lastPrintable); 85 this.dangerChars = dangers; 86 } 87 88 91 public SieveEncodingInfo(String mimeName, int lastPrintable) { 92 this(mimeName, mimeName, lastPrintable, null); 93 } 94 95 100 public boolean isPrintable(int ch) { 101 if (this.dangerChars != null && ch <= 0xffff) { 102 106 if (this.dangerChars.indexOf(ch) >= 0) 107 return false; 108 } 109 110 if (ch <= this.lastPrintable) 111 return true; 112 113 boolean printable = true; 114 synchronized (this) { 115 try { 116 if (this.checkerWriter == null) { 117 this.checkerStream = new BAOutputStream(10); 118 this.checkerWriter = new OutputStreamWriter (this.checkerStream, this.javaName); 119 } 120 121 if (ch > 0xffff) { 122 this.checkerWriter.write(((ch-0x10000)>>10)+0xd800); 123 this.checkerWriter.write(((ch-0x10000)&0x3ff)+0xdc00); 124 byte[] result = this.checkerStream.getBuffer(); 125 if (this.checkerStream.size() == 2 && result[0] == '?' && result[1] == '?') 126 printable = false; 127 } else { 128 this.checkerWriter.write(ch); 129 this.checkerWriter.flush(); 130 byte[] result = this.checkerStream.getBuffer(); 131 if (this.checkerStream.size() == 1 && result[0] == '?') 132 printable = false; 133 } 134 this.checkerStream.reset(); 135 } catch (IOException ioe) { 136 printable = false; 137 } 138 } 139 140 return printable; 141 } 142 143 148 static class BAOutputStream extends ByteArrayOutputStream { 149 BAOutputStream() { 150 super(); 151 } 152 153 BAOutputStream(int size) { 154 super(size); 155 } 156 157 byte[] getBuffer() { 158 return this.buf; 159 } 160 } 161 162 } 163 | Popular Tags |