You can use ObjectOutputStream

You write the object (obj in the code below) to the ObjectOutputStream, your object you want to convert to an input stream must implement Serializable.


    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);


    oos.writeObject(obj);

    oos.flush();
    oos.close();

    InputStream is = new ByteArrayInputStream(baos.toByteArray());
Answer from reevesy on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › ObjectInputStream.html
ObjectInputStream (Java Platform SE 8 )
April 21, 2026 - Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified.
🌐
Programiz
programiz.com › java-programming › objectinputstream
Java ObjectInputStream (With Examples)
The ObjectInputStream is mainly used to read data written by the ObjectOutputStream. Basically, the ObjectOutputStream converts Java objects into corresponding streams. This is known as serialization.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › InputStream.html
InputStream (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... AudioInputStream, ByteArrayInputStream, FileInputStream, FilterInputStream, InputStream, ObjectInputStream, PipedInputStream, SequenceInputStream, StringBufferInputStream · public abstract class InputStream extends Object implements Closeable · This abstract class is the superclass of all classes representing an input stream of bytes. Applications that need to define a subclass of InputStream must always provide a method that returns the next byte of input.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-inputstream-class-in-java
Java.io.InputStream Class in Java - GeeksforGeeks
March 28, 2024 - Syntax : public int read(byte[] arg) Parameters : arg : array whose number of bytes to be read Return : reads number of bytes and return to the buffer else, -1 i.e. when end of file is reached. Exception : -> IOException : If I/O error occurs. -> NullPointerException : if arg is null. Java.io.InputStream.reset() is invoked by mark() method.
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-objectinputstream-class-java-set-1
Java.io.ObjectInputStream Class in Java | Set 1 - GeeksforGeeks
July 6, 2023 - Serializable is basically a kind of marker for JVM(Java Virtual Machine) directing it to write out the state of Object present in Stream ... protected ObjectInputStream() : Help the sub-classes to not allocate private data used by ObjectInputStream, if they are re-implementing the ObjectInputStream. ObjectInputStream(InputStream source_input) : Create ObjectInputStream that read data from the 'source_input' Input Stream.
🌐
Jenkov
jenkov.com › tutorials › java-io › objectinputstream.html
Java IO: ObjectInputStream
September 3, 2015 - The Java ObjectInputStream class (java.io.ObjectInputStream) enables you to read Java objects from an InputStream instead of just raw bytes. You wrap an InputStream in a ObjectInputStream and then you can read objects from it. Of course the ...
Find elsewhere
🌐
Scaler
scaler.com › home › topics › input stream in java
Input Stream in Java - Scaler Topics
October 17, 2022 - To create an InputStream, we need to convert the String object to an InputStream object by using the class ByteArrayInputStream. The reason for using this is that it has an internal buffer present that can be used to read bytes from the stream.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › io › ObjectInputStream.html
ObjectInputStream (Java SE 17 & JDK 17)
April 21, 2026 - Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream. ... Creates an ObjectInputStream that reads from the specified InputStream.
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › io › ObjectInputStream.html
ObjectInputStream (Java Platform SE 7 )
Creates an ObjectInputStream that reads from the specified InputStream. A serialization stream header is read from the stream and verified.
🌐
Oracle
docs.oracle.com › en › java › javase › 22 › docs › api › java.base › java › io › ObjectInputStream.html
ObjectInputStream (Java SE 22 & JDK 22)
July 16, 2024 - Provide a way for subclasses that are completely reimplementing ObjectInputStream to not have to allocate private data just used by this implementation of ObjectInputStream. ... Creates an ObjectInputStream that reads from the specified InputStream.
🌐
Baeldung
baeldung.com › home › java › java string › java string to inputstream
Java String to InputStream | Baeldung
January 5, 2024 - In this quick tutorial, we’re going to look at how to convert a standard String to an InputStream using plain Java, Guava and the Apache Commons IO library.
🌐
Scientech Easy
scientecheasy.com › home › blog › objectinputstream in java
ObjectInputStream in Java - Scientech Easy
February 11, 2025 - All these methods of ObjectInputStream class can throw IOException if I/O errors occur while reading from the underlying InputStream. If the end of file is reached then EOFException can be thrown. Let’s take an example program where we will write student name, roll no, marks, percentage, and the current data to a file named objfile.txt and then will read these data using ObjectOutputStream and ObjectInputStream classes respectively. ... package javaProgram; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.Date; public class TestObjectInputStream { public static void main(String[] args) throws IOException, ClassNotFoundException { // Create an file output stream for file object.txt.
🌐
Baeldung
baeldung.com › home › java › java streams › convert inputstream to stream in java
Convert InputStream to Stream in Java | Baeldung
May 12, 2024 - In the example above, we create a BufferedReader object wrapped around the InputStream using an InputStreamReader. This allows us to read lines of text efficiently from the InputStream. Additionally, the lines() method of the BufferedReader returns a Stream<String> containing the lines read from the input.
🌐
Medium
medium.com › @praveenraovp01 › input-streams-in-java-613a953f730c
Input Streams in Java!. A stream in Java can be defined as a… | by PraveenLearnsStuff | Medium
May 6, 2023 - The ObjectInputStream class of the java.io package can be used to read objects that were previously written by ObjectOutputStream. It extends the InputStream abstract class.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 21 & JDK 21)
January 20, 2026 - Constructor for subclasses to call. ... Returns a new InputStream that reads no bytes. The returned stream is initially open. The stream is closed by calling the close() method.
🌐
Bureau of Economic Geology
beg.utexas.edu › lmod › agi.servlet › doc › detail › java › io › ObjectInputStream.html
java.io Class ObjectInputStream
It is the responsibility of the objects to handle any versioning that occurs. ... DataInput, ObjectOutputStream, Serializable, Object Serialization Specification, Section 3, Object Input Classes ... Create an ObjectInputStream that reads from the specified InputStream.