Solved

Hangs at PipedOutputStream.write()

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

685 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