-
- package test.io;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- /**
- * 用于把OutputStream 转化为 InputStream。
- * 适合于数据量不大,且内存足够全部容纳这些数据的情况。
- * @author 赵学庆 www.java2000.net
- *
- */
- public class Test1 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- OutputStreamClass1.putDataOnOutputStream(out);
- InputStreamClass1.processDataFromInputStream(new ByteArrayInputStream(out.toByteArray()));
- }
- }
- class OutputStreamClass1 {
- public static void putDataOnOutputStream(ByteArrayOutputStream out) throws IOException {
- byte[] bs = new byte[] { 1, 2, 3, 4, 5 };
- out.write(bs);
- }
- }
- class InputStreamClass1 {
- public static void processDataFromInputStream(ByteArrayInputStream in) throws IOException {
- byte[] bs = new byte[1024];
- int len = in.read(bs);
- for (int i = 0; i < len; i++) {
- System.out.println(bs[i]);
- }
- }
- }
|