Closed GoogleCodeExporter closed 8 years ago
This is a bit of a gotcha. The no arg constructors don't allocate because they
are for using a buffer that comes from somewhere else or for setting the buffer
size later. As the javaodcs say, setBuffer must be called before the
Output/Input can be used. This is true even if using a stream with the buffer.
Original comment by nathan.s...@gmail.com
on 28 Apr 2013 at 8:59
But... If I use the Output(OutputStream) constructor, I don't have to set any
buffer, as it is automatically created internally.
The issue here is that the API doesn't seem consistent. If I use the
OutputStream constructor, I don't have to care about buffer sizes at all:
Output out = new Output(new ByteArrayOutputStream());
//(...), later:
out.setOutputstream(new ByteArrayOutputStream());
But if I use the default constructor, I do:
Output out = new Output();
out.setBuffer(new byte[someInt], someInt);
out.setOutputStream(new ByteArrayOutputStream());
I'm (conceptually) writing to a stream, not a byte[]. The Output seems to be
leaking implementation details when making me set a byte[] when I want to use
an OutputStream.
When you use the with-OutputStream-constructor, you don't have to care about
the underlying byte[] of the Output, which is appropriate.
Original comment by Pitxy...@gmail.com
on 28 Apr 2013 at 9:33
The byte[] is not an implementation detail. It is needed even when using an
OutputStream and can be sized appropriately (eg, small if the total bytes read
from the OutputStream are likely to be small).
The issue is that the no arg constructor is the only one that constructs an
Output that is not ready for use. This constructor is intended for when the
Output does not need its own buffer because it will be writing to a buffer
provided later.
setOutputStream could allocate a buffer if one was not set, but this side
effect seems unintuitive. Of course having an Output that can't be used until a
buffer is set is also unintuitive, but at least it forces you to be explicit
about allocations.
Original comment by n...@n4te.com
on 29 Apr 2013 at 11:31
"setOutputStream could allocate a buffer if one was not set, but this side
effect seems unintuitive"
I don't agree. To me, it would seem as natural as the current effects of doing:
Output out = new Output(new ByteArrayOutputStream());
Original comment by Pitxy...@gmail.com
on 18 May 2013 at 3:57
The difference with new Output(stream) is allocation is expected in a
constructor. I don't think either solution is perfect, but I prefer not
allocating as a setter side effect.
Original comment by nathan.s...@gmail.com
on 5 Aug 2013 at 1:11
Original issue reported on code.google.com by
Pitxy...@gmail.com
on 28 Apr 2013 at 8:40