Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't more Java code use PipedInputStream / PipedOutputStream?

I've discovered this idiom recently, and I am wondering if there is something I am missing. I've never seen it used. Nearly all Java code I've worked with in the wild favors slurping data into a string or buffer, rather than something like this example (using HttpClient and XML APIs for example):

    final LSOutput output; // XML stuff initialized elsewhere     final LSSerializer serializer;     final Document doc;     // ...     PostMethod post; // HttpClient post request     final PipedOutputStream source = new PipedOutputStream();     PipedInputStream sink = new PipedInputStream(source);     // ...     executor.execute(new Runnable() {             public void run() {                 output.setByteStream(source);                 serializer.write(doc, output);                 try {                     source.close();                 } catch (IOException e) {                     throw new RuntimeException(e);                 }             }});      post.setRequestEntity(new InputStreamRequestEntity(sink));     int status = httpClient.executeMethod(post); 

That code uses a Unix-piping style technique to prevent multiple copies of the XML data being kept in memory. It uses the HTTP Post output stream and the DOM Load/Save API to serialize an XML Document as the content of the HTTP request. As far as I can tell it minimizes the use of memory with very little extra code (just the few lines for Runnable, PipedInputStream, and PipedOutputStream).

So, what's wrong with this idiom? If there's nothing wrong with this idiom, why haven't I seen it?

EDIT: to clarify, PipedInputStream and PipedOutputStream replace the boilerplate buffer-by-buffer copy that shows up everywhere, and they also allow you to process incoming data concurrently with writing out the processed data. They don't use OS pipes.

like image 230
Steven Huwig Avatar asked Jan 27 '09 16:01

Steven Huwig


People also ask

What does PipedOutputStream do in Java?

Creates a piped output stream connected to the specified piped input stream. Data bytes written to this stream will then be available as input from snk .

What is Pipedinputstream in Java?

The piped input stream contains a buffer, decoupling read operations from write operations, within limits. A pipe is said to be broken if a thread that was providing data bytes to the connected piped output stream is no longer alive.


1 Answers

From the Javadocs:

Typically, data is read from a PipedInputStream object by one thread and data is written to the corresponding PipedOutputStream by some other thread. Attempting to use both objects from a single thread is not recommended, as it may deadlock the thread.

This may partially explain why it is not more commonly used.

I'd assume another reason is that many developers do not understand its purpose / benefit.

like image 185
matt b Avatar answered Sep 23 '22 06:09

matt b