2

I have a formatted ebcdic byte[] which i have created using custom code. Now I want to write to file. I am able to do this using Java. I was trying to check if i can do this using a Spring writer. I tried using FlatFileItemWriter . but just wrote a set of objects to file. Also the problem is that the Item writer also introduces a new line after each item. Ebcdic is however is a single stream of bytes for the entire file. ItemWriter ebcdicByteWriter;

@Override
public void write(List<? extends AccountInvoice> items) throws Exception {
    ArrayList<byte[]> finalList= new ArrayList<byte[]>();

    for (AccountInvoice item : items) {  
        finalList.add(item.getEbcdicBytes()); 
    } 

    ebcdicByteWriter.write(finalList); 

} 

1 Answers1

2

FlatFileItemWriter has a hook to change the line separator: setLineSeparator(java.lang.String lineSeparator). Why not just pass it an empty String so the lines concatenate?

EDIT: Use a line aggregator to convert your byte array to an EBCDIC String.

public class BytesAggregator<byte[]> implements LineAggregator {
    @Override
    public String aggregate(byte[] item) {
        return new String(bytes, "Cp1047");
    }
} 
Dean Clark
  • 3,770
  • 1
  • 11
  • 26
  • Thanks Dean! now I have all of the data in a single line. LineSeparator. However I noticed that the FlatFileItemWriter always writes a String. which is causing the object string of byte[] is being written to file. Eg. [B@59ddcf86[B@42284509 instead of actual bytes – Merwin Pinto Jul 01 '16 at 23:07
  • Do you know of any other Item writer that would just bytes out to file. – Merwin Pinto Jul 01 '16 at 23:08
  • You don't need any other writer. Just write an aggregator like the one above – Dean Clark Jul 02 '16 at 12:48
  • Dean, I was looking to write the bytes as is to the file and not convert it to string. It will be non human readable. And can only be decoded using a tool (EBCDIC Hex reader). Hence I was looking for writer that simply writes outs the bytes to file without any conversion. – Merwin Pinto Jul 05 '16 at 15:17
  • Strings don't have to be human readable and can use _any_ encoding. The EBCDIC encoding is is `Cp1047`. I've used `FlatFileItemReader` to read and `FlatFileItemWriter` to read EBCDIC files in the past with no issues, and I would not recommend creating a custom writer when the out-of-box ones will work perfectly well. You can check the FlatFileItemWriter javadoc to confirm how to set the output encoding, but it will work. – Dean Clark Jul 06 '16 at 17:33