Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3936350
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T23:56:13+00:00 2026-05-19T23:56:13+00:00

I have an OutputStream that I needed to read from, and so I used

  • 0

I have an OutputStream that I needed to read from, and so I used the following (Groovy) code to get an InputStream reference to the data:

PipedInputStream inputStream = new PipedInputStream()
    PipedOutputStream outputStream = new PipedOutputStream(inputStream)
    new Thread(
            new Runnable() {
                public void run() {
                    // Some API method
                    putDataInOutputStream(outputStream)
                    outputStream.close()
                }
            }
    ).start()

handler.process(inputStream)

In this case, handler is some class that implements an interface which has this method:

public void process(InputStream stream);

The problem that came up in our new requirements was that there was some pre-processing on the stream, and therefore I need to read the stream at least twice in the handler.process() method. Here’s some example code from one implementation:

public void process(InputStream stream) {
    def bufferedStream = new BufferedInputStream(stream, 30 * 1048576)  // 30 MB
    bufferedStream.mark(Integer.MAX_VALUE)
    parseMetadata(bufferedStream)
    bufferedStream.reset()
    doTheThingYouDo(bufferedStream)
 }

I know that for some input I am not hitting the 30 MB limit or the Integer.MAX_VALUE buffer size. However, I’m always getting the following exception:

java.io.IOException: Stream closed

Is this even possible? I think the problem is the thread closing on the PipedOutputStream, but I don’t know how to prevent that or if perhaps I’m creating more problems by being a novice at Java Stream IO.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T23:56:14+00:00Added an answer on May 19, 2026 at 11:56 pm

    My best guess is that your parseMetadata somehow closed the stream. I’ve tried your scenario, and it works fine for me. In general, closing the OutputStream before your handler is done reading is not the problem, that’s exactly what the piped streams are for.

    Besides, given your situation, I would leave out the piping, and the additional thread. If you don’t mind having your entire stream in memory, you can do something like

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    
    fillTheOutput(out);
    
    ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
    
    pass1(in);
    in.reset();
    pass2(in);
    

    If you do mind having everything in memory, you’re in trouble anyway, since your BufferedInputStream does roughly the same thing.

    edit: Note that you can easily build a new ByteArrayInputStream based on the byte array, which is something you cannot do with regular streams.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I occasionally get an IOException from a Servlet I have that writes a byte
I have a code that does compression, encryption and checksum on a File Outputstream.
I have class A that derives from System.Data.Objects.DataClasses.EntityObject . When I try to serialize
I have the following code and I want to make the outputstream use utf-8.
I have this piece of code that adds a BOM UTF-8 to an outputstream
I have this method that downloads .csv files from yahoo finance and saves them
I have an ipod touch program that should receive messages from a server program
I have a problem with an J2ME client app, that sends data to an
I have an app that backs up data to the sd card. When the
I have several output listeners that are implementing OutputStream . It can be either

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.