1 package org.jahia.utils.fileparsers; 2 3 import java.io.*; 4 import java.net.*; 5 6 import org.mozilla.intl.chardet.*; 7 8 17 public class CharsetDetection implements nsICharsetDetectionObserver { 18 19 private static org.apache.log4j.Logger logger = 20 org.apache.log4j.Logger.getLogger (CharsetDetection.class); 21 22 private String charSet = null; 23 24 public CharsetDetection(){ 25 26 } 27 28 33 public String getCharset(){ 34 return this.charSet; 35 } 36 37 44 public int charsetDetection(InputStream ins) throws IOException { 45 return charsetDetection(nsPSMDetector.ALL , ins); 46 } 47 48 55 public int charsetDetection(URL url) throws IOException { 56 57 if ( url == null ){ 58 return -1; 59 } 60 return charsetDetection(nsPSMDetector.ALL , url.openStream()); 61 } 62 63 71 public int charsetDetection(int lang, URL url) throws IOException { 72 73 if ( url == null ){ 74 return -1; 75 } 76 return charsetDetection(lang, url.openStream()); 77 } 78 79 87 public int charsetDetection(int lang, InputStream ins) throws IOException { 88 89 if ( ins == null ){ 90 return -1; 91 } 92 nsDetector det = new nsDetector(lang); 93 det.Init(this); 94 BufferedInputStream imp = new BufferedInputStream(ins); 95 96 byte[] buf = new byte[1024]; 97 int len; 98 boolean done = false; 99 boolean isAscii = true; 100 101 while ( (len = imp.read(buf, 0, buf.length)) != -1) { 102 103 if (isAscii) 105 isAscii = det.isAscii(buf, len); 106 107 if (!isAscii && !done) 109 done = det.DoIt(buf, len, true); 110 } 111 det.DataEnd(); 112 113 String []charSets = det.getProbableCharsets(); 114 120 if ( charSets.length>0 ){ 121 this.charSet = charSets[0]; } 123 124 return (isAscii?1:0); 125 } 126 127 132 public void Notify(String charset) 133 { 134 136 141 } 142 143 } 144 | Popular Tags |