Entity.toBytes() re-setting writer and value

5 views
Skip to first unread message

Albert Latacz

unread,
Sep 13, 2017, 11:48:17 AM9/13/17
to utterlyidle
At the moment Entity.toBytes() re-sets entity value and writer

public byte[] toBytes() {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
writer.apply(outputStream);
byte[] bytes = outputStream.toByteArray();
value = bytes;
writer = Entities.writerFor(value);
return bytes;
}

Is that a bug or by design?

Alex Luker

unread,
Sep 13, 2017, 12:05:56 PM9/13/17
to utter...@googlegroups.com
It looks like it's a feature so you can continue to use the entity after calling it (e.g you could call getBytes again). Otherwise the entity couldn end up being backed by a stream that had already been read.

--
You received this message because you are subscribed to the Google Groups "utterlyidle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to utterlyidle+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Daniel Worthington-Bodart

unread,
Sep 13, 2017, 2:02:25 PM9/13/17
to utter...@googlegroups.com
Yes exactly, It's always felt a bit horrible mutating the entity but it was the only way I could think of to preserve toString especially when debugging stuff. It does mean it stops streaming at that point.

It came about as originally we didn't support streaming so things often ended up as bytes. If we want to fix it / break it, I think the main use is toString, so if that just did the headers only it might work better.

To unsubscribe from this group and stop receiving emails from it, send an email to utterlyidle...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "utterlyidle" group.
To unsubscribe from this group and stop receiving emails from it, send an email to utterlyidle...@googlegroups.com.

Albert Latacz

unread,
Sep 14, 2017, 4:08:09 AM9/14/17
to utterlyidle
That makes perfect sense now. Mutation seems bit poo but understandable here. Thanks guys.
Reply all
Reply to author
Forward
0 new messages