1 8 package org.enhydra.oyster.mail; 9 10 import java.io.InputStream ; 11 import java.io.FileInputStream ; 12 import java.io.BufferedInputStream ; 13 import java.io.IOException ; 14 import java.io.File ; 15 import org.enhydra.oyster.exception.SMIMEException; 16 17 38 public class ContentAnalyzer 39 { 40 41 44 private int numberOfBytes = 0; 45 46 50 private int numberOfNon7bitBytes = 0; 51 52 56 private int lineLenght = 0; 57 58 61 private boolean errorInLineLenght = false; 62 63 66 private boolean CRCharIndcator = false; 67 68 71 public ContentAnalyzer() { 72 73 } 74 75 82 public void isCharFor7BitEncoding(int dataToCheck) { 83 lineLenght++; 84 numberOfBytes++; 85 if(lineLenght > 1000) 86 errorInLineLenght = true; 87 if(dataToCheck < 0x7F && dataToCheck > 0x1F) { 88 return; 89 } 90 if(dataToCheck == 0x09) 91 return; 92 if(dataToCheck == 0x0D) { 93 if(CRCharIndcator) { 94 numberOfNon7bitBytes++; 95 return; 96 } 97 else { 98 CRCharIndcator = true; 99 return; 100 } 101 } 102 if(dataToCheck == 0x0A) { 103 if(CRCharIndcator) { 104 CRCharIndcator = false; 105 lineLenght = 0; return; 107 } 108 else { 109 numberOfNon7bitBytes++; 110 return; 111 } 112 } 113 numberOfNon7bitBytes++; 114 } 115 116 124 public void isFor7BitEncoding(InputStream dataToCheck) throws SMIMEException { 125 BufferedInputStream buf = new BufferedInputStream (dataToCheck, 1000); 126 try { 127 int available = buf.available(); 128 int markPoint = 0; 129 while(markPoint < available) { 130 buf.mark(markPoint); 131 buf.reset(); 132 int byteVal = buf.read(); 133 while(byteVal != -1) { 134 this.isCharFor7BitEncoding(byteVal); 135 byteVal = buf.read(); 136 } 137 markPoint+=1000; 138 } 139 buf.close(); 140 } 141 catch(IOException e) { 142 throw SMIMEException.getInstance(this, e, "isFor7BitEncoding"); 143 } 144 } 145 146 153 public void isFor7BitEncoding(File fileToCheck) throws SMIMEException { 154 try { 155 FileInputStream fi = new FileInputStream (fileToCheck); 156 this.isFor7BitEncoding(fi); 157 } 158 catch(IOException e) { 159 throw SMIMEException.getInstance(this, e, "isFor7BitEncoding"); } 160 } 161 162 166 public void analyse() { 167 168 } 169 170 175 public String getPreferedEncoding() { 176 if(numberOfBytes == 0) 177 return "7bit"; 178 if( numberOfNon7bitBytes == 0 && !errorInLineLenght) 179 return "7bit"; 180 if( numberOfNon7bitBytes == 0 && errorInLineLenght) 181 return "quoted-printable"; 182 if( numberOfNon7bitBytes != 0) { 183 int percentage = 100 * numberOfNon7bitBytes / numberOfBytes; 184 if(percentage > 30) 185 return "base64"; 186 else 187 return "quoted-printable"; 188 } 189 return null; 190 } 191 192 196 public void reset() { 197 numberOfBytes = 0; 198 numberOfNon7bitBytes = 0; 199 lineLenght = 0; 200 errorInLineLenght = false; 201 CRCharIndcator = false; 202 } 203 } | Popular Tags |