OutputStream怎么转换成InputStream
发布日期:2021-10-02 09:00:19
浏览次数:5
分类:技术文章
本文共 4282 字,大约阅读时间需要 14 分钟。
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())
- );
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);
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());
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());
// 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());
(架构师) 2009-03-18
- import java.io.*;
- public class Out2In {
- public static void main(String[] args) throws Exception {
- PipedInputStream pipeIn = new PipedInputStream();
- BufferedReader reader = new BufferedReader(new InputStreamReader(pipeIn));
- PipedOutputStream pipeOut = new PipedOutputStream(pipeIn);
- PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(pipeOut)));
- writer.println("Coming through the pipe...");
- writer.flush();
- System.out.println(reader.readLine()); // Coming through the pipe...
- reader.close();
- writer.close();
- }
- }
import java.io.*;public class Out2In { public static void main(String[] args) throws Exception { PipedInputStream pipeIn = new PipedInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(pipeIn)); PipedOutputStream pipeOut = new PipedOutputStream(pipeIn); PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(pipeOut))); writer.println("Coming through the pipe..."); writer.flush(); System.out.println(reader.readLine()); // Coming through the pipe... reader.close(); writer.close(); }}就这样。你的OutputStream是ZipOutputStream的话,在建立这个ZipOutputStream实例的时候底下接一个PipedOutputStream就行。
转载地址:https://blog.csdn.net/jiafu1115/article/details/7052684 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
做的很好,不错不错
[***.243.131.199]2024年04月08日 15时36分15秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
给Eclipse安装Google app engine插件
2019-04-26
java package 重命名时注意事项
2019-04-26
无法访问developer.android.com解决方法
2019-04-26
安卓Toolbar使用 Demo(Kotlin)
2019-04-26
使用JetPack中Navigation简化fragment切换
2019-04-26
MVVM中未生成binding类解决方法
2019-04-26
Rejecting re-init on previously-failed class java.lang.Class androidx.core.view.ViewCompat
2019-04-26
chrome插件下载github指定文件夹
2019-04-26
快速上传一个本地目录到github或码云
2019-04-26
安卓最好的异步任务执行框架:kotlin协程(coroutines)
2019-04-26
kotlin匿名类实现接口demo
2019-04-26
安卓多渠道打包
2019-04-26
安卓多渠道打包之指定渠道打包
2019-04-26
安卓辅助功能(无障碍)AccessibilityService实战介绍
2019-04-26
快速批量解决Kotlin包名和文件所在路径不一致问题
2019-04-26
ping命令
2019-04-26
2 倔
2019-04-26