HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java Convert OutputStream to InputStream Example

Java Convert OutputStream to InputStream Example

In this Java example, we will learn to convert OutputStream to InputStream which we may need when we read data from one source which return an outputstream; and write/pass the data to other target which wants data in inputstream.

1. Convert OutputStream to InputStream using byte array

Here, we will utilize byte array to pass intermediate data.

OutputStream -> byte[] -> InputStream

E.g.

//OutputStream
ByteArrayOutputStream outStream = new ByteArrayOutputStream(new File(outputFile));

//byte[] -> InputStream
ByteArrayInputStream inStream = new ByteArrayInputStream( outStream.toByteArray() )

This is the simplest way to convert from OutputStream to InputStream in java.

2. Copy OutputStream to InputStream Using NIO Channels

Above approach is pretty much useful when you have limited data in OutputStream. If you have some big amount of data, then you want to do the conversion in realtime in form of stream where whole data is not stored in buffer – at any amount of time.

In other words, you will need to create a pipe approach where data is flowing from one end to other end – and no need to store complete data in buffer.

FileOutputStream fos = new FileOutputStream(new File(outputFile));
FileChannel outputChannel = fos.getChannel();

FileInputStream fis = new FileInputStream(inputFile);
FileChannel inputChannel = fis.getChannel();

//Transfer from output stream to input stream is happening here
outputChannel.transferTo(0, inputChannel.size(), inputChannel);


/Don't forget to close the streams and channels
inputChannel.close();
fis.close();

outputChannel.close();
fos.close();

Read More: Java NIO – Data Transfer between Channels

That’s all. If you have more effective and practical ways to convert outputstream to inputstream in java, please share with us.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. Aleksander Allen

    April 4, 2018

    There is no constructor for ByteArrayOutputStream that accepts File objects as formal arguments

  2. Marcus

    December 6, 2017

    this article title’s “Convert OutputStream to InputStream using byte array”, but in fact what is being shown is how to convert a FileOutputStream (a subtype of OutputStream); I wanted to get know it with an OutputStream itself

    • Lokesh Gupta

      December 7, 2017

      OutputStream is an abstract class. You must use one of its subtypes.

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces