?
Solved

Hangs at PipedOutputStream.write()

Posted on 2001-06-30
3
Medium Priority
?
1,150 Views
Last Modified: 2012-08-13
HI,

Here is the program which I use for writing the data into a pipedoutputstream. This stream is connected to a pipedinputstream. I can write strings upto length of 1024 into this outputstream. But I write more than that, it will hang at write() statement.

NOTE : I pass pipedinputstream to a different function which needs inputstream.

/****************************************/
import java.io.*;
import java.util.*;

public class pipe
{
  public static void main(String[] args) throws Exception
  {
    String str = "";
    int count = Integer.parseInt(args[0]);
    for(int i=0; i<count; i++)
      str = str + "a";

    PipedOutputStream out = new PipedOutputStream();
    PipedInputStream in = new PipedInputStream(out);
    out.write(str.getBytes(), 0, str.length());
    out.close();

    System.out.println("DONE");
  }
}

/***************************************/

Here is how you can run the program
$java pipe 1000    //it works fine
$java pipe 1025    //This will hang.

Is this a bug of pipedoutpustream in java1.2? Does anybody know how to make this work?

Thanks,
Prasanna
0
Comment
Question by:Prasanna_Hebbar
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 92

Expert Comment

by:objects
ID: 6241538
The PipedInputStream classes use an internal buffer of 1024 bytes.  This buffer is used to store characters written to it, before they are read from the input stream. So as soon as it contains more than 1024 characters awaiting to be read it will block.
This cannot be changed and is a limitation of these classes.

Try only writing to the PipedOutputStream with small packets and ensure that whatevers reading from the PipedInputStreasm is keeping up with the flow.
If this is not possible then you'll probably have to write your own implementation of the piped stream classes.
0
 
LVL 7

Accepted Solution

by:
Igor Bazarny earned 400 total points
ID: 6241948
Hi,

In fact, there is a way to increase buffer size. <code>buffer</code> field is protected in PipedInputStream, you can subclass it and create greater buffer in constructor. Frankly, I would expect that both ends are accessed simultaneously by different threads. In such case buffer size would not be a big problem--producer would wait while consumer will free some room and vice versa--consumer would wait for producer when input is not available. If you just need some buffer, use ByteArrayOutputStream and ByteArrayInputStream--there will be no size limitation.

Regards,
Igor Bazarny,
Brainbench MVP for Java 1
www.brainbench.com
0
 
LVL 2

Author Comment

by:Prasanna_Hebbar
ID: 6243297
Objects & bazarny, thanks to both of you. I award the points to bazarny, since he gave me a alternative solution to my problem. ie use ByteArrayInputStream.

Thanks
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question