### Installation - Gradle Dependency Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Add JUniversalChardet to your Gradle project for encoding detection. ```APIDOC ## Gradle Dependency Add JUniversalChardet to your Gradle project for encoding detection. ```groovy implementation 'com.github.albfernandez:juniversalchardet:2.5.0' ``` ``` -------------------------------- ### Installation - Maven Dependency Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Add JUniversalChardet to your project via Maven for automatic encoding detection capabilities. ```APIDOC ## Maven Dependency Add JUniversalChardet to your project via Maven for automatic encoding detection capabilities. ```xml com.github.albfernandez juniversalchardet 2.5.0 ``` ``` -------------------------------- ### Accessing Supported Charset Constants Source: https://context7.com/albfernandez/juniversalchardet/llms.txt This example demonstrates how to access the predefined constants for supported charset names provided by the Constants class in JUniversalChardet. These constants are useful for referencing character encodings programmatically. ```java import org.mozilla.universalchardet.Constants; public class SupportedCharsets { public static void main(String[] args) { // Chinese encodings System.out.println("Chinese: " + Constants.CHARSET_BIG5); System.out.println("Chinese: " + Constants.CHARSET_GB18030); System.out.println("Chinese: " + Constants.CHARSET_ISO_2022_CN); System.out.println("Chinese: " + Constants.CHARSET_HZ_GB_2312); // Japanese encodings System.out.println("Japanese: " + Constants.CHARSET_SHIFT_JIS); System.out.println("Japanese: " + Constants.CHARSET_EUC_JP); System.out.println("Japanese: " + Constants.CHARSET_ISO_2022_JP); // Korean encodings System.out.println("Korean: " + Constants.CHARSET_EUC_KR); System.out.println("Korean: " + Constants.CHARSET_ISO_2022_KR); // Unicode encodings System.out.println("Unicode: " + Constants.CHARSET_UTF_8); System.out.println("Unicode: " + Constants.CHARSET_UTF_16BE); System.out.println("Unicode: " + Constants.CHARSET_UTF_16LE); System.out.println("Unicode: " + Constants.CHARSET_UTF_32BE); System.out.println("Unicode: " + Constants.CHARSET_UTF_32LE); // Cyrillic encodings System.out.println("Cyrillic: " + Constants.CHARSET_KOI8_R); System.out.println("Cyrillic: " + Constants.CHARSET_WINDOWS_1251); System.out.println("Cyrillic: " + Constants.CHARSET_ISO_8859_5); System.out.println("Cyrillic: " + Constants.CHARSET_IBM866); // Western encodings System.out.println("Western: " + Constants.CHARSET_WINDOWS_1252); System.out.println("Western: " + Constants.CHARSET_US_ASCII); // Greek and Hebrew System.out.println("Greek: " + Constants.CHARSET_ISO_8859_7); System.out.println("Hebrew: " + Constants.CHARSET_ISO_8859_8); } } ``` -------------------------------- ### Set Version and Build for Release Source: https://github.com/albfernandez/juniversalchardet/blob/main/release-process.md Update the release version in pom.xml and README.md, then build and verify the release artifacts. This includes committing changes and tagging the release. ```bash # change release in pom.xml and README.md mvn clean package install verify -Pdeploy mvn clean package install verify deploy -Pdeploy git add -A git commit -S -m 'Release ' git tag -a -m "Tagging release <1.0.0>" git push git push --tags ``` -------------------------------- ### Prepare Next Iteration Source: https://github.com/albfernandez/juniversalchardet/blob/main/release-process.md Update the release version in pom.xml for the next development cycle and push the changes. This prepares the project for future development. ```bash # change release in pom.xml git add -A git commit -S -m 'Next release cycle' git push ``` -------------------------------- ### Build juniversalchardet from Sources Source: https://github.com/albfernandez/juniversalchardet/blob/main/README.md Follow these bash commands to clone the juniversalchardet repository and build the project from its source code using Maven. This is useful for development or if you need a specific version not available in repositories. ```bash git clone https://github.com/albfernandez/juniversalchardet.git cd juniversalchardet mvn clean package ``` -------------------------------- ### Check Android Compatibility (Main Branch) Source: https://github.com/albfernandez/juniversalchardet/blob/main/release-process.md Verify Android compatibility for the main branch using Gradle within a Dockerized Android SDK environment. This ensures the library functions correctly on Android. ```bash docker run -it mobiledevops/android-sdk-image:34.0.0-jdk17 /bin/bash git clone https://github.com/albfernandez/juniversalchardet.git cd juniversalchardet gradle check ``` -------------------------------- ### Verify Java 8 and Java 11 Builds Source: https://github.com/albfernandez/juniversalchardet/blob/main/release-process.md Ensure the project builds successfully with both Java 8 and Java 11. This step is crucial for maintaining compatibility across different Java versions. ```bash JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/ mvn clean package install verify ``` ```bash JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64/ mvn clean package install verify ``` -------------------------------- ### Check Android Compatibility (Older Branches) Source: https://github.com/albfernandez/juniversalchardet/blob/main/release-process.md Verify Android compatibility for older branches, such as 2.5.x, using Gradle within a Dockerized Android SDK environment. Note the removal of module-info.java for compatibility. ```bash docker run -it mobiledevops/android-sdk-image:34.0.0-jdk17 /bin/bash git clone https://github.com/albfernandez/juniversalchardet.git cd juniversalchardet git checkout 2.5.x rm src/main/java/module-info.java gradle check ``` -------------------------------- ### UniversalDetector with CharsetListener Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Demonstrates how to use the UniversalDetector with a CharsetListener for event-driven encoding detection. ```APIDOC ## UniversalDetector.handleData ### Description Processes byte data to detect character encoding, triggering a callback when detection is complete. ### Parameters #### Request Body - **buf** (byte[]) - Required - Buffer containing the data to analyze. - **offset** (int) - Required - The start offset in the buffer. - **len** (int) - Required - The number of bytes to read. ### Request Example ```java detector.handleData(buf, 0, nread); ``` ``` -------------------------------- ### UniversalDetector - Manual Detection Flow Source: https://context7.com/albfernandez/juniversalchardet/llms.txt For advanced use cases, create a detector instance and feed data incrementally. This allows processing large files efficiently or streaming data. ```APIDOC ## UniversalDetector Manual Detection Flow For advanced use cases, create a detector instance and feed data incrementally. This allows processing large files efficiently or streaming data. ### Method N/A (Instance methods) ### Endpoint N/A ### Parameters #### Request Body (No explicit request body for this flow, data is fed via `handleData`) ### Request Example ```java import org.mozilla.universalchardet.UniversalDetector; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; public class ManualDetection { public static void main(String[] args) throws Exception { byte[] buf = new byte[4096]; try (InputStream fis = Files.newInputStream(Paths.get("large_file.txt"))) { // Step 1: Create detector instance UniversalDetector detector = new UniversalDetector(); // Step 2: Feed data until detection is complete or EOF int nread; while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } // Step 3: Signal end of data detector.dataEnd(); // Step 4: Get result String encoding = detector.getDetectedCharset(); if (encoding != null) { System.out.println("Detected encoding: " + encoding); } else { System.out.println("No encoding detected"); } // Step 5: Reset for reuse detector.reset(); } } } ``` ### Response #### Success Response (200) - **encoding** (string) - The detected charset name or null if encoding cannot be determined. ``` -------------------------------- ### Process files with encoding detection in Java Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Uses UniversalDetector to identify file encoding and ReaderFactory to create a buffered reader for content processing. ```java import org.mozilla.universalchardet.UniversalDetector; import org.mozilla.universalchardet.ReaderFactory; import java.io.*; import java.nio.charset.Charset; import java.nio.file.*; import java.util.List; import java.util.ArrayList; public class CompleteFileProcessor { public static void main(String[] args) throws IOException { // Process multiple files with unknown encodings List files = Files.list(Paths.get("documents")) .filter(p -> p.toString().endsWith(".txt")) .toList(); for (Path filePath : files) { processFile(filePath.toFile()); } } public static void processFile(File file) throws IOException { // Step 1: Detect encoding String encoding = UniversalDetector.detectCharset(file); System.out.println("File: " + file.getName()); System.out.println(" Encoding: " + (encoding != null ? encoding : "unknown")); // Step 2: Create reader with detected encoding (UTF-8 fallback) try (BufferedReader reader = ReaderFactory.createBufferedReader( file, Charset.forName("UTF-8"))) { // Step 3: Process content int lineCount = 0; int charCount = 0; String line; while ((line = reader.readLine()) != null) { lineCount++; charCount += line.length(); // Process each line... if (lineCount <= 3) { System.out.println(" Line " + lineCount + ": " + (line.length() > 50 ? line.substring(0, 50) + "..." : line)); } } System.out.println(" Total lines: " + lineCount); System.out.println(" Total characters: " + charCount); } System.out.println(); } } ``` -------------------------------- ### Detect Encoding of a File (Simple Way) Source: https://github.com/albfernandez/juniversalchardet/blob/main/README.md This method provides a straightforward way to detect the character encoding of a given file using a static utility method. Ensure the file path is provided as a command-line argument. ```java 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."); } } } ``` -------------------------------- ### Create BufferedReader with Detected Encoding Source: https://github.com/albfernandez/juniversalchardet/blob/main/README.md This snippet demonstrates how to create a BufferedReader for a file, automatically detecting its encoding using ReaderFactory. This is useful for reading text files with unknown encodings. ```java 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(); } } } } ``` -------------------------------- ### Detect Charset from File Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Use this static convenience method to detect the encoding of a file in a single call. It returns the detected charset name or null if detection fails. ```java import org.mozilla.universalchardet.UniversalDetector; import java.io.File; import java.io.IOException; public class SimpleDetection { public static void main(String[] args) throws IOException { File file = new File("document.txt"); // Simple one-line detection String encoding = UniversalDetector.detectCharset(file); if (encoding != null) { System.out.println("Detected encoding: " + encoding); // Output: Detected encoding: UTF-8 } else { System.out.println("Unable to detect encoding"); } } } ``` -------------------------------- ### UnicodeBOMInputStream with BOM Preservation Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Use UnicodeBOMInputStream with skipIfFound set to false to create a BOM-aware stream that preserves the BOM. The BOM must be manually skipped using skipBOM() when needed. This is useful when the BOM needs to be processed or maintained separately. ```java import org.mozilla.universalchardet.UnicodeBOMInputStream; import org.mozilla.universalchardet.UnicodeBOMInputStream.BOM; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class BOMPreservation { public static void main(String[] args) throws IOException { // UTF-16 LE with BOM (FF FE) byte[] utf16leWithBom = new byte[] { (byte)0xFF, (byte)0xFE, // BOM 'A', 0x00, 'B', 0x00 // "AB" in UTF-16LE }; ByteArrayInputStream source = new ByteArrayInputStream(utf16leWithBom); // Create stream but don't skip BOM (skipIfFound = false) UnicodeBOMInputStream bomStream = new UnicodeBOMInputStream(source, false); BOM detectedBom = bomStream.getBOM(); System.out.println("BOM type: " + detectedBom); System.out.println("BOM bytes: " + bytesToHex(detectedBom.getBytes())); // Output: BOM type: UTF-16 little-endian // Output: BOM bytes: FF FE // Manually skip BOM when ready bomStream.skipBOM(); // Read remaining content ByteArrayOutputStream buffer = new ByteArrayOutputStream(); int b; while ((b = bomStream.read()) != -1) { buffer.write(b); } bomStream.close(); System.out.println("Content bytes: " + bytesToHex(buffer.toByteArray())); // Output: Content bytes: 41 00 42 00 } private static String bytesToHex(byte[] bytes) { StringBuilder sb = new StringBuilder(); for (byte b : bytes) { sb.append(String.format("%02X ", b)); } return sb.toString().trim(); } } ``` -------------------------------- ### Gradle Dependency for juniversalchardet Source: https://github.com/albfernandez/juniversalchardet/blob/main/README.md Include this line in your build.gradle file to add the juniversalchardet library as a dependency in your Gradle project. Check for the latest version available. ```groovy implementation 'com.github.albfernandez:juniversalchardet:2.5.0' ``` -------------------------------- ### Detect encoding with UniversalDetector and CharsetListener Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Register a CharsetListener to receive callbacks when encoding detection completes. Useful for event-driven architectures. ```java import org.mozilla.universalchardet.UniversalDetector; import org.mozilla.universalchardet.CharsetListener; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; public class ListenerDetection { public static void main(String[] args) throws Exception { // Create listener for charset detection events CharsetListener listener = charset -> { System.out.println("Charset detected callback: " + charset); // Perform actions when charset is detected }; // Create detector with listener UniversalDetector detector = new UniversalDetector(listener); byte[] buf = new byte[4096]; try (InputStream fis = Files.newInputStream(Paths.get("data.txt"))) { int nread; while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } detector.dataEnd(); // Listener is automatically called when charset is determined } // Can also get encoding directly String encoding = detector.getDetectedCharset(); System.out.println("Final result: " + encoding); } } ``` -------------------------------- ### Create BufferedReader from byte array Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Create a BufferedReader directly from a byte array with automatic encoding detection. ```java import org.mozilla.universalchardet.ReaderFactory; import java.io.BufferedReader; import java.io.IOException; import java.nio.charset.StandardCharsets; public class ByteArrayReader { public static void main(String[] args) throws IOException { // Simulate receiving data from network or database byte[] data = "Привет мир! Hello World! 你好世界!".getBytes(StandardCharsets.UTF_8); try (BufferedReader reader = ReaderFactory.createBufferedReader(data)) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); // Output: Привет мир! Hello World! 你好世界! } } } } ``` -------------------------------- ### Create BufferedReader with automatic encoding detection Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Use ReaderFactory to create a BufferedReader that automatically detects file encoding, falling back to the system default. ```java import org.mozilla.universalchardet.ReaderFactory; import java.io.BufferedReader; import java.io.File; import java.io.IOException; public class AutoEncodingReader { public static void main(String[] args) throws IOException { File file = new File("international_text.txt"); // Reader automatically detects encoding try (BufferedReader reader = ReaderFactory.createBufferedReader(file)) { String line; while ((line = reader.readLine()) != null) { System.out.println(line); } } } } ``` -------------------------------- ### UniversalDetector - Detect Charset from File Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Static convenience method that detects the encoding of a file in a single call. Returns the detected charset name or null if encoding cannot be determined. ```APIDOC ## UniversalDetector.detectCharset(File file) Static convenience method that detects the encoding of a file in a single call. Returns the detected charset name or null if encoding cannot be determined. ### Method GET ### Endpoint N/A (Static method) ### Request Example ```java import org.mozilla.universalchardet.UniversalDetector; import java.io.File; import java.io.IOException; public class SimpleDetection { public static void main(String[] args) throws IOException { File file = new File("document.txt"); // Simple one-line detection String encoding = UniversalDetector.detectCharset(file); if (encoding != null) { System.out.println("Detected encoding: " + encoding); // Output: Detected encoding: UTF-8 } else { System.out.println("Unable to detect encoding"); } } } ``` ### Response #### Success Response (200) - **encoding** (string) - The detected charset name or null if encoding cannot be determined. ``` -------------------------------- ### Manual Encoding Detection Flow Source: https://context7.com/albfernandez/juniversalchardet/llms.txt For advanced use cases like processing large files or streaming data, create a detector instance and feed data incrementally. This allows for efficient processing and reuse of the detector. ```java import org.mozilla.universalchardet.UniversalDetector; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Paths; public class ManualDetection { public static void main(String[] args) throws Exception { byte[] buf = new byte[4096]; try (InputStream fis = Files.newInputStream(Paths.get("large_file.txt"))) { // Step 1: Create detector instance UniversalDetector detector = new UniversalDetector(); // Step 2: Feed data until detection is complete or EOF int nread; while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } // Step 3: Signal end of data detector.dataEnd(); // Step 4: Get result String encoding = detector.getDetectedCharset(); if (encoding != null) { System.out.println("Detected encoding: " + encoding); } else { System.out.println("No encoding detected"); } // Step 5: Reset for reuse detector.reset(); } } } ``` -------------------------------- ### Create BufferedReader with fallback charset Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Specify a fallback Charset to use if automatic detection fails. ```java import org.mozilla.universalchardet.ReaderFactory; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; public class ReaderWithFallback { public static void main(String[] args) throws IOException { File file = new File("unknown_encoding.txt"); // Use UTF-8 as fallback if detection fails Charset fallback = StandardCharsets.UTF_8; try (BufferedReader reader = ReaderFactory.createBufferedReader(file, fallback)) { StringBuilder content = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { content.append(line).append("\n"); } System.out.println(content.toString()); } } } ``` -------------------------------- ### ReaderFactory.createBufferedReader Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Provides methods to create a BufferedReader that automatically detects the file or byte array encoding. ```APIDOC ## ReaderFactory.createBufferedReader ### Description Creates a BufferedReader that automatically detects and uses the correct encoding for the input source. ### Parameters #### Request Body - **file** (File) - Required - The file to read. - **data** (byte[]) - Required - The byte array to read. - **defaultCharset** (Charset) - Optional - Fallback charset to use if detection fails. ### Request Example ```java BufferedReader reader = ReaderFactory.createBufferedReader(file); BufferedReader reader = ReaderFactory.createBufferedReader(file, StandardCharsets.UTF_8); BufferedReader reader = ReaderFactory.createBufferedReader(data); ``` ``` -------------------------------- ### Handle Unicode BOM with UnicodeBOMInputStream Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Wraps an InputStream to detect and skip Unicode Byte Order Marks. Prevents BOM bytes from interfering with subsequent text processing. ```java import org.mozilla.universalchardet.UnicodeBOMInputStream; import org.mozilla.universalchardet.UnicodeBOMInputStream.BOM; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.BufferedReader; public class BOMHandling { public static void main(String[] args) throws IOException { // UTF-8 with BOM (EF BB BF) byte[] utf8WithBom = new byte[] { (byte)0xEF, (byte)0xBB, (byte)0xBF, // BOM 'H', 'e', 'l', 'l', 'o' // Content }; ByteArrayInputStream source = new ByteArrayInputStream(utf8WithBom); // Wrap to handle BOM (automatically skips BOM by default) UnicodeBOMInputStream bomStream = new UnicodeBOMInputStream(source); // Check what BOM was detected BOM detectedBom = bomStream.getBOM(); System.out.println("BOM detected: " + detectedBom); // Output: BOM detected: UTF-8 // Read content without BOM bytes BufferedReader reader = new BufferedReader( new InputStreamReader(bomStream, "UTF-8") ); String content = reader.readLine(); System.out.println("Content: " + content); // Output: Content: Hello reader.close(); } } ``` -------------------------------- ### Maven Dependency for juniversalchardet Source: https://github.com/albfernandez/juniversalchardet/blob/main/README.md Add this XML snippet to your pom.xml file to include the juniversalchardet library in your Maven project. Ensure you use the latest compatible version. ```xml com.github.albfernandez juniversalchardet 2.5.0 ``` -------------------------------- ### UniversalDetector - Detect Charset from InputStream Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Detects the encoding of content from an InputStream. Useful for network streams, byte arrays, or any input source. ```APIDOC ## UniversalDetector.detectCharset(InputStream inputStream) Detects the encoding of content from an InputStream. Useful for network streams, byte arrays, or any input source. ### Method GET ### Endpoint N/A (Static method) ### Request Example ```java import org.mozilla.universalchardet.UniversalDetector; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.IOException; public class StreamDetection { public static void main(String[] args) throws IOException { // UTF-8 encoded Japanese text byte[] utf8Data = "こんにちは世界".getBytes("UTF-8"); try (InputStream is = new ByteArrayInputStream(utf8Data)) { String encoding = UniversalDetector.detectCharset(is); System.out.println("Detected: " + encoding); // Output: Detected: UTF-8 } // Shift_JIS encoded text byte[] sjisData = "日本語テキスト".getBytes("Shift_JIS"); try (InputStream is = new ByteArrayInputStream(sjisData)) { String encoding = UniversalDetector.detectCharset(is); System.out.println("Detected: " + encoding); // Output: Detected: SHIFT_JIS } } } ``` ### Response #### Success Response (200) - **encoding** (string) - The detected charset name or null if encoding cannot be determined. ``` -------------------------------- ### Detect Encoding from InputStream Source: https://github.com/albfernandez/juniversalchardet/blob/main/README.md Use this snippet to detect the character encoding of data read from an InputStream. It involves creating a UniversalDetector, handling data in chunks, signaling the end of data, and retrieving the detected charset. Remember to reset the detector for reuse. ```java 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) UniversalDetector detector = new UniversalDetector(); // (2) int nread; while ((nread = fis.read(buf)) > 0 && !detector.isDone()) { detector.handleData(buf, 0, nread); } // (3) detector.dataEnd(); // (4) String encoding = detector.getDetectedCharset(); if (encoding != null) { System.out.println("Detected encoding = " + encoding); } else { System.out.println("No encoding detected."); } // (5) detector.reset(); } } ``` -------------------------------- ### Maven Dependency for JUniversalChardet Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Add this Maven dependency to your project to include JUniversalChardet for automatic encoding detection. ```xml com.github.albfernandez juniversalchardet 2.5.0 ``` -------------------------------- ### Detect Encoding While Reading with EncodingDetectorInputStream Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Wraps an InputStream to identify character encoding automatically as data is consumed. Requires closing the stream to ensure detection is finalized. ```java import org.mozilla.universalchardet.EncodingDetectorInputStream; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class DetectWhileReading { public static void main(String[] args) throws IOException { // Simulate incoming data stream byte[] sourceData = "日本語テキストのサンプル".getBytes("UTF-8"); ByteArrayInputStream source = new ByteArrayInputStream(sourceData); // Wrap with encoding detector EncodingDetectorInputStream detectingStream = new EncodingDetectorInputStream(source); // Read all data (encoding detection happens automatically) ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] chunk = new byte[1024]; int bytesRead; while ((bytesRead = detectingStream.read(chunk)) != -1) { buffer.write(chunk, 0, bytesRead); } detectingStream.close(); // Get detected encoding after reading String encoding = detectingStream.getDetectedCharset(); System.out.println("Detected encoding: " + encoding); // Output: Detected encoding: UTF-8 // Convert bytes to string with detected encoding String text = buffer.toString(encoding); System.out.println("Content: " + text); } } ``` -------------------------------- ### Detect Charset from InputStream Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Detects the encoding of content from an InputStream, suitable for network streams, byte arrays, or other input sources. Handles different encodings like UTF-8 and Shift_JIS. ```java import org.mozilla.universalchardet.UniversalDetector; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.IOException; public class StreamDetection { public static void main(String[] args) throws IOException { // UTF-8 encoded Japanese text byte[] utf8Data = "こんにちは世界".getBytes("UTF-8"); try (InputStream is = new ByteArrayInputStream(utf8Data)) { String encoding = UniversalDetector.detectCharset(is); System.out.println("Detected: " + encoding); // Output: Detected: UTF-8 } // Shift_JIS encoded text byte[] sjisData = "日本語テキスト".getBytes("Shift_JIS"); try (InputStream is = new ByteArrayInputStream(sjisData)) { String encoding = UniversalDetector.detectCharset(is); System.out.println("Detected: " + encoding); // Output: Detected: SHIFT_JIS } } } ``` -------------------------------- ### Detect Encoding While Writing with EncodingDetectorOutputStream Source: https://context7.com/albfernandez/juniversalchardet/llms.txt Wraps an OutputStream to identify character encoding as data is passed through. Useful for monitoring encoding during write operations. ```java import org.mozilla.universalchardet.EncodingDetectorOutputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; public class DetectWhileWriting { public static void main(String[] args) throws IOException { ByteArrayOutputStream destination = new ByteArrayOutputStream(); // Wrap with encoding detector EncodingDetectorOutputStream detectingStream = new EncodingDetectorOutputStream(destination); // Write data (simulating copy operation) byte[] chineseText = "中文文本示例".getBytes("GB18030"); detectingStream.write(chineseText); detectingStream.close(); // Get detected encoding after writing String encoding = detectingStream.getDetectedCharset(); System.out.println("Detected encoding: " + encoding); // Output: Detected encoding: GB18030 } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.