read stream to end for negative length in writeBinary(InputStream) - #894
read stream to end for negative length in writeBinary(InputStream)#894Sahana2524 wants to merge 1 commit into
Conversation
|
(Analysis by Claude) The underlying problem is real and worth fixing. A few things to address before this can go in. 1. Branch target: should this go to
|
…tream) Per the JsonGenerator contract a negative dataLength means "read to end of stream"; the XML backend passed it straight through to new byte[len] / a bounded read, escaping as NegativeArraySizeException or IndexOutOfBoundsException. writeStreamAsBinary() now streams until EOF for negative length (through the recycled base64 buffer instead of 3 bytes per read) and returns the byte count; the attribute and pretty-printer paths, which need a full buffer for Stax2 anyway, read to end via ByteArrayBuilder on the buffer recycler. Non-negative lengths behave as before.
e9b23fa to
ec2d1e0
Compare
|
Makes sense on all counts. Reworked it and retargeted this PR at
Nits: |
Raw exception from
writeBinarywhen stream length is unknownwriteBinary(Base64Variant, InputStream, int)treats a negativedataLengthas a real length, but per theJsonGeneratorcontract a negative value means "length unknown, read to the end of stream", and the JSON backend already handles it that way. The attribute path reachesnew byte[dataLength]and the element path a boundedread(..., dataLength), so the call leaves the generator as a rawNegativeArraySizeException/IndexOutOfBoundsException.Fix keeps streaming where the code already streams:
writeStreamAsBinary(...)now treatslen < 0as "until EOF" and returns the byte count, reading through the recycled base64 buffer (_ioContext.allocBase64Buffer()) instead of 3 bytes perread(). Only the attribute and pretty-printer paths, which need a full buffer for the Stax2 API anyway, read the stream to the end (viaByteArrayBuilderon the buffer recycler). Non-negative lengths keep prior behavior, andwriteBinary(..., -1)returns the actual number of bytes written.Targets
2.xper review, so it can be merged forward.