OutputStream转换成InputStream

本文介绍了三种将 Java OutputStream 转换为 InputStream 的方法:使用字节数组缓冲数据、利用管道流(Pipes)以及采用循环缓冲区(Circular Buffers)。这些方法解决了在不同类间传递数据的问题,并提供了灵活的内存管理和多线程处理方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

If you have ever programmed using Java IO, you will quickly run into a situation in which a class creates data on an OutputStream and you need to send it to another class that expects to read the data from an input stream. You'll soon be asking the question, "How do I convert an OutputStream to an InputStream?"

Nowhere in Java will you find a OutpStreamToInputStreamConverter class. Luckily, there are several ways to go about this.

Method 1: Buffer the data using a byte array

The easiest method is to buffer the data using a byte array. The code will look something like this:


  ByteArrayOutputStream out  =   new  ByteArrayOutputStream();   class1.putDataOnOutputStream(out);   class2.processDataFromInputStream(      new  ByteArrayInputStream(out.toByteArray())   );

That's it! The OutputStream has been converted to an InputStream.

Method 2: Use pipes

The problem with the first method is that you must actually have enough memory to buffer the entire amount of data. You could buffer larger amounts of data by using the filesystem rather than memory, but either way there is a hard limit to the size of the data that can be handled. The solution is create a thread to produce the data to the PipedOutputStream. The current thread can then read the data as it comes in.


  PipedInputStream in  =   new  PipedInputStream();   PipedOUtputStream out  =   new  PipedOutputStream(in);    new  Thread(      new  Runnable() ... {        public   void  run() ... {         class1.putDataOnOutputStream(out);       }     }   ).start();   class2.processDataFromInputStream(in);

Method 3: Use Circular Buffers

The two piped streams in method two actually manage a hidden circular buffer. It is conceptually easier to use an explicit Circular Buffer . CircularBuffers offer several advantages:

  • One CircularBuffer class rather than two pipe classes.
  • It is easier to convert between the "buffer all data" and "extra threads" approaches.
  • You can change the buffer size rather than relying on the hard-coded 1k of buffer in the pipes.

Multiple Threaded Example of a Circular Buffer


  CircularByteBuffer cbb  =   new  CircularByteBuffer();    new  Thread(      new  Runnable() ... {        public   void  run() ... {         class1.putDataOnOutputStream(cbb.getOutputStream());       }     }   ).start();   class2.processDataFromInputStream(cbb.getInputStream());

Single Threaded Example of a Circular Buffer

  // buffer all data in a circular buffer of infinite size
  CircularByteBuffer cbb = new CircularByteBuffer(CircularByteBuffer.INFINITE_SIZE);
  class1.putDataOnOutputStream(cbb.getOutputStream());
  class2.processDataFromInputStream(cbb.getInputStream());
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值