\hypertarget{class_qore_1_1_stream_pipe}{}\doxysection{Qore\+::Stream\+Pipe Class Reference}
\label{class_qore_1_1_stream_pipe}\index{Qore::StreamPipe@{Qore::StreamPipe}}


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




{\ttfamily \#include $<$QC\+\_\+\+Stream\+Pipe.\+dox.\+h$>$}

\doxysubsection*{Public Member Functions}
\begin{DoxyCompactItemize}
\item 
\mbox{\hyperlink{class_qore_1_1_stream_pipe_a9276f076d1fdbf032bd13370626c5feb}{constructor}} (bool sync\+Close=\mbox{\hyperlink{group__boolean__constants_ga3ba2df91713a6249449347131ea526c3}{True}}, timeout timeout\+\_\+ms=-\/1, \mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}} buffer\+Size=4096)
\begin{DoxyCompactList}\small\item\em Creates the \mbox{\hyperlink{class_qore_1_1_stream_pipe}{Stream\+Pipe}}. \end{DoxyCompactList}\item 
\mbox{\hyperlink{class_qore_1_1_pipe_input_stream}{Pipe\+Input\+Stream}} \mbox{\hyperlink{class_qore_1_1_stream_pipe_a73047700e49178f9c1ec3015068f3db0}{get\+Input\+Stream}} ()
\begin{DoxyCompactList}\small\item\em Returns the input stream connected to the pipe. \end{DoxyCompactList}\item 
\mbox{\hyperlink{class_qore_1_1_pipe_output_stream}{Pipe\+Output\+Stream}} \mbox{\hyperlink{class_qore_1_1_stream_pipe_a24a7d09e306d09c62fea5c270af1f2db}{get\+Output\+Stream}} ()
\begin{DoxyCompactList}\small\item\em Returns the output stream connected to the pipe. \end{DoxyCompactList}\end{DoxyCompactItemize}


