免费预览已结束,剩余1页可下载查看
下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
将输出流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 赵学庆 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); 2021 ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray() 22 byte bs = new byte1024; 23 int len = in.read(bs); 24 for (int i = 0; i len; i+) 25 System.out.println(bsi); 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 赵学庆 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 byte2;24 for (int i = 0; i = 100; i+) 25 bs0 = (byte) i;26 bs1 = (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 byte1024;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(bsi);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 * 9 * 使用CircilarBuffer 辅助类 10 * 下载地址为 A href=/utils/download.html 11 /utils/download.html 12 * 介绍地址为 /utils/CircularBuffer.html 13 * 14 * 15 * author 赵学庆 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 InputStreamCcessDataFromInputStream(cbb.getInputStream(); 38 39 40 class OutputStreamClass3 41 public static void putDataOnOutputStream(OutputStream out) throws IOException 42 byte bs = new byte2; 43 for (int i = 0; i = 100; i+) 44 bs0 = (byte) i; 45 bs1 = (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 byte1024; 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(bsi); 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 * 82 * 使用CircilarBuffer 辅助类 83 * 下载地址为 A href=/utils/download.html 84 /utils/download.html 85 * 介绍地址为 /utils/CircularBuffer.html 86 * 87 * 88 * author 赵学庆 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 InputStreamCcessDataFromInputStream(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.
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025医院护理三基培训考试试题(附答案)
- 2025年项目管理与实施知识考察试题及答案解析
- 药店GSP认证质量管理体系文件培训考试试题及答案
- 2025年安全员考试考题及参考答案
- 2025年酒店前台服务员技能考核试题及答案
- 2025年文书档案考试题库及答案
- 2025年人工智能应用开发考试试题及答案
- 内科主治医师内科学考试历年真题及答案
- 交通事故伤残赔偿项目计算标准
- 2025年科普知识竞赛题及答案
- GB/T 2481.2-2020固结磨具用磨料粒度组成的检测和标记第2部分:微粉
- 糖尿病肾病诊治进展 课件
- 安全员之A证(企业负责人)【含答案】
- MoldFlow实例分析报告-中英文对照课件
- 部编 二年级语文上册 第五单元【集体备课】课件
- 泸定桥的资料
- 工业硅项目可行性研究报告
- 烟草专卖执法案件知识讲稿课件
- 《神经病学》(英文)课件11.Peripheral Neuropathy
- PID图(工艺仪表流程图)基础知识培训
- 预应力管桩单桩竖向承载力特征值计算表格
评论
0/150
提交评论