Java OutputStream 转换为 InputStream
来源:互联网 发布:2017淘宝双11红包口令 编辑:程序博客网 时间:2024/05/24 01:46
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());See also: Converting a Writer to a Reader
- Java OutputStream 转换为 InputStream
- java中如何将OutputStream转换为InputStream
- inputstream 转换outputstream
- java inputStream ,outputStream
- Java.IO.InputStream-OutputStream
- java inputStream ,outputStream
- Java IO - InputStream&OutputStream
- java InputStream 和 OutputStream
- Java InputStream 和OutputStream
- Java流中的InputStream类和OutputStream类 转换流
- (转载)java中outputStream与inputStream的相互转换
- OutputStream怎么转换成InputStream
- java-InputStream与OutputStream类型
- InputStream、OutputStream、String的相互转换
- InputStream、OutputStream、String的相互转换
- InputStream 、OutputStream与String的转换
- InputStream、OutputStream、String的相互转换
- InputStream、OutputStream、String的相互转换
- 开发项目要注意(小议软件开发项目的分类)
- 祝csdn越半越好
- 【转】刷新aspx页面的六种方法
- 暂停8天啊!我哭!
- TCP实现P2P通信、TCP穿越NAT的方法、TCP打洞
- Java OutputStream 转换为 InputStream
- php函数实现虚拟主机url优化
- Power up C++ with STL: Part III (more on STL)
- Power up C++ with STL: Part IV (Implementing real algorithms)
- New World,New beginning
- M0n0wall 是什么?
- DDNS简介
- 基于Spring 2.0的Web应用设计
- 给初学者:用VB写外挂 ———— 序言