\doxysubsection{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 \mbox{\hyperlink{class_qore_1_1_output_stream_a2125e17612767b9636554b282638e883}{Output\+Stream\+::close()}} and all remaining data are read from the buffer. On the other hand, the \mbox{\hyperlink{class_qore_1_1_output_stream_a2125e17612767b9636554b282638e883}{Output\+Stream\+::close()}} method waits until \mbox{\hyperlink{class_qore_1_1_pipe_input_stream_ad8bd2da8c43c2ca2596c25216ea9a2c8}{Pipe\+Input\+Stream\+::finish\+Close()}} is called on the \mbox{\hyperlink{class_qore_1_1_stream_pipe}{Stream\+Pipe}} object which can be used to delay the main thread until all data are read from the pipe in the background thread.

\begin{DoxyParagraph}{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 \mbox{\hyperlink{class_qore_1_1_stream_pipe}{Stream\+Pipe}} instance since it keeps references to both streams. See the examples below for templates of correct usage and note that the \mbox{\hyperlink{class_qore_1_1_stream_pipe}{Stream\+Pipe}} instance goes out of scope as soon as possible.
\end{DoxyParagraph}
\begin{DoxyParagraph}{Example\+: pulling data from a background producer}

\begin{DoxyCode}{0}
\DoxyCodeLine{InputStream sub example() \{}
\DoxyCodeLine{    StreamPipe pipe(\textcolor{keyword}{False});         \textcolor{comment}{\# False indicates that the output stream's close() will not block}}
\DoxyCodeLine{    PipeOutputStream os = pipe.getOutputStream();}
\DoxyCodeLine{    background sub() \{}
\DoxyCodeLine{        \textcolor{keywordflow}{try} \{}
\DoxyCodeLine{            os.write(<01>);         \textcolor{comment}{\# produce data and write bytes to the pipe using os.write()}}
\DoxyCodeLine{            os.close();             \textcolor{comment}{\# causes the input stream's read() method to report the end of data}}
\DoxyCodeLine{        \} catch (hash ex) \{}
\DoxyCodeLine{            os.reportError(ex);     \textcolor{comment}{\# causes the input stream's read() method to throw the exception}}
\DoxyCodeLine{        \}}
\DoxyCodeLine{    \}();}
\DoxyCodeLine{    \textcolor{keywordflow}{return} pipe.getInputStream();}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{InputStream \textcolor{keywordflow}{is} = example();}
\DoxyCodeLine{*binary b;}
\DoxyCodeLine{\textcolor{keywordflow}{while} (b = is.read(4096)) \{}
\DoxyCodeLine{    \textcolor{comment}{\# process the data}}
\DoxyCodeLine{\}}

\end{DoxyCode}

\end{DoxyParagraph}
\begin{DoxyParagraph}{Example\+: pushing data to a background consumer}

\begin{DoxyCode}{0}
\DoxyCodeLine{OutputStream sub example() \{}
\DoxyCodeLine{    StreamPipe pipe();}
\DoxyCodeLine{    PipeInputStream \textcolor{keywordflow}{is} = pipe.getInputStream();}
\DoxyCodeLine{    background sub() \{}
\DoxyCodeLine{        \textcolor{keywordflow}{try} \{}
\DoxyCodeLine{            binary *b;}
\DoxyCodeLine{            \textcolor{keywordflow}{while} (b = is.read(4096)) \{}
\DoxyCodeLine{                \textcolor{comment}{\# process the data}}
\DoxyCodeLine{            \}}
\DoxyCodeLine{            \textcolor{comment}{\# finish processing / cleanup}}
\DoxyCodeLine{            is.finishClose();       \textcolor{comment}{\# wakeup PipeOutputStream::close()}}
\DoxyCodeLine{        \} catch (hash ex) \{}
\DoxyCodeLine{            is.reportError(ex);     \textcolor{comment}{\# causes the output stream's write() or close() methods to throw the exception}}
\DoxyCodeLine{        \}}
\DoxyCodeLine{    \}();}
\DoxyCodeLine{    \textcolor{keywordflow}{return} pipe.getOutputStream();}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{OutputStream os = example();}
\DoxyCodeLine{os.write(<01>);         \textcolor{comment}{\# produce data}}
\DoxyCodeLine{os.close();             \textcolor{comment}{\# waits until the consumer is done or reports an error}}

\end{DoxyCode}

\end{DoxyParagraph}
\begin{DoxySince}{Since}
Qore 0.\+8.\+13 
\end{DoxySince}


\doxysubsection{Member Function Documentation}
\mbox{\Hypertarget{class_qore_1_1_stream_pipe_a9276f076d1fdbf032bd13370626c5feb}\label{class_qore_1_1_stream_pipe_a9276f076d1fdbf032bd13370626c5feb}} 
\index{Qore::StreamPipe@{Qore::StreamPipe}!constructor@{constructor}}
\index{constructor@{constructor}!Qore::StreamPipe@{Qore::StreamPipe}}
\doxysubsubsection{\texorpdfstring{constructor()}{constructor()}}
{\footnotesize\ttfamily Qore\+::\+Stream\+Pipe\+::constructor (\begin{DoxyParamCaption}\item[{bool}]{sync\+Close = {\ttfamily \mbox{\hyperlink{group__boolean__constants_ga3ba2df91713a6249449347131ea526c3}{True}}},  }\item[{timeout}]{timeout\+\_\+ms = {\ttfamily -\/1},  }\item[{\mbox{\hyperlink{group__type__conversion__functions_ga2de8717e92c5f97ccc6511f6062d6502}{int}}}]{buffer\+Size = {\ttfamily 4096} }\end{DoxyParamCaption})}



Creates the \mbox{\hyperlink{class_qore_1_1_stream_pipe}{Stream\+Pipe}}. 


\begin{DoxyParams}{Parameters}
{\em sync\+Close} & if True, then the output stream\textquotesingle{}s close() method blocks until the input stream\textquotesingle{}s \mbox{\hyperlink{class_qore_1_1_pipe_input_stream_ad8bd2da8c43c2ca2596c25216ea9a2c8}{finish\+Close()}} method is called \\
\hline
{\em timeout\+\_\+ms} & a timeout period with a resolution of milliseconds (a \mbox{\hyperlink{basic_data_types_relative_dates}{relative date/time value}}; integer arguments will be assumed to be milliseconds); if not given or negative the operations will never time out \\
\hline
{\em buffer\+Size} & the size of the internal buffer \\
\hline
\end{DoxyParams}
\mbox{\Hypertarget{class_qore_1_1_stream_pipe_a73047700e49178f9c1ec3015068f3db0}\label{class_qore_1_1_stream_pipe_a73047700e49178f9c1ec3015068f3db0}} 
\index{Qore::StreamPipe@{Qore::StreamPipe}!getInputStream@{getInputStream}}
\index{getInputStream@{getInputStream}!Qore::StreamPipe@{Qore::StreamPipe}}
\doxysubsubsection{\texorpdfstring{getInputStream()}{getInputStream()}}
{\footnotesize\ttfamily \mbox{\hyperlink{class_qore_1_1_pipe_input_stream}{Pipe\+Input\+Stream}} Qore\+::\+Stream\+Pipe\+::get\+Input\+Stream (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Returns the input stream connected to the pipe. 

\begin{DoxyReturn}{Returns}
the input stream connected to the pipe 
\end{DoxyReturn}
\mbox{\Hypertarget{class_qore_1_1_stream_pipe_a24a7d09e306d09c62fea5c270af1f2db}\label{class_qore_1_1_stream_pipe_a24a7d09e306d09c62fea5c270af1f2db}} 
\index{Qore::StreamPipe@{Qore::StreamPipe}!getOutputStream@{getOutputStream}}
\index{getOutputStream@{getOutputStream}!Qore::StreamPipe@{Qore::StreamPipe}}
\doxysubsubsection{\texorpdfstring{getOutputStream()}{getOutputStream()}}
{\footnotesize\ttfamily \mbox{\hyperlink{class_qore_1_1_pipe_output_stream}{Pipe\+Output\+Stream}} Qore\+::\+Stream\+Pipe\+::get\+Output\+Stream (\begin{DoxyParamCaption}{ }\end{DoxyParamCaption})}



Returns the output stream connected to the pipe. 

\begin{DoxyReturn}{Returns}
the output stream connected to the pipe 
\end{DoxyReturn}
