1 // Copyright (C) 2003 Adam Megacz <adam@ibex.org> all rights reserved.
3 // You may modify, copy, and redistribute this code under the terms of
4 // the GNU Library Public License version 2.1, with the exception of
5 // the portion of clause 6a after the semicolon (aka the "obnoxious
11 public class InputStreamToByteArray {
13 /** scratch space for isToByteArray() */
14 private static byte[] workspace = new byte[16 * 1024];
16 /** Trivial method to completely read an InputStream */
17 public static synchronized byte[] convert(InputStream is) throws IOException {
20 int numread = is.read(workspace, pos, workspace.length - pos);
21 if (numread == -1) break;
22 else if (pos + numread < workspace.length) pos += numread;
25 byte[] temp = new byte[workspace.length * 2];
26 System.arraycopy(workspace, 0, temp, 0, workspace.length);
30 byte[] ret = new byte[pos];
31 System.arraycopy(workspace, 0, ret, 0, pos);