Solved

Hangs at PipedOutputStream.write()

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 96
Error in @AspectJ Based AOP with Spring 2 13
runtime exception 2 27
printf performancy 11 35
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
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 learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
The viewer will learn how to implement Singleton Design Pattern in Java.

776 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