Package org.junithelper.core.util

Source Code of org.junithelper.core.util.UniversalDetectorUtil

package org.junithelper.core.util;

import org.mozilla.universalchardet.UniversalDetector;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class UniversalDetectorUtil {

  private UniversalDetectorUtil() {
  }

  public static String getDetectedEncoding(InputStream is) throws IOException {
    UniversalDetector detector = new UniversalDetector(null);
    byte[] buf = new byte[4096];
    int nread;
    while ((nread = is.read(buf)) > 0 && !detector.isDone()) {
      detector.handleData(buf, 0, nread);
    }
    detector.dataEnd();
    return detector.getDetectedCharset();
  }

  public static String getDetectedEncoding(File file) throws IOException {
    if (file != null && file.isFile() && file.canRead()) {
      return getDetectedEncoding(new FileInputStream(file));
    }
    throw new IOException("Cannot read file - " + file.getAbsolutePath());
  }

}
TOP

Related Classes of org.junithelper.core.util.UniversalDetectorUtil

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.