]> granicus.if.org Git - icu/commitdiff
ICU-7057 InputStream.read() does not always read all .available() bytes
authorMarkus Scherer <markus.icu@gmail.com>
Mon, 2 Jun 2014 16:52:13 +0000 (16:52 +0000)
committerMarkus Scherer <markus.icu@gmail.com>
Mon, 2 Jun 2014 16:52:13 +0000 (16:52 +0000)
X-SVN-Rev: 35790

icu4j/main/classes/core/src/com/ibm/icu/impl/ICUBinary.java

index 764df4a8b1d01341aff7ccfd36a8df63a90cfd78..8856516bd38b948623e4574cbffe9cac3d65c5f8 100644 (file)
@@ -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 -------------------------------------------------
   
     /**