Make test more robust by ensuring all bytes are read from input stream, not just those that are immediately available.
Let any IOException percolate up to test framework for handling.

git-svn-id: https://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk@882634 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java b/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
index f83b216..65f6c9a 100644
--- a/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
+++ b/modules/security/src/test/api/java/org/apache/harmony/security/tests/java/security/SecureClassLoader2Test.java
@@ -17,6 +17,7 @@
 
 package org.apache.harmony.security.tests.java.security;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
 import java.io.InputStream;
@@ -35,7 +36,7 @@
 	/**
 	 * @tests java.security.SecureClassLoader#getPermissions(java.security.CodeSource)
 	 */
-	public void test_getPermissionsLjava_security_CodeSource() {
+	public void test_getPermissionsLjava_security_CodeSource() throws IOException {
 		class MyClassLoader extends SecureClassLoader {
 			public PermissionCollection getPerms() {
 				return super.getPermissions(new CodeSource(null,
@@ -58,21 +59,49 @@
 		}
 		assertEquals("expected no permissions", 0, count);
 
-		byte[] bytes = null;
-		try {
-			File file = Support_GetLocal.getLocalFile("hyts_security.jar");
-			JarFile jar = new JarFile(file);
-			InputStream in = jar.getInputStream(jar
-					.getEntry("packA/SecurityTest.class"));
-			bytes = new byte[in.available()];
-			in.read(bytes);
-			in.close();
-		} catch (IOException e) {
-			fail("unexpected IOException : " + e);
-		}
-		Class c = myloader.define("packA.SecurityTest", bytes);
+        File file = Support_GetLocal.getLocalFile("hyts_security.jar");
+        JarFile jar = new JarFile(file);
+        InputStream in = jar.getInputStream(jar
+                .getEntry("packA/SecurityTest.class"));
+        byte[] bytes = drain(in);
+        Class c = myloader.define("packA.SecurityTest", bytes);
 		ProtectionDomain pd = c.getProtectionDomain();
 		assertNotNull("Expected dynamic policy", pd.getClassLoader());
 		assertNull("Expected null permissions", pd.getPermissions());
 	}
+
+    /*
+     * Drains the entire content from the given input stream and returns it as a
+     * byte[]. The stream is closed after being drained, or if an IOException
+     * occurs.
+     */
+    private byte[] drain(InputStream is) throws IOException {
+        try {
+            // Initial read
+            byte[] buffer = new byte[1024];
+            int count = is.read(buffer);
+            int nextByte = is.read();
+
+            // Did we get it all in one read?
+            if (nextByte == -1) {
+                byte[] dest = new byte[count];
+                System.arraycopy(buffer, 0, dest, 0, count);
+                return dest;
+            }
+
+            // Requires additional reads
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(count * 2);
+            baos.write(buffer, 0, count);
+            baos.write(nextByte);
+            while (true) {
+                count = is.read(buffer);
+                if (count == -1) {
+                    return baos.toByteArray();
+                }
+                baos.write(buffer, 0, count);
+            }
+        } finally {
+            is.close();
+        }
+    }
 }
\ No newline at end of file