Solved

Hangs at PipedOutputStream.write()

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
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 …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

630 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