![]() |
Code Examples
A repository of 155 code examples for BeepBeep
|
Read bytes from the standard input (. More...
Static Public Member Functions | |
static void | main (String[] args) throws ProcessorException, InterruptedException |
Read bytes from the standard input (.
). This program creates a StreamReader that reads from the standard input, and merely pushes whatever comes into to a Print processor that reprints it to the standard output. The chain of processors hence looks like this:
In this picture, the leftmost processor is the
. As you can see, it takes its input from the standard input; note how the input at its left does not have the same shape as regular BeepBeep pipes. This is to represent the fact that the processor does not receive events from a BeepBeep processor, but rather reads a system stream from the "outside world". In terms of BeepBeep processors, the
has an input arity of zero.
A similar comment can be done for the
processor. It receives input events, but as far as BeepBeep is concerned, does not produce any output events. Rather, it sends whatever it receives to the "outside world", this time through the
system stream. This is also what does the
processor in the PrintStdout example; however, the "stdout" output which was implicit in that example here is written explicitly in the drawing.
As with PrintStdout, you can compile this program as a runnable JAR file (e.g.
and try it out on the command line. Suppose you type:
$ java -jar read-stdin.jar
Nothing happens; however, if you type a few characters and press
, you should see the program reprint exactly what you typed (followed by a comma, as the
processor is instructed to¸ insert one between each event).
Let's try something slightly more interesting. If you are at a Unix-like command prompt, you can create a named pipe. Let us create one with the name
:
$ mkfifo mypipe
Now, let us launch
, by redirecting
into its standard input:
$ cat mypipe > java -jar read-stdin.jar
If you open another command prompt, you can then push characters into
; for example using the command
. Hence, if you type
$ echo "foo" > mypipe
you should see the string
being immediately printed at the other command prompt. This happens because
continuously polls its standard input for new characters, and pushes them down the processor chain whenever it receives some.
As you can see, the use of stream readers in BeepBeep, combined with system pipes on the command line, makes it possible for BeepBeep to interact with other programs from the command line, in exactly the same way Unix programs can be connected into each other.
This can be used to read a file. Instead of redirecting a named pipe to the program, one can use the
command with an actual filename:
$ cat somefile.txt > java -jar read-stdin.jar
This will have for effect of reading and pushing the entire contents of
into the processor chain. However, one can also read a file directly from BeepBeep; see the ReadFile example.
Definition at line 106 of file ReadStdin.java.