From eee3938658a0a928da4f3e4b782cdd98e1ab31d6 Mon Sep 17 00:00:00 2001 From: Markus Scherer Date: Mon, 2 Jun 2014 16:52:13 +0000 Subject: [PATCH] ICU-7057 InputStream.read() does not always read all .available() bytes X-SVN-Rev: 35790 --- .../core/src/com/ibm/icu/impl/ICUBinary.java | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUBinary.java b/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUBinary.java index 764df4a8b1d..8856516bd38 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUBinary.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/impl/ICUBinary.java @@ -218,14 +218,12 @@ public final class ICUBinary try { int avail = is.available(); byte[] bytes = new byte[avail]; - int numRead = is.read(bytes); - assert numRead == avail; + readFully(is, bytes, 0, avail); while((avail = is.available()) != 0) { // TODO Java 6 replace new byte[] and arraycopy(): byte[] newBytes = Arrays.copyOf(bytes, bytes.length + avail); byte[] newBytes = new byte[bytes.length + avail]; System.arraycopy(bytes, 0, newBytes, 0, bytes.length); - numRead = is.read(newBytes, bytes.length, avail); - assert numRead == avail; + readFully(is, newBytes, bytes.length, avail); bytes = newBytes; } return ByteBuffer.wrap(bytes); @@ -234,6 +232,16 @@ public final class ICUBinary } } + private static final void readFully(InputStream is, byte[] bytes, int offset, int avail) + throws IOException { + while (avail > 0) { + int numRead = is.read(bytes, offset, avail); + assert numRead > 0; + offset += numRead; + avail -= numRead; + } + } + // private variables ------------------------------------------------- /** -- 2.40.0