code_junkie

Why doesn't more Java code use PipedInputStream / PipedOutputStream

Nov 13th, 2011
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. final LSOutput output; // XML stuff initialized elsewhere
  2. final LSSerializer serializer;
  3. final Document doc;
  4. // ...
  5. PostMethod post; // HttpClient post request
  6. final PipedOutputStream source = new PipedOutputStream();
  7. PipedInputStream sink = new PipedInputStream(source);
  8. // ...
  9. executor.execute(new Runnable() {
  10. public void run() {
  11. output.setByteStream(source);
  12. serializer.write(doc, output);
  13. try {
  14. source.close();
  15. } catch (IOException e) {
  16. throw new RuntimeException(e);
  17. }
  18. }});
  19.  
  20. post.setRequestEntity(new InputStreamRequestEntity(sink));
  21. int status = httpClient.executeMethod(post);
  22.  
  23. post.setRequestEntity(new RequestEntity()
  24. {
  25. public long getContentLength()
  26. {
  27. return 0-1;
  28. }
  29.  
  30. public String getContentType()
  31. {
  32. return "text/xml";
  33. }
  34.  
  35. public boolean isRepeatable()
  36. {
  37. return false;
  38. }
  39.  
  40. public void writeRequest(OutputStream out) throws IOException
  41. {
  42. output.setByteStream(out);
  43. serializer.write(doc, output);
  44. }
  45. });
  46.  
  47. import java.io.PipedInputStream;
  48. import java.io.PipedOutputStream;
  49.  
  50. import com.jcraft.jsch.Channel;
  51.  
  52. Channel channel;
  53. PipedInputStream channelInputStream = new PipedInputStream();
  54. PipedOutputStream channelOutputStream = new PipedOutputStream();
  55.  
  56. channel.setInputStream(new PipedInputStream(this.channelOutputStream));
  57. channel.setOutputStream(new PipedOutputStream(this.channelInputStream));
  58. channel.connect();
  59.  
  60. // Write to channelInputStream
  61. // Read from channelInputStream
  62.  
  63. channel.disconnect();
Add Comment
Please, Sign In to add comment