mass rename and rebranding from xwt to ibex - fixed to use ixt files
[org.ibex.core.git] / src / org / ibex / util / InputStreamToByteArray.java
diff --git a/src/org/ibex/util/InputStreamToByteArray.java b/src/org/ibex/util/InputStreamToByteArray.java
new file mode 100644 (file)
index 0000000..7e19644
--- /dev/null
@@ -0,0 +1,35 @@
+// Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
+//
+// You may modify, copy, and redistribute this code under the terms of
+// the GNU Library Public License version 2.1, with the exception of
+// the portion of clause 6a after the semicolon (aka the "obnoxious
+// relink clause")
+
+package org.ibex.util;
+import java.io.*;
+
+public class InputStreamToByteArray {
+
+    /** scratch space for isToByteArray() */
+    private static byte[] workspace = new byte[16 * 1024];
+
+    /** Trivial method to completely read an InputStream */
+    public static synchronized byte[] convert(InputStream is) throws IOException {
+        int pos = 0;
+        while (true) {
+            int numread = is.read(workspace, pos, workspace.length - pos);
+            if (numread == -1) break;
+            else if (pos + numread < workspace.length) pos += numread;
+            else {
+                pos += numread;
+                byte[] temp = new byte[workspace.length * 2];
+                System.arraycopy(workspace, 0, temp, 0, workspace.length);
+                workspace = temp;
+            }
+        }
+        byte[] ret = new byte[pos];
+        System.arraycopy(workspace, 0, ret, 0, pos);
+        return ret;
+    }
+
+}