harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From telli...@apache.org
Subject svn commit: r884013 - /harmony/enhanced/classlib/trunk/modules/sound/src/main/java/org/apache/harmony/sound/utils/ProviderService.java
Date Wed, 25 Nov 2009 09:56:23 GMT
Author: tellison
Date: Wed Nov 25 09:55:36 2009
New Revision: 884013

URL: http://svn.apache.org/viewvc?rev=884013&view=rev
Log:
Fix for HARMONY-6386 ([classlib][sound] Failure to load sound providers)

Modified:
    harmony/enhanced/classlib/trunk/modules/sound/src/main/java/org/apache/harmony/sound/utils/ProviderService.java

Modified: harmony/enhanced/classlib/trunk/modules/sound/src/main/java/org/apache/harmony/sound/utils/ProviderService.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/classlib/trunk/modules/sound/src/main/java/org/apache/harmony/sound/utils/ProviderService.java?rev=884013&r1=884012&r2=884013&view=diff
==============================================================================
--- harmony/enhanced/classlib/trunk/modules/sound/src/main/java/org/apache/harmony/sound/utils/ProviderService.java
(original)
+++ harmony/enhanced/classlib/trunk/modules/sound/src/main/java/org/apache/harmony/sound/utils/ProviderService.java
Wed Nov 25 09:55:36 2009
@@ -17,6 +17,7 @@
 
 package org.apache.harmony.sound.utils;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.InputStream;
@@ -144,9 +145,7 @@
                             try {
                                 InputStream in = urls.nextElement()
                                         .openStream();
-                                bytes = new byte[in.available()];
-                                in.read(bytes);
-                                in.close();
+                                bytes = getAllBytesFromStreamAndClose(in);
                             } catch (IOException e) {
                                 continue;
                             }
@@ -173,5 +172,41 @@
     public static Properties getSoundProperties() {
         return devices;
     }
+    
+    /*
+     * 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 static byte[] getAllBytesFromStreamAndClose(InputStream is)
+            throws IOException {
+        try {
+            // Initial read
+            byte[] buffer = new byte[512];
+            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();
+        }
+    }
 
 }



Mime
View raw message