mkodekar / guava-libraries

Automatically exported from code.google.com/p/guava-libraries
Apache License 2.0
0 stars 0 forks source link

Make ByteStreams.toByteArray(InputStream, int) public #1788

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I'd like to suggest making the

  ByteStreams.toByteArray(InputStream in, int expectedSize)

method "public" rather than having package visibility. I'm aware that limit() 
and the single-argument toByteArray() methods could be combined to achieve a 
similar effect, but that would incur an extra array copy since 
toByteArray(InputStream in) uses ByteArrayOutputStream's toByteArray() method.

Original issue reported on code.google.com by bardur.a...@gmail.com on 22 Jun 2014 at 10:00

GoogleCodeExporter commented 9 years ago
Out of curiosity, what are you interested in using this method for? It's 
primarily designed around the use case of files, where the actual number of 
bytes in the stream could differ from what you get when you check the length of 
the file beforehand (but in most cases won't). It handles the stream having 
both more and fewer bytes than the expected size gracefully... for other use 
cases, it's possible that's not the best behavior.

Note that using limit() with toByteArray() does not have the same result: if 
there are more than the expected number of bytes in the stream when using 
limit, it will read at most the expected number of bytes, where this method 
will read all bytes regardless.

Original comment by cgdecker@google.com on 23 Jun 2014 at 4:21

GoogleCodeExporter commented 9 years ago
Dang it, you're right -- I misread the code. However, I could still use it in 
combination with ByteStreams.limit -- that would get me the behavior I would 
like, i.e. avoiding the extra copy of ByteStreams.toArray(InputStream). That 
would probably be too contrived as an API, though.

Thinking a bit more about it, I think I'll probably be better off just using a 
customized ByteArrayOutputStream (to gain access to its internal buffer) and 
using ByteStreams.copy().

Feel free to close this.

Original comment by bardur.a...@gmail.com on 23 Jun 2014 at 4:57

GoogleCodeExporter commented 9 years ago
This issue has been migrated to GitHub.

It can be found at https://github.com/google/guava/issues/<issue id>

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:08

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 1 Nov 2014 at 4:17

GoogleCodeExporter commented 9 years ago

Original comment by cgdecker@google.com on 3 Nov 2014 at 9:07