01: // Copyright (c) 2003 Jython project
02: package org.python.core;
03:
04: import java.io.ByteArrayOutputStream;
05: import java.io.InputStream;
06: import java.io.IOException;
07:
08: /**
09: * Utility methods for Java file handling.
10: */
11: public class FileUtil {
12: /**
13: * Read all bytes from the input stream. <p/> Note that using this method to
14: * read very large streams could cause out-of-memory exceptions and/or block
15: * for large periods of time.
16: */
17: public static byte[] readBytes(InputStream in) throws IOException {
18: final int bufsize = 8192; // nice buffer size used in JDK
19: byte[] buf = new byte[bufsize];
20: ByteArrayOutputStream out = new ByteArrayOutputStream(bufsize);
21: int count;
22: while (true) {
23: count = in.read(buf, 0, bufsize);
24: if (count < 0) {
25: break;
26: }
27: out.write(buf, 0, count);
28: }
29: return out.toByteArray();
30: }
31: }
|