public static String getString(File file, String encoding) throws IOException { StringWriter sw = new StringWriter(); FileInputStream fis = new FileInputStream(file); logger.debug("Reading file: " + file + " using encoding: " + encoding); encoding = validateEncoding(encoding); IOUtils.copy(fis, sw, encoding); return sw.toString(); } }
public static InputStreamReader getInputStreamReader(InputStream is, String encoding) throws IOException { logger.debug("Reading stream: using encoding: " + encoding); encoding = validateEncoding(encoding); BOMInputStream bis = new BOMInputStream(is); //So that we can remove the BOM return new InputStreamReader(bis, encoding); }
public static String detect(File file) { try { FileInputStream fis = new FileInputStream(file); String encoding = EncodingDetector.detect(fis); logger.debug("Detected encoding for file: " + file.getName() + ": " + encoding); if (encoding == null) { encoding = DEFAULT_ENCODING; } encoding = validateEncoding(encoding); return encoding; } catch(Exception e) { logger.debug("Exception detecting encoding, using default: " + DEFAULT_ENCODING); } return DEFAULT_ENCODING; }
public static String detect(InputStream is) throws IOException { byte[] buf = new byte[4096]; // (1) UniversalDetector detector = new UniversalDetector(null); // (2) int nread; while ((nread = is.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } // (3) detector.dataEnd(); // (4) String encoding = detector.getDetectedCharset(); if (encoding != null) { logger.debug("Detected encoding = " + encoding); } else { logger.debug("No encoding detected, using default: " + DEFAULT_ENCODING); encoding = DEFAULT_ENCODING; } encoding = validateEncoding(encoding); // (5) detector.reset(); return encoding; }