輸出流轉為輸入流


 

原文地址:https://blog.csdn.net/u010822824/article/details/51030150

 

  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[] { 1, 2, 3, 4, 5 };    
  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[] { 1, 2, 3, 4, 5 };     
  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. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM