getByteBufferFromInputStream() can be very inefficient

Description

ICUBinary.getByteBufferFromInputStream() tries to read all bytes from an InputStream into a byte array. It reads chunks of is.available() bytes. This seems to work fine on a normal JDK, but some library implementations and InputStream instances return very pessimistic values from available(), maybe even always 1, as long as anything is available. This causes a huge number of reallocations in the current getByteBufferFromInputStream() code.

The recommendation seems to be to not use available() at all, but to call read() until it returns a negative value, something like

GoogleIssue:17975585

Activity

Show:
TracBot
June 30, 2018, 11:37 PM
Trac Comment 2 by —2014-10-16T19:13:49.107Z

PS: Apparently it is even permitted for available() to return 0 even when not at the end of the stream yet. In that case, the current code would read only part of the stream.

TracBot
June 30, 2018, 11:37 PM
Trac Comment 3 by —2014-10-20T17:13:53.518Z

Review link: http://codereview.appspot.com/155570043

Yoshito already said "I reviewed the changes and it looks great".

I have not yet submitted this change.

Fixed

Assignee

Markus Scherer

Reporter

Markus Scherer

Components

Labels

Reviewer

None

Priority

major

Time Needed

Hours

Fix versions