There is a huge file format ebcdic. Need to convert it to ANCII. File weighs from 250MB. If everything is counted, converted, divided into an array of strings, then it takes a lot of time, and most importantly, it takes a lot of memory (soooo much). It is necessary to implement a mechanism for fast reading and conversion, so that even less memory is spent on it.
Old algorithm that I do not recommend to use.
private static final char[] NON_PRINTABLE_EBCDIC_CHARS = new char[] { /*0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x11, 0x12, 0x13, 0x14, 0x21, 0x22, 0x23, 0x24 *//*, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, 0x7F, 0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F, 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0xA0*/ }; public String convert(String input) throws IOException { StringWriter writer = new StringWriter(); Reader reader = null; reader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(input)), ebcdicCharset)); int[] ebcdicInput = loadContent(reader); close(reader); convert(ebcdicInput, writer); return writer.toString(); } private int[] loadContent(Reader reader) throws IOException { int[] buffer = new int[INITIAL_BUFFER_SIZE]; int bufferIndex = 0; int bufferSize = buffer.length; int character; while ((character = reader.read()) != -1) { if (bufferIndex == bufferSize) { buffer = resizeArray(buffer, bufferSize + INITIAL_BUFFER_SIZE); bufferSize = buffer.length; } buffer[bufferIndex++] = character; } return resizeArray(buffer, bufferIndex); } final int[] resizeArray(int[] orignalArray, int newSize) { int[] resizedArray = new int[newSize]; for (int i = 0; i < newSize && i < orignalArray.length; i++) { resizedArray[i] = orignalArray[i]; } return resizedArray; } private void convert(int[] ebcdicInput, Writer convertedOutputWriter) throws IOException { int convertedChar; for (int index = 0; index < ebcdicInput.length; index++) { int character = ebcdicInput[index]; if (fixedLength != -1 && index > 0 && index % fixedLength == 0) { convertedOutputWriter.append((char) LF); } if (fixedLength == -1 && character == NEL) { convertedChar = LF; } else { convertedChar = replaceNonPrintableCharacterByWhitespace(character); } convertedOutputWriter.append((char) character); }