|
| 1 | +import org.slf4j.Logger; |
| 2 | +import org.slf4j.LoggerFactory; |
| 3 | +import java.io.*; |
| 4 | +import java.nio.charset.Charset; |
| 5 | +import java.nio.charset.StandardCharsets; |
| 6 | + |
| 7 | +public class EBCDICConversionTest { |
| 8 | + private static final Logger logger = LoggerFactory.getLogger(EBCDICConversionTest.class); |
| 9 | + |
| 10 | + public static void main(String[] args) throws Exception { |
| 11 | + testBasicEBCDICToAsciiExample(); |
| 12 | + testStepByStepConversion(); |
| 13 | + testFileBasedConversion(); |
| 14 | + testStreamingConversion(); |
| 15 | + logger.info("✅ All tests passed!"); |
| 16 | + } |
| 17 | + |
| 18 | + static void assertEquals(String expected, String actual) { |
| 19 | + if (!expected.equals(actual)) { |
| 20 | + throw new AssertionError("Expected: [" + expected + "], but got: [" + actual + "]"); |
| 21 | + } |
| 22 | + } |
| 23 | + |
| 24 | + static void testBasicEBCDICToAsciiExample() { |
| 25 | + // Example: EBCDIC bytes for "ABC" (in Cp037) |
| 26 | + byte[] ebcdicBytes = new byte[] { (byte) 0xC1, (byte) 0xC2, (byte) 0xC3 }; |
| 27 | + String text = new String(ebcdicBytes, Charset.forName("Cp037")); |
| 28 | + logger.info("Decoded text: {}", text); |
| 29 | + assertEquals("ABC", text); |
| 30 | + } |
| 31 | + |
| 32 | + static void testStepByStepConversion() { |
| 33 | + // Example EBCDIC bytes ("Hello" in Cp037) |
| 34 | + byte[] ebcdicData = { (byte) 0xC8, (byte) 0x85, (byte) 0x93, (byte) 0x93, (byte) 0x96 }; |
| 35 | + String unicodeText = new String(ebcdicData, Charset.forName("Cp037")); |
| 36 | + assertEquals("Hello", unicodeText); |
| 37 | + |
| 38 | + byte[] asciiData = unicodeText.getBytes(StandardCharsets.US_ASCII); |
| 39 | + assertEquals("Hello", new String(asciiData, StandardCharsets.US_ASCII)); |
| 40 | + logger.info("Step-by-step conversion OK: {}", new String(asciiData)); |
| 41 | + } |
| 42 | + |
| 43 | + static void testFileBasedConversion() throws Exception { |
| 44 | + File tempFile = File.createTempFile("input", ".ebc"); |
| 45 | + tempFile.deleteOnExit(); |
| 46 | + |
| 47 | + // "TEST" in Cp037 (all uppercase) |
| 48 | + byte[] ebcdicData = { (byte) 0xE3, (byte) 0xC5, (byte) 0xE2, (byte) 0xE3 }; |
| 49 | + try (FileOutputStream fos = new FileOutputStream(tempFile)) { |
| 50 | + fos.write(ebcdicData); |
| 51 | + } |
| 52 | + |
| 53 | + byte[] rawBytes = new FileInputStream(tempFile).readAllBytes(); |
| 54 | + String unicodeText = new String(rawBytes, Charset.forName("Cp037")); |
| 55 | + assertEquals("TEST", unicodeText); |
| 56 | + |
| 57 | + byte[] asciiData = unicodeText.getBytes(StandardCharsets.US_ASCII); |
| 58 | + assertEquals("TEST", new String(asciiData, StandardCharsets.US_ASCII)); |
| 59 | + logger.info("File-based conversion OK: {}", unicodeText); |
| 60 | + } |
| 61 | + |
| 62 | + static void testStreamingConversion() throws IOException { |
| 63 | + File input = File.createTempFile("input", ".ebc"); |
| 64 | + File output = File.createTempFile("output", ".txt"); |
| 65 | + input.deleteOnExit(); |
| 66 | + output.deleteOnExit(); |
| 67 | + |
| 68 | + // "JAVA" in Cp037 (all uppercase) |
| 69 | + byte[] ebcdicData = { (byte) 0xD1, (byte) 0xC1, (byte) 0xE5, (byte) 0xC1 }; |
| 70 | + try (FileOutputStream fos = new FileOutputStream(input)) { |
| 71 | + fos.write(ebcdicData); |
| 72 | + } |
| 73 | + |
| 74 | + try ( |
| 75 | + InputStreamReader reader = new InputStreamReader( |
| 76 | + new FileInputStream(input), |
| 77 | + Charset.forName("Cp037") |
| 78 | + ); |
| 79 | + OutputStreamWriter writer = new OutputStreamWriter( |
| 80 | + new FileOutputStream(output), |
| 81 | + StandardCharsets.US_ASCII |
| 82 | + ) |
| 83 | + ) { |
| 84 | + char[] buffer = new char[1024]; |
| 85 | + int length; |
| 86 | + while ((length = reader.read(buffer)) != -1) { |
| 87 | + writer.write(buffer, 0, length); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + String result = new String( |
| 92 | + new FileInputStream(output).readAllBytes(), |
| 93 | + StandardCharsets.US_ASCII |
| 94 | + ); |
| 95 | + assertEquals("JAVA", result); |
| 96 | + logger.info("Streaming conversion OK: {}", result); |
| 97 | + } |
| 98 | +} |
0 commit comments