将输出流OutputStream转化为输入流InputStream的方法

来源:互联网 发布:we淘宝官方旗舰店 编辑:程序博客网 时间:2024/06/05 03:50
  1. 将输出流OutputStream转化为输入流InputStream的方法  
  2. 一:  
  3. package test.io;     
  4. import java.io.ByteArrayInputStream;     
  5. import java.io.ByteArrayOutputStream;     
  6. import java.io.IOException;     
  7. /**  
  8. * 用于把OutputStream 转化为 InputStream。  
  9. * 适合于数据量不大,且内存足够全部容纳这些数据的情况。  
  10.  
  11. */    
  12. public class Test1 {     
  13. /**  
  14. * @param args  
  15. * @throws IOException  
  16. */    
  17. public static void main(String[] args) throws IOException {  
  18. ByteArrayOutputStream out = new ByteArrayOutputStream();  
  19. byte[] bs = new byte[] { 12345 };    
  20. out.write(bs);   
  21.   
  22. ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())   
  23. byte[] bs = new byte[1024];     
  24. int len = in.read(bs);     
  25. for (int i = 0; i < len; i++) {     
  26. System.out.println(bs[i]);     
  27. }     
  28. }  
  29. }   
  30.   
  31. 二:  
  32. package test.io;     
  33. import java.io.IOException;     
  34. import java.io.PipedInputStream;     
  35. import java.io.PipedOutputStream;     
  36. /**  
  37. * 用于把OutputStream 转化为 InputStream。 适合于数据量大的情况,一个类专门负责产生数据,另一个类负责读取数据。  
  38. */    
  39. public class Test2 {     
  40. /**  
  41. * @param args  
  42. * @throws IOException  
  43. */    
  44. public static void main(String[] args) throws IOException {     
  45. // 使用Piped 的输入输出流  
  46. PipedInputStream in = new PipedInputStream();  
  47. final PipedOutputStream out = new PipedOutputStream(in);  
  48. // 启动线程,让数据产生者单独运行  
  49. new Thread(new Runnable() {  
  50. public void run() {  
  51. try {  
  52. byte[] bs = new byte[2];  
  53. for (int i = 0; i <= 100; i++) {  
  54. bs[0] = (byte) i;  
  55. bs[1] = (byte) (i + 1);  
  56. // 测试写入字节数组  
  57. out.write(bs);  
  58. out.flush();  
  59. // 等待0.1秒  
  60. Thread.sleep(100);  
  61. }  
  62. catch (IOException e) {  
  63. e.printStackTrace();  
  64. catch (InterruptedException e) {  
  65. e.printStackTrace();  
  66. }  
  67. }  
  68. }).start();  
  69. // 数据使用者处理数据  
  70. // 也可以使用线程来进行并行处理  
  71. byte[] bs = new byte[1024];  
  72. int len;  
  73. // 读取数据,并进行处理  
  74. try {  
  75. while ((len = in.read(bs)) != -1) {  
  76. for (int i = 0; i < len; i++) {  
  77. System.out.println(bs[i]);  
  78. }  
  79. }  
  80. catch (IOException e) {  
  81. e.printStackTrace();  
  82. }  
  83. }  
  84. }  
  85.   
  86. 下面是关于 PipedOutputStream 的API介绍  
  87. 传送输出流可以连接到传送输入流,以创建通信管道。传送输出流是管道的发送端。通常,数据由某个线程写入 PipedOutputStream 对象,并由其他线程从连接的 PipedInputStream 读取。不建议对这两个对象尝试使用单个线程,因为这样可能会死锁该线程。  
  88.   
  89. 下面是关于 PipedInputStream的API介绍  
  90. 传送输入流应该连接到传送输出流;传送输入流会提供要写入传送输出流的所有数据字节。通常,数据由某个线程从 PipedInputStream 对象读取,并由其他线程将其写入到相应的 PipedOutputStream。不建议对这两个对象尝试使用单个线程,因为这样可能会死锁该线程。传送输入流包含一个缓冲区,可在缓冲区限定的范围内将读操作和写操作分离开。  
  91.   
  92. 三:  
  93. package test.io;  
  94. import java.io.IOException;     
  95. import java.io.InputStream;     
  96. import java.io.OutputStream;     
  97. import com.Ostermiller.util.CircularByteBuffer;     
  98. /**  
  99. * 用于把OutputStream 转化为 InputStream。  
  100. * <p>  
  101. * 使用CircilarBuffer 辅助类 <br>  
  102. * 下载地址为 <A href="http://ostermiller.org/utils/download.html  
  103. http://ostermiller.org/utils/download.html<br>  
  104. * 介绍地址为 http://ostermiller.org/utils/CircularBuffer.html  
  105. * </p> 
  106. */    
  107. public class Test3 {     
  108. /**  
  109. * @param args  
  110. * @throws IOException  
  111. */    
  112. public static void main(String[] args) throws IOException {     
  113. // 使用CircularByteBuffer    
  114. final CircularByteBuffer cbb = new CircularByteBuffer();     
  115. // 启动线程,让数据产生者单独运行    
  116. new Thread(new Runnable() {     
  117. public void run() {     
  118. try {     
  119. OutputStreamClass3.putDataOnOutputStream(cbb.getOutputStream());     
  120. catch (IOException e) {     
  121. e.printStackTrace();     
  122. }     
  123. }     
  124. }).start();     
  125. // 数据使用者处理数据    
  126. // 也可以使用线程来进行并行处理    
  127. InputStreamClass3.processDataFromInputStream(cbb.getInputStream());     
  128. }     
  129. }     
  130. class OutputStreamClass3 {     
  131. public static void putDataOnOutputStream(OutputStream out) throws IOException {     
  132. byte[] bs = new byte[2];     
  133. for (int i = 0; i <= 100; i++) {     
  134. bs[0] = (byte) i;     
  135. bs[1] = (byte) (i + 1);     
  136. // 测试写入字节数组    
  137. out.write(bs);     
  138. out.flush();     
  139. try {     
  140. // 等待0.1秒    
  141. Thread.sleep(100);     
  142. catch (InterruptedException e) {     
  143. e.printStackTrace();     
  144. }     
  145. }     
  146. }     
  147. }     
  148. class InputStreamClass3 {     
  149. public static void processDataFromInputStream(InputStream in) {     
  150. byte[] bs = new byte[1024];     
  151. int len;     
  152. // 读取数据,并进行处理    
  153. try {     
  154. while ((len = in.read(bs)) != -1) {     
  155. for (int i = 0; i < len; i++) {     
  156. System.out.println(bs[i]);     
  157. }     
  158. }     
  159. catch (IOException e) {     
  160. e.printStackTrace();     
  161. }     
  162. }     
  163. }    
  164.   
  165. 此方法使用了一个类处理,代码更简洁,可以很方便的在缓冲处理全部数据的小数据量情况和多线程处理大数据量的不同情况切换  
  166.   
  167. package test.io;     
  168. import java.io.IOException;     
  169. import java.io.InputStream;     
  170. import java.io.OutputStream;     
  171. import com.Ostermiller.util.CircularByteBuffer;     
  172. /**  
  173. * 用于把OutputStream 转化为 InputStream。  
  174. * <p>  
  175. * 使用CircilarBuffer 辅助类 <br>  
  176. * 下载地址为 <A href="http://ostermiller.org/utils/download.html  
  177. * http://ostermiller.org/utils/download.html<br>  
  178. * 介绍地址为 http://ostermiller.org/utils/CircularBuffer.html  
  179. * </p> 
  180. */    
  181. public class Test4 {     
  182. /**  
  183. * @param args  
  184. * @throws IOException  
  185. */    
  186. public static void main(String[] args) throws IOException {     
  187. // 缓冲所有数据的例子,不使用多线程    
  188. CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);     
  189. OutputStreamClass4.putDataOnOutputStream(cbb.getOutputStream());     
  190. InputStreamClass4.processDataFromInputStream(cbb.getInputStream());     
  191. }     
  192. }     
  193. class OutputStreamClass4 {     
  194. public static void putDataOnOutputStream(OutputStream out) throws IOException {     
  195. byte[] bs = new byte[] { 12345 };     
  196. out.write(bs);     
  197. }     
  198. }     
  199. class InputStreamClass4 {     
  200. public static void processDataFromInputStream(InputStream in) throws IOException {     
  201. byte[] bs = new byte[1024];     
  202. int len = in.read(bs);     
  203. for (int i = 0; i < len; i++) {     
  204. System.out.println(bs[i]);     
  205. }     
  206. }     
  207. }  
0 0
原创粉丝点击