FELIX-4375 - improve stream handling:

- improve the reading of gosh scripts from URLs using the proposed patch.



git-svn-id: https://svn.apache.org/repos/asf/felix/trunk@1725519 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java b/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
index a2c6602..39a1168 100644
--- a/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
+++ b/gogo/shell/src/main/java/org/apache/felix/gogo/shell/Shell.java
@@ -22,6 +22,7 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.Reader;
+import java.net.HttpURLConnection;
 import java.net.URI;
 import java.net.URL;
 import java.net.URLConnection;
@@ -232,23 +233,35 @@
 
     private CharSequence readScript(URI script) throws Exception
     {
-        URLConnection conn = script.toURL().openConnection();
-        int length = conn.getContentLength();
+        CharBuffer buf = CharBuffer.allocate(4096);
+        StringBuilder sb = new StringBuilder();
 
-        if (length == -1)
+        URLConnection conn = script.toURL().openConnection();
+
+        InputStreamReader in = null;
+        try
         {
-            System.err.println("eek! unknown Contentlength for: " + script);
-            length = 10240;
+            in = new InputStreamReader(conn.getInputStream());
+            while (in.read(buf) > 0)
+            {
+                buf.flip();
+                sb.append(buf);
+                buf.clear();
+            }
+        }
+        finally
+        {
+            if (conn instanceof HttpURLConnection)
+            {
+                ((HttpURLConnection) conn).disconnect();
+            }
+            if (in != null)
+            {
+                in.close();
+            }
         }
 
-        InputStream in = conn.getInputStream();
-        CharBuffer cbuf = CharBuffer.allocate(length);
-        Reader reader = new InputStreamReader(in);
-        reader.read(cbuf);
-        in.close();
-        cbuf.rewind();
-
-        return cbuf;
+        return sb;
     }
 
     @SuppressWarnings("unchecked")