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

  1.     
  2. package test.io;   
  3. import java.io.IOException;   
  4. import java.io.InputStream;   
  5. import java.io.OutputStream;   
  6. import com.Ostermiller.util.CircularByteBuffer;   
  7. /**  
  8.  * 用于把OutputStream 转化为 InputStream。  
  9.  * <p>  
  10.  * 使用CircilarBuffer 辅助类 <br>  
  11.  * 下载地址为 <A href="http://ostermiller.org/utils/download.html  
  12. http://ostermiller.org/utils/download.html<br>  
  13.  * 介绍地址为 http://ostermiller.org/utils/CircularBuffer.html  
  14.  * </p>  
  15.  *   
  16.  * @author 赵学庆 www.java2000.net  
  17.  */  
  18. public class Test3 {   
  19.   /**  
  20.    * @param args  
  21.    * @throws IOException  
  22.    */  
  23.   public static void main(String[] args) throws IOException {   
  24.     // 使用CircularByteBuffer   
  25.     final CircularByteBuffer cbb = new CircularByteBuffer();   
  26.     // 启动线程,让数据产生者单独运行   
  27.     new Thread(new Runnable() {   
  28.       public void run() {   
  29.         try {   
  30.           OutputStreamClass3.putDataOnOutputStream(cbb.getOutputStream());   
  31.         } catch (IOException e) {   
  32.           e.printStackTrace();   
  33.         }   
  34.       }   
  35.     }).start();   
  36.     // 数据使用者处理数据   
  37.     // 也可以使用线程来进行并行处理   
  38.     InputStreamClass3.processDataFromInputStream(cbb.getInputStream());   
  39.   }   
  40. }   
  41. class OutputStreamClass3 {   
  42.   public static void putDataOnOutputStream(OutputStream out) throws IOException {   
  43.     byte[] bs = new byte[2];   
  44.     for (int i = 0; i <= 100; i++) {   
  45.       bs[0] = (byte) i;   
  46.       bs[1] = (byte) (i + 1);   
  47.       // 测试写入字节数组   
  48.       out.write(bs);   
  49.       out.flush();   
  50.       try {   
  51.         // 等待0.1秒   
  52.         Thread.sleep(100);   
  53.       } catch (InterruptedException e) {   
  54.         e.printStackTrace();   
  55.       }   
  56.     }   
  57.   }   
  58. }   
  59. class InputStreamClass3 {   
  60.   public static void processDataFromInputStream(InputStream in) {   
  61.     byte[] bs = new byte[1024];   
  62.     int len;   
  63.     // 读取数据,并进行处理   
  64.     try {   
  65.       while ((len = in.read(bs)) != -1) {   
  66.         for (int i = 0; i < len; i++) {   
  67.           System.out.println(bs[i]);   
  68.         }   
  69.       }   
  70.     } catch (IOException e) {   
  71.       e.printStackTrace();   
  72.     }   
  73.   }   
  74. }  
 
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>
 * 
 * @author 赵学庆 www.java2000.net
 */
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();
    }
  }
}



此方法使用了一个类处理,代码更简洁,可以很方便的在缓冲处理全部数据的小数据量情况和多线程处理大数据量的不同情况切换

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值