Code Examples
A repository of 155 code examples for BeepBeep
io.ReadStdin Class Reference

Read bytes from the standard input (. More...

Static Public Member Functions

static void main (String[] args) throws ProcessorException, InterruptedException
 

Detailed Description

Read bytes from the standard input (.

stdin

). 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:

Processor graph

In this picture, the leftmost processor is the

StreamReader

. 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

StreamReader

has an input arity of zero.

A similar comment can be done for the

Print

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

stdout

system stream. This is also what does the

Print

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.

read-stdin.jar

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

Enter

, you should see the program reprint exactly what you typed (followed by a comma, as the

Print

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

mypipe

:

$ mkfifo mypipe

Now, let us launch

read-stdin.jar

, by redirecting

mypipe

into its standard input:

$ cat mypipe > java -jar read-stdin.jar

If you open another command prompt, you can then push characters into

mypipe

; for example using the command

echo

. Hence, if you type

$ echo "foo" > mypipe

you should see the string

foo

being immediately printed at the other command prompt. This happens because

read-stdin.jar

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

cat

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

somefile.txt

into the processor chain. However, one can also read a file directly from BeepBeep; see the ReadFile example.

Author
Sylvain Hallé Easy

Definition at line 106 of file ReadStdin.java.


The documentation for this class was generated from the following file: