Combine 3 byte arrays

190 views Asked by At

I was trying to combine 3 byte arrays to form a single one to generate a report.

byte[] bArray=null;
ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

for(int i=0;i<3;i++)
{
//Other stuff
bArray = getTheReportContent(); //The return type of this method is List<byte[]> 
outputStream.write(bArray);
} 

byte bArrayCombined[] = outputStream.toByteArray( );  //Checked the count. bArrayCombined.length=sum of all the 3 bArray

response.setContentLength((int) bArrayCombined.length);
outStream.write(bArrayCombined, 0, bArrayCombined.length);
outStream.flush();

When I have written this in to the report, the content is not as expected. It shows only first bArray contents. Where I went wrong here.

EDIT:

The getTheReportContent do the following:

Exports the report using jasper. And returns byteArrList.

 List byteArrList = new ArrayList();
 --------
 exporterXLS.exportReport();
 byteArrList.add(outputStream.toByteArray());
1

There are 1 answers

2
Raaghu On

You are assigning List to byte[] -- which cannot happen. So I believe u might have not pasted the full code.

Tried your code, the resultant length is proper. You can check with the following class.So I think the problem might be there in the receiving part. Let me know

public static void main(String[] args) {
    byte[] bArray=null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );

    for(int i=0;i<3;i++)
    {
        try {
            //Other stuff
            bArray = getTheReportContent(); //The return type of this method is List<byte[]> 
            outputStream.write(bArray);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } 

    byte bArrayCombined[] = outputStream.toByteArray( );  //Checked the count. bArrayCombined.length=sum of all the 3 bArray
    System.out.println("Size is " + bArrayCombined.length);

    ByteArrayOutputStream outStream = new ByteArrayOutputStream( );
    outStream.write(bArrayCombined, 0, bArrayCombined.length);

    System.out.println("new outStream size is " + outStream.toByteArray().length);

}

private static byte[] getTheReportContent() {
    return "123".getBytes();
}