Skip to content

Commit cbc35d3

Browse files
Merge pull request #18830 from Sourov72/java_push
Code samples of Java
2 parents 94c1318 + 94c901d commit cbc35d3

File tree

5 files changed

+192
-0
lines changed

5 files changed

+192
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
3+
import java.nio.charset.Charset;
4+
5+
public class EbcdicExample {
6+
private static final Logger logger = LoggerFactory.getLogger(EbcdicExample.class);
7+
8+
public static void main(String[] args) {
9+
// Example: EBCDIC bytes for "ABC" (in Cp037)
10+
byte[] ebcdicBytes = new byte[] { (byte)0xC1, (byte)0xC2, (byte)0xC3 };
11+
12+
// Convert to String using EBCDIC Cp037 charset
13+
String text = new String(ebcdicBytes, Charset.forName("Cp037"));
14+
logger.info(text);
15+
}
16+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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 StreamingConverter {
8+
private static final Logger logger = LoggerFactory.getLogger(StreamingConverter.class);
9+
10+
public static void main(String[] args) {
11+
try (
12+
InputStreamReader reader = new InputStreamReader(
13+
new FileInputStream("input.ebc"),
14+
Charset.forName("Cp037")
15+
);
16+
OutputStreamWriter writer = new OutputStreamWriter(
17+
new FileOutputStream("output.txt"),
18+
StandardCharsets.US_ASCII
19+
)
20+
) {
21+
char[] buffer = new char[1024];
22+
int length;
23+
24+
while ((length = reader.read(buffer)) != -1) {
25+
writer.write(buffer, 0, length);
26+
}
27+
28+
logger.info("Conversion complete! See output.txt");
29+
30+
} catch (IOException e) {
31+
logger.error("Error during conversion", e);
32+
}
33+
}
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
3+
import java.io.FileInputStream;
4+
import java.nio.charset.Charset;
5+
import java.nio.charset.StandardCharsets;
6+
7+
public class FileConverter {
8+
private static final Logger logger = LoggerFactory.getLogger(FileConverter.class);
9+
10+
public static void main(String[] args) throws Exception {
11+
// Step 1: Read raw EBCDIC bytes from file
12+
FileInputStream fis = new FileInputStream("input.ebc");
13+
byte[] ebcdicData = fis.readAllBytes();
14+
fis.close();
15+
16+
// Step 2: Decode EBCDIC bytes to Unicode string
17+
String unicodeText = new String(ebcdicData, Charset.forName("Cp037"));
18+
19+
// Step 3: Encode Unicode string to ASCII bytes
20+
byte[] asciiData = unicodeText.getBytes(StandardCharsets.US_ASCII);
21+
22+
// Step 4: Log final ASCII string
23+
logger.info(new String(asciiData, StandardCharsets.US_ASCII));
24+
}
25+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import org.slf4j.Logger;
2+
import org.slf4j.LoggerFactory;
3+
import java.nio.charset.Charset;
4+
import java.nio.charset.StandardCharsets;
5+
6+
public class EbcdicToAsciiConverter {
7+
private static final Logger logger = LoggerFactory.getLogger(EbcdicToAsciiConverter.class);
8+
9+
public static void main(String[] args) {
10+
// Step 0: Example EBCDIC bytes ("HELLO" in Cp037)
11+
byte[] ebcdicData = { (byte)0xC8, (byte)0x85, (byte)0x93, (byte)0x93, (byte)0x96 };
12+
// Step 1: Decode from EBCDIC (Cp037) to Unicode string
13+
String unicodeText = new String(ebcdicData, Charset.forName("Cp037"));
14+
// Step 2: Encode from Unicode string to ASCII bytes
15+
byte[] asciiData = unicodeText.getBytes(StandardCharsets.US_ASCII);
16+
// Step 3: Log final ASCII string
17+
logger.info(new String(asciiData, StandardCharsets.US_ASCII));
18+
}
19+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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

Comments
 (0)