While browsing our sources, I came across this snippet from o.a.c..components.pipeline.AbstractProcessingPipeline:
// execute the pipeline: this.generator.generate(); byte[] data = os.toByteArray(); environment.setContentLength(data.length); environment.getOutputStream(0).write(data);
But from the javadocs of java.io.ByteArrayOutputStream#toByteArray, I read:
"Creates a newly allocated byte array. Its size is the current size of this output stream and the valid contents of the buffer have been copied into it."
Thus I wonder: why are we doing this copy instead of doing:
// execute the pipeline: this.generator.generate(); environment.setContentLength(os.size()); os.writeTo(environment.getOutputStream(0));
If I'm not mistaken, this would avoid allocating and copying an array as large as the serializer's output. Or am I missing something subtle here?
I guess you just found a more efficient way to achieve exactly the same thing ;-)
It seems to me we can safely avoid this array allocation.
Sylvain
-- Sylvain Wallez Anyware Technologies http://www.apache.org/~sylvain http://www.anyware-tech.com { XML, Java, Cocoon, OpenSource }*{ Training, Consulting, Projects }