system.arraycopy not throwing exception but not giving desired result either

122 views Asked by At

I have 2 byte arrays. I am concatenating using system.arraycopy. It is not throwing exception but the resulting stream is displaying only 2nd array data

byte mainPdf[] = generatePDF(creditAppPDFurl, cifNumber,appRefId,pdfid1,appTransId);
byte supportingPdf[] = generateSupportingDocPDF();

byte[] destination = new byte[mainPdf.length + supportingPdf.length];
System.arraycopy(mainPdf, 0, destination, 0, mainPdf.length);
System.arraycopy(supportingPdf, 0, destination, mainPdf.length, supportingPdf.length);
pdfInputStreamData = new ByteArrayInputStream(destination);

pdfInputStreamData is displaying only supportingPdf data

2

There are 2 answers

0
assylias On

Your code is fine and the error is somewhere else. In particular, the original arrays probably don't contain the information you expect.

You can try this simple example to confirm that the array concatenation part of your code works:

public static void main(String[] args) throws Exception {
    byte mainPdf[] = {1, 2, 3};
    byte supportingPdf[] = {4, 5, 6};

    byte[] destination = new byte[mainPdf.length + supportingPdf.length];
    System.arraycopy(mainPdf, 0, destination, 0, mainPdf.length);
    System.arraycopy(supportingPdf, 0, destination, mainPdf.length, supportingPdf.length);

    System.out.println(Arrays.toString(destination));
}

prints [1, 2, 3, 4, 5, 6].

0
xyz On

For the same above mentioned coding lines, When I run

pdfInputStreamData = new ByteArrayInputStream(mainPdf);

it gives correct data for 1st byte[].

When I run

pdfInputStreamData = new ByteArrayInputStream(supportingPdf);

it gives correct data for 2nd byte[].

But the last line

byte[] destination = new byte[mainPdf.length + supportingPdf.length];
System.arraycopy(mainPdf, 0, destination, 0, mainPdf.length);
System.arraycopy(supportingPdf, 0, destination, mainPdf.length, supportingPdf.length);
pdfInputStreamData = new ByteArrayInputStream(destination);

is giving only supportingPdf data when printed.

I am not able to figure out yet what is the problem in this case