KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > python > core > FileUtil


1 // Copyright (c) 2003 Jython project
2
package org.python.core;
3
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6 import java.io.IOException JavaDoc;
7
8 /**
9  * Utility methods for Java file handling.
10  */

11 public class FileUtil {
12     /**
13      * Read all bytes from the input stream.
14      * <p/>
15      * Note that using this method to read very large streams could
16      * cause out-of-memory exceptions and/or block for large periods
17      * of time.
18      */

19     public static byte[] readBytes(InputStream JavaDoc in)
20       throws IOException JavaDoc {
21         final int bufsize = 8192; // nice buffer size used in JDK
22
byte[] buf = new byte[bufsize];
23         ByteArrayOutputStream JavaDoc out =
24           new ByteArrayOutputStream JavaDoc(bufsize);
25         int count;
26         while (true) {
27             count = in.read(buf, 0, bufsize);
28             if (count < 0) {
29                 break;
30             }
31             out.write(buf, 0, count);
32         }
33         return out.toByteArray();
34     }
35 }
36
Popular Tags