juniversalchardet

repository·main·Indexed 18 days ago

https://github.com/albfernandez/juniversalchardet

A Java port of Mozilla's 'universalchardet' library used for detecting the character encoding of text data. It provides tools for detecting encoding from byte streams or files via UniversalDetector and ReaderFactory, supporting a wide range of Chinese, Cyrillic, Greek, Hebrew, Japanese, Korean, and Unicode encodings.

Tokens
1.7K
Snippets
4
Records
6
Agent score
14%

What's inside juniversalchardet

  1. Install juniversalchardet via Maven or Gradle

    main

    To use juniversalchardet in your Java project, add the following dependency to your build configuration.

    Maven Add this to your pom.xml:

    Gradle Add this to your build.gradle:

    <!-- Maven -->
    <dependency>
    	<groupId>com.github.albfernandez</groupId>
    	<artifactId>juniversalchardet</artifactId>
    	<version>2.5.0</version>
    </dependency>
    
    <!-- Gradle -->
    implementation 'com.github.albfernandez:juniversalchardet:2.5.0'
  2. Detect encoding of a File (simple way)

    main

    For a quick way to detect the encoding of a specific java.io.File, use the static method UniversalDetector.detectCharset(File file). This method returns the encoding name as a String, or null if no encoding could be detected.

    import org.mozilla.universalchardet.UniversalDetector;
    
    public class TestDetectorFile {
    
    	public static void main (String[] args) throws java.io.IOException {
    		if (args.length != 1) {
    			System.err.println("Usage: java TestDetectorFile FILENAME");
    			System.exit(1);
    		}
    		java.io.File file = new java.io.File(args[0]);
    		String encoding = UniversalDetector.detectCharset(file);
    		if (encoding != null) {
    			System.out.println("Detected encoding = " + encoding);
    		} else {
    			System.out.println("No encoding detected.");
    		}
    	}
    }
  3. Detect encoding using UniversalDetector

    main

    To detect the encoding of a byte stream, follow these steps using org.mozilla.universalchardet.UniversalDetector:

    1. Instantiate: Create a new UniversalDetector.
    2. Feed Data: Call handleData(byte[] buf, int offset, int length) repeatedly with chunks of data (typically a few thousand bytes).
    3. Signal End: Call dataEnd() once all data has been processed.
    4. Retrieve Result: Call getDetectedCharset() to get the name of the detected encoding as a String.
    5. Reset: Call reset() if you intend to reuse the same detector instance for a different detection task.

    Note: You can check isDone() during the data feeding loop to stop early if the detector has already reached a confident conclusion.

    import org.mozilla.universalchardet.UniversalDetector;
    
    public class TestDetector
    {
      public static void main(String[] args)
      {
        byte[] buf = new byte[4096];
        java.io.InputStream fis = java.nio.file.Files.newInputStream(java.nio.file.Paths.get("test.txt"));
    
        // (1) Construct instance
        UniversalDetector detector = new UniversalDetector();
    
        // (2) Feed data
        int nread;
        while ((nread = fis.read(buf)) > 0 && !detector.isDone()) {
          detector.handleData(buf, 0, nread);
        }
        // (3) Notify end of data
        detector.dataEnd();
    
        // (4) Get detected encoding
        String encoding = detector.getDetectedCharset();
        if (encoding != null) {
          System.out.println("Detected encoding = " + encoding);
        } else {
          System.out.println("No encoding detected.");
        }
    
        // (5) Reset for reuse
        detector.reset();
      }
    }
  4. Create a BufferedReader with correct encoding

    main

    Use org.mozilla.universalchardet.ReaderFactory to automatically detect a file's encoding and return a java.io.BufferedReader configured with that encoding. This abstracts away the manual detection and reader creation steps.

    import org.mozilla.universalchardet.ReaderFactory;
    
    public class TestCreateReaderFromFile {
    	
    	public static void main (String[] args) throws java.io.IOException {
    		if (args.length != 1) {
    			System.err.println("Usage: java TestCreateReaderFromFile FILENAME");
    			System.exit(1);
    		}
    	
    		java.io.Reader reader = null;
    		try {
    			java.io.File file = new java.io.File(args[0]);
    			reader = ReaderFactory.createBufferedReader(file);
    			
    			// Do whatever you want with the reader
    		} 
    		finally {
    			if (reader != null) {
    				reader.close();
    			}
    		}
    	}
    }
  5. Compatibility and Requirements

    main

    Ensure your environment meets the following requirements based on the version of juniversalchardet you are using:

    Java (JDK)

    • 2.x: Requires JDK 7 or higher.
    • 3.x: Requires JDK 11 or higher.

    Android

    • 2.0.x: Requires Android 4 (API Level 14).
    • 2.1.x and newer: Requires Android 8 (API Level 26).
    • 3.0.x and newer: Requires Android 12 (API Level 32).
  6. Supported encodings in juniversalchardet

    main

    The library supports a wide range of encodings across several language groups. A full list of supported encodings is available in org.mozilla.universalchardet.Constants.

    Supported Groups:

    • Chinese: ISO-2022-CN, BIG-5, EUC-TW, HZ-GB-2312, GB-18030
    • Cyrillic: ISO-8859-5, KOI8-R, WINDOWS-1251, MACCYRILLIC, IBM866, IBM855
    • Greek: ISO-8859-7, WINDOWS-1253
    • Hebrew: ISO-8859-8, WINDOWS-1255
    • Japanese: ISO-2022-JP, Shift_JIS, EUC-JP
    • Korean: ISO-2022-KR, EUC-KR
    • Unicode: UTF-8, UTF-16BE/LE, UTF-32BE/LE, X-ISO-10646-UCS-4-3412, X-ISO-10646-UCS-4-2143
    • Others: WINDOWS-1252, US-ASCII