原文地址:https://blog.csdn.net/u010822824/article/details/51030150
- 將輸出流OutputStream轉化為輸入流InputStream的方法
- 一:
- package test.io;
- import java.io.ByteArrayInputStream;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- /**
- * 用於把OutputStream 轉化為 InputStream。
- * 適合於數據量不大,且內存足夠全部容納這些數據的情況。
- *
- */
- public class Test1 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- ByteArrayOutputStream out = new ByteArrayOutputStream();
- byte[] bs = new byte[] { 1, 2, 3, 4, 5 };
- out.write(bs);
- ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray())
- byte[] bs = new byte[1024];
- int len = in.read(bs);
- for (int i = 0; i < len; i++) {
- System.out.println(bs[i]);
- }
- }
- }
- 二:
- package test.io;
- import java.io.IOException;
- import java.io.PipedInputStream;
- import java.io.PipedOutputStream;
- /**
- * 用於把OutputStream 轉化為 InputStream。 適合於數據量大的情況,一個類專門負責產生數據,另一個類負責讀取數據。
- */
- public class Test2 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // 使用Piped 的輸入輸出流
- PipedInputStream in = new PipedInputStream();
- final PipedOutputStream out = new PipedOutputStream(in);
- // 啟動線程,讓數據產生者單獨運行
- new Thread(new Runnable() {
- public void run() {
- try {
- byte[] bs = new byte[2];
- for (int i = 0; i <= 100; i++) {
- bs[0] = (byte) i;
- bs[1] = (byte) (i + 1);
- // 測試寫入字節數組
- out.write(bs);
- out.flush();
- // 等待0.1秒
- Thread.sleep(100);
- }
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }).start();
- // 數據使用者處理數據
- // 也可以使用線程來進行並行處理
- byte[] bs = new byte[1024];
- int len;
- // 讀取數據,並進行處理
- try {
- while ((len = in.read(bs)) != -1) {
- for (int i = 0; i < len; i++) {
- System.out.println(bs[i]);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- 下面是關於 PipedOutputStream 的API介紹
- 傳送輸出流可以連接到傳送輸入流,以創建通信管道。傳送輸出流是管道的發送端。通常,數據由某個線程寫入 PipedOutputStream 對象,並由其他線程從連接的 PipedInputStream 讀取。不建議對這兩個對象嘗試使用單個線程,因為這樣可能會死鎖該線程。
- 下面是關於 PipedInputStream的API介紹
- 傳送輸入流應該連接到傳送輸出流;傳送輸入流會提供要寫入傳送輸出流的所有數據字節。通常,數據由某個線程從 PipedInputStream 對象讀取,並由其他線程將其寫入到相應的 PipedOutputStream。不建議對這兩個對象嘗試使用單個線程,因為這樣可能會死鎖該線程。傳送輸入流包含一個緩沖區,可在緩沖區限定的范圍內將讀操作和寫操作分離開。
- 三:
- package test.io;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import com.Ostermiller.util.CircularByteBuffer;
- /**
- * 用於把OutputStream 轉化為 InputStream。
- * <p>
- * 使用CircilarBuffer 輔助類 <br>
- * 下載地址為 <A href="http://ostermiller.org/utils/download.html
- http://ostermiller.org/utils/download.html<br>
- * 介紹地址為 http://ostermiller.org/utils/CircularBuffer.html
- * </p>
- */
- public class Test3 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // 使用CircularByteBuffer
- final CircularByteBuffer cbb = new CircularByteBuffer();
- // 啟動線程,讓數據產生者單獨運行
- new Thread(new Runnable() {
- public void run() {
- try {
- OutputStreamClass3.putDataOnOutputStream(cbb.getOutputStream());
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }).start();
- // 數據使用者處理數據
- // 也可以使用線程來進行並行處理
- InputStreamClass3.processDataFromInputStream(cbb.getInputStream());
- }
- }
- class OutputStreamClass3 {
- public static void putDataOnOutputStream(OutputStream out) throws IOException {
- byte[] bs = new byte[2];
- for (int i = 0; i <= 100; i++) {
- bs[0] = (byte) i;
- bs[1] = (byte) (i + 1);
- // 測試寫入字節數組
- out.write(bs);
- out.flush();
- try {
- // 等待0.1秒
- Thread.sleep(100);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
- }
- class InputStreamClass3 {
- public static void processDataFromInputStream(InputStream in) {
- byte[] bs = new byte[1024];
- int len;
- // 讀取數據,並進行處理
- try {
- while ((len = in.read(bs)) != -1) {
- for (int i = 0; i < len; i++) {
- System.out.println(bs[i]);
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- 此方法使用了一個類處理,代碼更簡潔,可以很方便的在緩沖處理全部數據的小數據量情況和多線程處理大數據量的不同情況切換
- package test.io;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.OutputStream;
- import com.Ostermiller.util.CircularByteBuffer;
- /**
- * 用於把OutputStream 轉化為 InputStream。
- * <p>
- * 使用CircilarBuffer 輔助類 <br>
- * 下載地址為 <A href="http://ostermiller.org/utils/download.html
- * http://ostermiller.org/utils/download.html<br>
- * 介紹地址為 http://ostermiller.org/utils/CircularBuffer.html
- * </p>
- */
- public class Test4 {
- /**
- * @param args
- * @throws IOException
- */
- public static void main(String[] args) throws IOException {
- // 緩沖所有數據的例子,不使用多線程
- CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
- OutputStreamClass4.putDataOnOutputStream(cbb.getOutputStream());
- InputStreamClass4.processDataFromInputStream(cbb.getInputStream());
- }
- }
- class OutputStreamClass4 {
- public static void putDataOnOutputStream(OutputStream out) throws IOException {
- byte[] bs = new byte[] { 1, 2, 3, 4, 5 };
- out.write(bs);
- }
- }
- class InputStreamClass4 {
- public static void processDataFromInputStream(InputStream 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]);
- }
- }
- }