• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Question about PipedOUtputStream / PipedInputStream

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Using pipedoutputstream and pipedinputstream is the best way of converting outputstream into inputstream, but I have got question about a pice of code, does it make sense?



I am not sure if I can just return 'in' like this, and it will still work. Could somebody say if this is ok? Thank you.
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course it should be InputStream and not inputstream but the general approach is sound.
 
Sheriff
Posts: 22800
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code will probably suffer from the biggest flaw in PipedInputStream / PipedOutputStream. From the Javadoc of PipedInputStream:

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.


That means that you are going to need to keep that thread alive until you close the input stream.

I have one simple way of solving this, using a helper class:
The thread will now block until either interrupted or until the returned InputStream is closed, thereby keeping it alive long enough to not cause a broken pipe. The only way a broken pipe can now occur is either the wait being interrupted, or the reading (main?) thread ending before all output is processed.
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anna Smalska wrote:Hello,
Using pipedoutputstream and pipedinputstream is the best way of converting outputstream into inputstream,


What do you mean by this? What do you want to do?
I can almost guarantee you want to use ByteArrayOutputStream / ByteArrayInputStream. Pipes are rarely used.
 
Rob Spoor
Sheriff
Posts: 22800
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The disadvantage of using those is that you'll need to store the entire byte[] into memory.
 
Masa Saito
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:The disadvantage of using those is that you'll need to store the entire byte[] into memory.


That is an interesting technique.
At the least, I'd test about buffering the streams and messing around with the buffer sizes.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic