How do I convert an OutputStream to an InputStream?

Source: Internet
Author: User

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 cocould 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 1 k 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());

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.