1

I need to pass an InputStream to an object which reads data which I previously stored into a File. I'm assessing a more efficient approach than storing eveything into a File and then passing the FileInputStream. I'd like to do it on the fly.

May someone appoint me the correct approach to do that?

The idea would be passing a Custom InputStream which innerly calls every line I was going to store in the file. I guess I need buffering. I discard storing everything in a String and then build an InputStream on it, as we are in the same situation, waiting to output all the lines before rereading them again.

Whimusical
  • 6,401
  • 11
  • 62
  • 105
  • your question is unclear..! Show us your attempt! – Anirudha Nov 27 '13 at 11:12
  • I don't want the solution, just understanding what is the best approach to do that, maybe I am forgetting some magical class in io package or something, or some apprach is strongly discouraged... – Whimusical Nov 27 '13 at 11:16

1 Answers1

2

There already is a stream for this. It's the PipedInputStream. You'll need to have one thread write to the PipedOutputStream, and pass the PipedInputStream to the object that will be reading in another thread.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Nice That is exactly what I was looking for! I'll let the question open in case it could exist an even higher level solution, but I doubt it, so probably I will accept this one in a while! – Whimusical Nov 27 '13 at 11:20
  • Damn, I found the SO answer: http://stackoverflow.com/questions/5778658/java-converting-from-outputstream-to-inputstream – Whimusical Nov 27 '13 at 11:40