Need to Merge two ByteArrayOutputStream to generate single ByteArrayOutputStream

StackOverflow https://stackoverflow.com/questions/17564021

  •  02-06-2022
  •  | 
  •  

Вопрос

I need to merge two ByteArrayOutputStream and pass to xdo api TemplateHelper.processTemplate to generate report

Following code is written to get xml output in two ByteArrayOutputStreams -

ByteArrayOutputStream hdrclob = new ByteArrayOutputStream (1024);

hdrclob = (ByteArrayOutputStream)this.getDataTemplateXML(transaction,"ASO",
                      "ASOPD",parameters1,null);

ByteArrayOutputStream conclob = new ByteArrayOutputStream (1024);

ContractTermsXMLGenerator.writeXML(PrintQuote,(OutputStream) conclob, true,
            documentType, new Number(params[8]), new Number("0"));

Now passing hdrclob / conclob separately to xdo api then able to see respective xml output on separate reports like this -

TemplateHelper.processTemplate(((OADBTransactionImpl)transaction).getAppsContext(),
        "ASO", "SampleRTF", language, country,
         new ByteArrayInputStream(hdrclob.toByteArray()),
           TemplateHelper.OUTPUT_TYPE_PDF, new Properties(), pdf);    

Or

TemplateHelper.processTemplate(((OADBTransactionImpl)transaction).getAppsContext(),
         "ASO", "SampleRTF", language, country, 
         new ByteArrayInputStream(conclob.toByteArray()),
           TemplateHelper.OUTPUT_TYPE_PDF, new Properties(), pdf);  

But need to merge both hdrclob and conclob to generate single ByteArrayOutputStream and pass to xdo api to get single report containing both xml outputs

Please tell how to merge two ByteArrayOutputStreams

thanks for replying on this

Это было полезно?

Решение

Assuming that this is Java, just write one stream to the end of the other.

hdrclob.write(conclob.toByteArray());
// hdrclob.toByteArray() now returns the concatenation of the two streams

If you just want to read them sequentially as a single InputStream, you can construct a SequenceInputStream that concatenates any two input streams together.

InputStream everything = new SequenceInputStream(
    new ByteArrayInputStream(hdrclob.toByteArray()),
    new ByteArrayInputStream(conclob.toByteArray()));
// now read everything
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top