Reputation: 172
Is it possible to concatenate two OutputStreams (of the same type, stored as OutputStreams) without converting either to a string? If so, how?
Upvotes: 2
Views: 4416
Reputation: 9
A short example:
private void test(Document xmlDoc) throws Exception {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
String s1 = "header";
outputStream.write(s1.getBytes());
ByteArrayOutputStream bodySubTree = (ByteArrayOutputStream) xmlToOutStream(xmlDoc);
outputStream.write(bodySubTree.toByteArray());
String s2 = "footer";
outputStream.write(s2.getBytes());
}
Upvotes: 0
Reputation: 4824
So, If you have OutputStream A, and OutputStream B, and you want to concatenate them so that you end up with the stuff from A, followed by the stuff from B, you could convert B into an InputStream (a task that has likely been explained over 9000 times in this forum), and then Read data from this new InputStream, and write it to A. There: A generic answer for a generic question. Good luck!
Upvotes: 1