`
eimhee
  • 浏览: 2115459 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

OutputStream转换成InputStream

    博客分类:
  • JAVA
阅读更多

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());
0
0
分享到:
评论

相关推荐

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

    NULL 博文链接:https://shihuan830619.iteye.com/blog/2094068

    InputStream与OutputStream及File间互转

    InputStream与OutputStream及File间互转

    PDF转换(DOC、DOCX、PPT 、PPTX、ODT)

    基于网上搜索版本修改的,不需要maven,独立...InputStream inStream: 入参 OutputStream outStream:出参 boolean showMessages: 是否展现处理过程消息 boolean closeStreamsWhenComplete: 完成转换后是否需要关闭流。

    json-lib-2.4-jdk15.jar下载

    4. 支持流:除了支持对象/JSON与字符串的相互转换,也支持与InputStream/OutputStream的相互转换,处理大数据时比较有用。 5. 无依赖:json-lib是一个独立的jar包,无任何第三方依赖,体积小,易于在项目中使用。 6. 高...

    AndroidHttpURLConnection发送GET请求

    读取返回的输入流中的数据,并将其中的数据转换为byte数组 使用InputStream 的read方法以及ByteArrayOutputStream的wirte方法 inputStream.read(buffer) outputStream.write(buffer, 0, len) outputStream....

    java IO章节的总结

    2、输出:Writer, OutputStream类型的子类(字符,字节) 决定使用哪个类以及它的构造进程的一般准则如下(不考虑特殊需要): 第一,考虑最原始的数据格式是什么:是否为文本? 第二,是输入还是输出? 第三,是否...

    mutator-io:一个小库来处理(大)数据转换

    它使用组合从源(inputStream)到目标(outputStream)的数据流 安装 npm i mutator-io 管子 Mutator I / O中“管道”的概念只是将递到 interface Pipe { name : string in : InputStream out : OutputStream ...

    Android开发人员不得不收集的代码

    input2OutputStream, output2InputStream : inputStream 与 outputStream 互转 inputStream2Bytes, bytes2InputStream : inputStream 与 byteArr 互转 outputStream2Bytes, bytes2OutputStream : outputStream 与 ...

    Java IO 体系.md

    - InputStream - OutputStream - 字符流对象 - Reader - Writer - 字节流与字符流的转换 - 新潮的 NIO - 缓冲区(Buffer) - 通道(Channel) - 示例:文件拷贝案例 - BIO 和 NIO 拷贝文件的区别 - 操作...

    Java 基础核心总结 +经典算法大全.rar

    基础 IO 类和相关方法InputStream OutputStream Reader 类Writer 类 InputStream 及其子类 OutputStream 及其子类Reader 及其子类Writer 及其子类 注解 关于 null 的几种处理方式大小写敏感 null 是任何引用类型的...

    day019-io笔记和代码.rar

    字节流 InputStream(抽象类) OutputStream(抽象类) 字符流 Reader (抽象类) Writer(抽象类) 2.字节流:(重点) * 使用场景: * 1.字节流处理除了文本、文字相关所有的流...

    Java之IO流学习总结

    OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,它的子类FileWriter 其实就是一个实现此功能的具体类(具体可以研究一SourceCode)。功能和使用和OutputStream 极其类似,后面会有它们的对应图。 6.字符...

    fifo-transform:将流转换为基于fifo排序的层

    Fifo转换在订购您的信息流时分层。 在发送不能保证fifo订购的传输之前使用。 const { createFifoWrapper , createFifoUnwrapper } = require ( 'fifo-transform' )inputStream . pipe ( createFifoWrapper ( ) ) . ...

    计算机网络实验报告 获取MAC socket通信

    // 转换成网络输出流 java.net.ServerSocket ss = new java.net.ServerSocket(9000); java.net.Socket sk = ss.accept(); //DataOutputStream 处理数据 数据的输出流 java.io.OutputStream os = new java.io...

    jsonrpc4j:Java的JSON-RPC

    流服务器( InputStream \ OutputStream ) HTTP服务器( HttpServletRequest \ HttpServletResponse ) Portlet服务器( ResourceRequest \ ResourceResponse ) 套接字服务器( StreamServer ) 与Spring框架...

    (超赞)JAVA精华之--深入JAVA API

    1.3.2 InputStream 的方法有: 1.3.3 OutputStream 的方法: 1.3.4 File 类 1.3.5 文件流的建立 1.3.6 缓冲区流 1.3.7 原始型数据流 1.3.8 对象流 1.3.9 字符流 InputStreamReader/OutputStreamWriter 1.3.10 随机...

    关于编码问题的深度解析

    我们知道I/O有四大家族InputStream,OutputStream,Writer,Reader前两个是基于字节的操作,后两个是基于字符的操作。由于我们平时是使用字符的方式进行记录信息,但由于计算机只认识0和1,所以I/O作为人机交互的...

    android xml文件操作

    public static Document parseForDoc(final InputStream is) throws SAXException, IOException, ParserConfigurationException, IllegalArgumentException { try { DocumentBuilderFactory factory = ...

    JAVA SE学习精华集锦

    1.3.2 InputStream 的方法有: 51 1.3.3 OutputStream 的方法: 51 1.3.4 File 类 51 1.3.5 文件流的建立 52 1.3.6 缓冲区流 53 1.3.7 原始型数据流 53 1.3.8 对象流 54 1.3.9 字符流 InputStreamReader/...

    JAVA基础课程讲义

    常用InputStream和OutputStream子类用法 150 FileInputStream和FileOutputStream 150 ByteArrayInutStream和ByteArrayOutputStream 154 BufferedInputStream和BufferedOutputStream 156 DataInputStream和...

Global site tag (gtag.js) - Google Analytics