Qore Programming Language Reference Manual  0.8.13.5
 All Classes Namespaces Functions Variables Groups Pages
Qore::StreamPipe Class Reference

This class provides a pair of streams connected through a buffer. More...

Public Member Functions

 constructor (bool syncClose=True, timeout timeout_ms=-1, int bufferSize=4096)
 Creates the StreamPipe. More...
 
PipeInputStream getInputStream ()
 Returns the input stream connected to the pipe. More...
 
PipeOutputStream getOutputStream ()
 Returns the output stream connected to the pipe. More...
 

Detailed Description

This class provides a pair of streams connected through a buffer.

The input stream provides the bytes that are written to the output stream. Data should be written to and read from the streams by different threads. The pipe contains a buffer (the size can be specified in the constructor) - the reading operations on the input stream are blocked if the buffer is empty and the writing operations on the output stream are blocked if the buffer is full. The input stream reports the end of the stream once the output stream is closed using OutputStream::close() and all remaining data are read from the buffer. On the other hand, the OutputStream::close() method waits until PipeInputStream::finishClose() is called on the StreamPipe object which can be used to delay the main thread until all data are read from the pipe in the background thread.

Broken pipe
A broken pipe is a situation when one of the streams ceases to exist (goes out of scope). In that case, a BROKEN-PIPE-ERROR is thrown by any operation on the other stream. This is particularly useful for unblocking the background producer (or consumer) when the other end stops consuming data before the end of the stream is reached (or stops producing data without closing the stream). For this to work as intended, it is important not to hold on to the StreamPipe instance since it keeps references to both streams. See the examples below for templates of correct usage and note that the StreamPipe instance goes out of scope as soon as possible.
Example: pulling data from a background producer
1 InputStream sub example() {
2  StreamPipe pipe(False); # False indicates that the output stream's close() will not block
3  PipeOutputStream os = pipe.getOutputStream();
4  background sub() {
5  try {
6  os.write(<01>); # produce data and write bytes to the pipe using os.write()
7  os.close(); # causes the input stream's read() method to report the end of data
8  } catch (hash ex) {
9  os.reportError(ex); # causes the input stream's read() method to throw the exception
10  }
11  }();
12  return pipe.getInputStream();
13 }
14 
15 InputStream is = example();
16 *binary b;
17 while (b = is.read(4096)) {
18  # process the data
19 }
Example: pushing data to a background consumer
1 OutputStream sub example() {
2  StreamPipe pipe();
3  PipeInputStream is = pipe.getInputStream();
4  background sub() {
5  try {
6  binary *b;
7  while (b = is.read(4096)) {
8  # process the data
9  }
10  # finish processing / cleanup
11  is.finishClose(); # wakeup PipeOutputStream::close()
12  } catch (hash ex) {
13  is.reportError(ex); # causes the output stream's write() or close() methods to throw the exception
14  }
15  }();
16  return pipe.getOutputStream();
17 }
18 
19 OutputStream os = example();
20 os.write(<01>); # produce data
21 os.close(); # waits until the consumer is done or reports an error
Since
Qore 0.8.13

Member Function Documentation

Qore::StreamPipe::constructor ( bool  syncClose = True,
timeout  timeout_ms = -1,
int  bufferSize = 4096 
)

Creates the StreamPipe.

Parameters
syncCloseif True, then the output stream's close() method blocks until the input stream's finishClose() method is called
timeout_msa timeout period with a resolution of milliseconds (a relative date/time value; integer arguments will be assumed to be milliseconds); if not given or negative the operations will never time out
bufferSizethe size of the internal buffer
PipeInputStream Qore::StreamPipe::getInputStream ( )

Returns the input stream connected to the pipe.

Returns
the input stream connected to the pipe
PipeOutputStream Qore::StreamPipe::getOutputStream ( )

Returns the output stream connected to the pipe.

Returns
the output stream connected to the pipe