Code Examples
A repository of 155 code examples for BeepBeep
ReadStdin.java
1 /*
2  BeepBeep, an event stream processor
3  Copyright (C) 2008-2017 Sylvain Hallé
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published
7  by the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package io;
19 
20 import ca.uqac.lif.cep.Connector;
21 import ca.uqac.lif.cep.ProcessorException;
22 import ca.uqac.lif.cep.io.Print;
23 import ca.uqac.lif.cep.io.ReadStringStream;
24 import ca.uqac.lif.cep.tmf.Pump;
25 
26 /**
27  * Read bytes from the standard input ({@code stdin}). This program creates a
28  * {@link ca.uqac.lif.cep.io.StreamReader StreamReader} that reads from the
29  * standard input, and merely pushes whatever comes into to a
30  * {@link ca.uqac.lif.cep.io.Print Print}
31  * processor that reprints it to the standard output. The chain of processors
32  * hence looks like this:
33  * <p>
34  * <img
35  * src="./doc-files/io/ReadStdin.png"
36  * alt="Processor graph">
37  * <p>
38  * In this picture, the leftmost processor is the {@code StreamReader}. As you
39  * can see, it takes its input from the standard input; note how the input at
40  * its left does not have the same shape as regular BeepBeep pipes. This is
41  * to represent the fact that the processor does not receive events from a
42  * BeepBeep processor, but rather reads a <em>system stream</em> from the
43  * "outside world". In terms of BeepBeep processors, the {@code StreamReader}
44  * has an input arity of zero.
45  * <p>
46  * A similar comment can be done for the {@code Print} processor. It receives
47  * input events, but as far as BeepBeep is concerned, does not produce any
48  * output events. Rather, it sends whatever it receives to the "outside world",
49  * this time through the {@code stdout} system stream. This is also what does
50  * the {@code Print} processor in the {@link PrintStdout} example; however,
51  * the "stdout" output which was implicit in that example here is written
52  * explicitly in the drawing.
53  * <p>
54  * As with {@link PrintStdout}, you can compile this program as a runnable JAR
55  * file (e.g. {@code read-stdin.jar} and try it out on the command line.
56  * Suppose you type:
57  * <pre>
58  * $ java -jar read-stdin.jar
59  * </pre>
60  * Nothing happens; however, if you type a few characters and press
61  * {@code Enter}, you should see the program reprint exactly what you typed
62  * (followed by a comma, as the {@code Print} processor is instructed to¸
63  * insert one between each event).
64  * <p>
65  * Let's try something slightly more interesting. If you are at a Unix-like
66  * command prompt, you can create a
67  * <a href="https://en.wikipedia.org/wiki/Named_pipe">named pipe</a>. Let us
68  * create one with the name {@code mypipe}:
69  * <pre>
70  * $ mkfifo mypipe
71  * </pre>
72  * Now, let us launch {@code read-stdin.jar}, by redirecting {@code mypipe}
73  * into its standard input:
74  * <pre>
75  * $ cat mypipe &gt; java -jar read-stdin.jar
76  * </pre>
77  * If you open another command prompt, you can then push characters into
78  * {@code mypipe}; for example using the command {@code echo}. Hence, if you
79  * type
80  * <pre>
81  * $ echo "foo" &gt; mypipe
82  * </pre>
83  * you should see the string {@code foo} being immediately printed at the
84  * other command prompt. This happens because {@code read-stdin.jar}
85  * continuously polls its standard input for new characters, and pushes them
86  * down the processor chain whenever it receives some.
87  * <p>
88  * As you can see, the use of stream readers in BeepBeep, combined with
89  * system pipes on the command line, makes it possible for BeepBeep to
90  * interact with other programs from the command line, in exactly the same way
91  * Unix programs can be connected into each other.
92  * <p>
93  * This can be used to read a file. Instead of redirecting a named pipe to
94  * the program, one can use the {@code cat} command with an actual filename:
95  * <pre>
96  * $ cat somefile.txt &gt; java -jar read-stdin.jar
97  * </pre>
98  * This will have for effect of reading and pushing the entire contents of
99  * {@code somefile.txt} into the processor chain.
100  * However, one can also read a file directly from BeepBeep; see the
101  * {@link ReadFile} example.
102  *
103  * @author Sylvain Hallé
104  * @difficulty Easy
105  */
106 public class ReadStdin
107 {
108  public static void main(String[] args) throws ProcessorException, InterruptedException
109  {
110  /* We create a stream reader, and instruct it to read bytes from
111  * the standard input (represented in Java by the System.in object).
112  * We must tell the reader that it is not reading from a file, and
113  * that it should push whatever it receives. */
114  ///
115  ReadStringStream reader = new ReadStringStream(System.in);
116  reader.setIsFile(false);
117 
118  /* We connect the reader to a pump, which will periodically ask
119  * the reader to read new characters from the input stream */
120  Pump pump = new Pump(100);
121  Thread pump_thread = new Thread(pump);
122  Connector.connect(reader, pump);
123 
124  /* We connect the output of the stream reader to a Print processor,
125  * that will merely re-print to the standard output what was received
126  * from the standard input. */
127  Print print = new Print();
128  Connector.connect(pump, print);
129 
130  /* We need to call start() on the pump's thread so that it can start
131  * listening to its input stream. */
132  pump_thread.start();
133 
134  /* Since our program does nothing else, it would stop right away.
135  * We put here an idle loop. You can stop it by pressing Ctrl+C. */
136  while (true)
137  {
138  Thread.sleep(10000);
139  }
140  ///
141  }
142 
143 }
Read bytes from the standard input (.
Definition: ReadStdin.java:106