本文共 5548 字,大约阅读时间需要 18 分钟。
将输出流OutputStream转化为输入流InputStream的方法
一:
1 package test.io;
2 import java.io.ByteArrayInputStream;
3 import java.io.ByteArrayOutputStream;
4 import java.io.IOException;
5 /**
6 * 用于把OutputStream 转化为 InputStream。
7 * 适合于数据量不大,且内存足够全部容纳这些数据的情况。
8 * @author 赵学庆 www.java2000.net
9 *
10 */
11 public class Test1 {
12 /**
13 * @param args
14 * @throws IOException
15 */
16 public static void main(String[] args) throws IOException {
17 ByteArrayOutputStream out = new ByteArrayOutputStream();
18 byte[] bs = new byte[] { 1, 2, 3, 4, 5 };
19 out.write(bs);
20
21 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())
22 byte[] bs = new byte[1024];
23 int len = in.read(bs);
24 for (int i = 0; i < len; i++) {
25 System.out.println(bs[i]);
26 }
27 }
28 }
二:
1 package test.io;
2 import java.io.IOException;
3 import java.io.PipedInputStream;
4 import java.io.PipedOutputStream;
5 /**
6 * 用于把OutputStream 转化为 InputStream。 适合于数据量大的情况,一个类专门负责产生数据,另一个类负责读取数据。
7 *
8 * @author 赵学庆 www.java2000.net
9 */
10 public class Test2 {
11 /**
12 * @param args
13 * @throws IOException
14 */
15 public static void main(String[] args) throws IOException {
16 // 使用Piped 的输入输出流
17 PipedInputStream in = new PipedInputStream();
18 final PipedOutputStream out = new PipedOutputStream(in);
19 // 启动线程,让数据产生者单独运行
20 new Thread(new Runnable() {
21 public void run() {
22 try {
23 byte[] bs = new byte[2];
24 for (int i = 0; i <= 100; i++) {
25 bs[0] = (byte) i;
26 bs[1] = (byte) (i + 1);
27 // 测试写入字节数组
28 out.write(bs);
29 out.flush();
30 // 等待0.1秒
31 Thread.sleep(100);
32 }
33 } catch (IOException e) {
34 e.printStackTrace();
35 } catch (InterruptedException e) {
36 e.printStackTrace();
37 }
38 }
39 }).start();
40 // 数据使用者处理数据
41 // 也可以使用线程来进行并行处理
42 byte[] bs = new byte[1024];
43 int len;
44 // 读取数据,并进行处理
45 try {
46 while ((len = in.read(bs)) != -1) {
47 for (int i = 0; i < len; i++) {
48 System.out.println(bs[i]);
49 }
50 }
51 } catch (IOException e) {
52 e.printStackTrace();
53 }
54 }
55 }
下面是关于 PipedOutputStream 的API介绍
传送输出流可以连接到传送输入流,以创建通信管道。传送输出流是管道的发送端。通常,数据由某个线程写入 PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。不建议对这两个对象尝试使用单个线程,因为这样可能会死锁该线程。 下面是关于 PipedInputStream的API介绍 传送输入流应该连接到传送输出流;传送输入流会提供要写入传送输出流的所有数据字节。通常,数据由某个线程从 PipedInputStream 对象读取,并由其他线程将其写入到相应的 PipedOutputStream。不建议对这两个对象尝试使用单个线程,因为这样可能会死锁该线程。传送输入流包含一个缓冲区,可在缓冲区限定的范围内将读操作和写操作分离开。三:
1 package test.io;
2 import java.io.IOException;
3 import java.io.InputStream;
4 import java.io.OutputStream;
5 import com.Ostermiller.util.CircularByteBuffer;
6 /**
7 * 用于把OutputStream 转化为 InputStream。
8 * <p>
9 * 使用CircilarBuffer 辅助类 <br>
10 * 下载地址为 <A href="http://ostermiller.org/utils/download.html
11 http://ostermiller.org/utils/download.html<br>
12 * 介绍地址为 http://ostermiller.org/utils/CircularBuffer.html
13 * </p>
14 *
15 * @author 赵学庆 www.java2000.net
16 */
17 public class Test3 {
18 /**
19 * @param args
20 * @throws IOException
21 */
22 public static void main(String[] args) throws IOException {
23 // 使用CircularByteBuffer
24 final CircularByteBuffer cbb = new CircularByteBuffer();
25 // 启动线程,让数据产生者单独运行
26 new Thread(new Runnable() {
27 public void run() {
28 try {
29 OutputStreamClass3.putDataOnOutputStream(cbb.getOutputStream());
30 } catch (IOException e) {
31 e.printStackTrace();
32 }
33 }
34 }).start();
35 // 数据使用者处理数据
36 // 也可以使用线程来进行并行处理
37 InputStreamClass3.processDataFromInputStream(cbb.getInputStream());
38 }
39 }
40 class OutputStreamClass3 {
41 public static void putDataOnOutputStream(OutputStream out) throws IOException {
42 byte[] bs = new byte[2];
43 for (int i = 0; i <= 100; i++) {
44 bs[0] = (byte) i;
45 bs[1] = (byte) (i + 1);
46 // 测试写入字节数组
47 out.write(bs);
48 out.flush();
49 try {
50 // 等待0.1秒
51 Thread.sleep(100);
52 } catch (InterruptedException e) {
53 e.printStackTrace();
54 }
55 }
56 }
57 }
58 class InputStreamClass3 {
59 public static void processDataFromInputStream(InputStream in) {
60 byte[] bs = new byte[1024];
61 int len;
62 // 读取数据,并进行处理
63 try {
64 while ((len = in.read(bs)) != -1) {
65 for (int i = 0; i < len; i++) {
66 System.out.println(bs[i]);
67 }
68 }
69 } catch (IOException e) {
70 e.printStackTrace();
71 }
72 }
73 }
此方法使用了一个类处理,代码更简洁,可以很方便的在缓冲处理全部数据的小数据量情况和多线程处理大数据量的不同情况切换
74 package test.io;
75 import java.io.IOException;
76 import java.io.InputStream;
77 import java.io.OutputStream;
78 import com.Ostermiller.util.CircularByteBuffer;
79 /**
80 * 用于把OutputStream 转化为 InputStream。
81 * <p>
82 * 使用CircilarBuffer 辅助类 <br>
83 * 下载地址为 <A href="http://ostermiller.org/utils/download.html
84 http://ostermiller.org/utils/download.html<br>
85 * 介绍地址为 http://ostermiller.org/utils/CircularBuffer.html
86 * </p>
87 *
88 * @author 赵学庆 www.java2000.net
89 */
90 public class Test4 {
91 /**
92 * @param args
93 * @throws IOException
94 */
95 public static void main(String[] args) throws IOException {
96 // 缓冲所有数据的例子,不使用多线程
97 CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
98 OutputStreamClass4.putDataOnOutputStream(cbb.getOutputStream());
99 InputStreamClass4.processDataFromInputStream(cbb.getInputStream());
100 }
101 }
102 class OutputStreamClass4 {
103 public static void putDataOnOutputStream(OutputStream out) throws IOException {
104 byte[] bs = new byte[] { 1, 2, 3, 4, 5 };
105 out.write(bs);
106 }
107 }
108 class InputStreamClass4 {
109 public static void processDataFromInputStream(InputStream in) throws IOException {
110 byte[] bs = new byte[1024];
111 int len = in.read(bs);
112 for (int i = 0; i < len; i++) {
113 System.out.println(bs[i]);
114 }
115 }
116 }
转载地址:https://blog.csdn.net/jiafu1115/article/details/7053699 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!