licensing update to APSL 2.0
[org.ibex.util.git] / src / org / ibex / util / InputStreamToByteArray.java
1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
4
5 package org.ibex.util;
6 import java.io.*;
7
8 public class InputStreamToByteArray {
9
10     /** scratch space for isToByteArray() */
11     private static byte[] workspace = new byte[16 * 1024];
12
13     /** Trivial method to completely read an InputStream */
14     public static synchronized byte[] convert(InputStream is) throws IOException {
15         int pos = 0;
16         while (true) {
17             int numread = is.read(workspace, pos, workspace.length - pos);
18             if (numread == -1) break;
19             else if (pos + numread < workspace.length) pos += numread;
20             else {
21                 pos += numread;
22                 byte[] temp = new byte[workspace.length * 2];
23                 System.arraycopy(workspace, 0, temp, 0, workspace.length);
24                 workspace = temp;
25             }
26         }
27         byte[] ret = new byte[pos];
28         System.arraycopy(workspace, 0, ret, 0, pos);
29         return ret;
30     }
31
32 }