Solved

Hangs at PipedOutputStream.write()

Posted on 2001-06-30
3
1,023 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
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 100 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now