Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process InputStream

How can I "modify" an InputStream? I have a file as input, I would like to modify some variable and forward a new InputStream.

For example, the initial InputStream contains Hello ${var}. Then I want to "modify" this InputStream with var = "world", resulting an InputStream Hello world.

What's the best practice to do this? Thanks.

like image 482
Laurențiu Dascălu Avatar asked Jul 15 '10 13:07

Laurențiu Dascălu


People also ask

What is getInputStream () in Java?

This method returns the MIME type of the data in the form of a string. InputStream. getInputStream() This method returns an InputStream representing the data and throws the appropriate exception if it can not do so.

What is InputStream used for?

The InputStream is used to read data from a source and the OutputStream is used for writing data to a destination. Here is a hierarchy of classes to deal with Input and Output streams. The two important streams are FileInputStream and FileOutputStream, which would be discussed in this tutorial.

What does process waitFor () do?

waitFor. Causes the current thread to wait, if necessary, until the subprocess represented by this Process object has terminated, or the specified waiting time elapses.

What is Process class in Java?

Process provides control of native processes started by ProcessBuilder. start and Runtime. exec. The class provides methods for performing input from the process, performing output to the process, waiting for the process to complete, checking the exit status of the process, and destroying (killing) the process.


1 Answers

The java.io is one and all decorator pattern. Make use of it and create a class which extends InputStream (maybe DataInputStream or better, some Reader since you're actually interested in characters, not in bytes, but ala), add a constructor which takes the original InputStream and override the read() methods to read the original stream in, buffer it to a certain degree (e.g. from ${ until with firstnext }) and then determine the key and return the modified data instead.

If you call your new class FormattedInputStream or so, then you could return new FormattedInputStream(originalInputStream) to the enduser instead and have the enduser still just assign and use it as an InputStream.

like image 79
BalusC Avatar answered Sep 23 '22 21:09

BalusC