/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ebcdicant;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;

/**
 *
 * @author Lisewski
 */
public class EbcdicAnt {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {

        Charset charsetEBCDIC = Charset.forName("Cp037");
        Charset charsetACSII = Charset.forName("US-ASCII");

        InputStream is = new FileInputStream("c:/dev/test.ipm");
        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
        String line = buf.readLine();
        StringBuilder sb = new StringBuilder();
        while (line != null) {
            System.out.println(line);
            System.out.println("String converted to ASCII: " + convertTO(line, charsetEBCDIC, charsetACSII));
            System.out.println("String converted to EBCDIC: " + convertTO(line, charsetACSII, charsetEBCDIC));
//            sb.append(line).append("\n");
            line = buf.readLine();
        }
        String ebcdic = sb.toString();

//        String ebcdic = "(((((((";
//        System.out.println("String EBCDIC: " + ebcdic);
//        System.out.println("String converted to ASCII: " + convertTO(ebcdic, charsetEBCDIC, charsetACSII));
//
//        String ascII = "MMMMMM";
//        System.out.println("String ASCII: " + ascII);
//        System.out.println("String converted to EBCDIC: " + convertTO(ascII, charsetACSII, charsetEBCDIC));
    }

    public static String convertTO(String dados, Charset encondingFrom, Charset encondingTo) {
        return new String(dados.getBytes(encondingFrom), encondingTo);
    }
}
