\section{KBuffered\-IO Class Reference}
\label{classKBufferedIO}\index{KBufferedIO@{KBufferedIO}}
This abstract class implements basic functionality for buffered input/output. Buffered I/O.  


{\tt \#include $<$kbufferedio.h$>$}

Inheritance diagram for KBuffered\-IO:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=85pt]{classKBufferedIO__inherit__graph}
\end{center}
\end{figure}
Collaboration diagram for KBuffered\-IO:\begin{figure}[H]
\begin{center}
\leavevmode
\includegraphics[width=149pt]{classKBufferedIO__coll__graph}
\end{center}
\end{figure}
\subsection*{Public Types}
\begin{CompactItemize}
\item 
enum {\bf close\-Modes} \{ \par
{\bf avail\-Read} =  0x01, 
{\bf dirty\-Write} =  0x02, 
{\bf involuntary} =  0x10, 
{\bf delayed} =  0x20, 
\par
{\bf closed\-Now} =  0x40
 \}
\end{CompactItemize}
\subsection*{Signals}
\begin{CompactItemize}
\item 
void {\bf bytes\-Written} (int nbytes)
\item 
void {\bf closed} (int {\bf state})
\end{CompactItemize}
\subsection*{Public Member Functions}
\begin{CompactItemize}
\item 
virtual {\bf $\sim$KBuffered\-IO} ()
\item 
virtual void {\bf close\-Now} ()=0
\item 
virtual bool {\bf set\-Buffer\-Size} (int rsize, int wsize=-2)
\item 
virtual int {\bf bytes\-Available} () const 
\item 
virtual int {\bf wait\-For\-More} (int msec)=0
\item 
virtual int {\bf bytes\-To\-Write} () const 
\item 
virtual bool {\bf can\-Read\-Line} () const 
\item 
virtual int {\bf peek\-Block} (char $\ast$data, uint maxlen)=0
\item 
virtual int {\bf unread\-Block} (const char $\ast$data, uint len)
\end{CompactItemize}
\subsection*{Protected Member Functions}
\begin{CompactItemize}
\item 
virtual unsigned {\bf consume\-Read\-Buffer} (unsigned nbytes, char $\ast$destbuffer, bool discard=true)
\item 
virtual void {\bf consume\-Write\-Buffer} (unsigned nbytes)
\item 
virtual unsigned {\bf feed\-Read\-Buffer} (unsigned nbytes, const char $\ast$buffer, bool at\-Beginning=false)
\item 
virtual unsigned {\bf feed\-Write\-Buffer} (unsigned nbytes, const char $\ast$buffer)
\item 
virtual unsigned {\bf read\-Buffer\-Size} () const 
\item 
virtual unsigned {\bf write\-Buffer\-Size} () const 
\item 
virtual void {\bf virtual\_\-hook} (int id, void $\ast$data)\label{classKBufferedIO_b7}

\end{CompactItemize}
\subsection*{Protected Attributes}
\begin{CompactItemize}
\item 
{\bf QPtr\-List}$<$ {\bf QByte\-Array} $>$ {\bf in\-Buf}
\item 
{\bf QPtr\-List}$<$ {\bf QByte\-Array} $>$ {\bf out\-Buf}
\item 
unsigned {\bf in\-Buf\-Index}\label{classKBufferedIO_p2}

\item 
unsigned {\bf out\-Buf\-Index}\label{classKBufferedIO_p3}

\end{CompactItemize}


\subsection{Detailed Description}
This abstract class implements basic functionality for buffered input/output. Buffered I/O. 

Through the available methods, you can find out how many bytes are available for reading, how many are still unsent and you can peek at the buffered data.

This class was intentionally written to resemble {\bf QSocket}, because KExtended\-Socket is a subclass of this one. This is so that applications written using QSocket's buffering characteristics will be more easily ported to the more powerful KExtended\-Socket class.

KBuffered\-IO already provides a powerful internal buffering algorithm. However, this does not include the I/O itself, which must be implemented in derived classes. Thus, to implement a class that does some I/O, you must override, in addition to the pure virtual {\bf QIODevice} methods, these two: \begin{itemize}
\item {\bf close\-Now()}{\rm (p.\,\pageref{classKBufferedIO_a1})} \item {\bf wait\-For\-More()}{\rm (p.\,\pageref{classKBufferedIO_a4})}\end{itemize}
If your derived class reimplements the buffering algorithm, you must then decide which buffering functions to override. For instance, you may want to change the protected functions like {\bf feed\-Read\-Buffer()}{\rm (p.\,\pageref{classKBufferedIO_b3})} and {\bf consume\-Read\-Buffer()}{\rm (p.\,\pageref{classKBufferedIO_b1})}.

\begin{Desc}
\item[Author:]Thiago Macieira $<${\tt thiagom@mail.com}$>$ \end{Desc}




Definition at line 56 of file kbufferedio.h.

\subsection{Member Enumeration Documentation}
\index{KBufferedIO@{KBuffered\-IO}!closeModes@{closeModes}}
\index{closeModes@{closeModes}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}enum {\bf KBuffered\-IO::close\-Modes}}\label{classKBufferedIO_w5}


The modes for {\bf closed()}{\rm (p.\,\pageref{classKBufferedIO_l1})} signal. 



Definition at line 68 of file kbufferedio.h.

\subsection{Constructor \& Destructor Documentation}
\index{KBufferedIO@{KBuffered\-IO}!~KBufferedIO@{$\sim$KBufferedIO}}
\index{~KBufferedIO@{$\sim$KBufferedIO}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}KBuffered\-IO::$\sim${\bf KBuffered\-IO} ()\hspace{0.3cm}{\tt  [virtual]}}\label{classKBufferedIO_a0}


Destroys this class. 

The flushing of the buffers is implementation dependant. The default implementation discards the contents 

Definition at line 95 of file kbufferedio.cpp.

\subsection{Member Function Documentation}
\index{KBufferedIO@{KBuffered\-IO}!closeNow@{closeNow}}
\index{closeNow@{closeNow}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}virtual void KBuffered\-IO::close\-Now ()\hspace{0.3cm}{\tt  [pure virtual]}}\label{classKBufferedIO_a1}


Closes the stream now, discarding the contents of the write buffer. 

That is, we won't try to flush that buffer before closing. If you want that buffer to be flushed, you can call {\bf QIODevice::flush()}, which is blocking, and then close\-Now, or you can call {\bf QIODevice::close()} for a delayed close. \index{KBufferedIO@{KBuffered\-IO}!setBufferSize@{setBufferSize}}
\index{setBufferSize@{setBufferSize}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}bool KBuffered\-IO::set\-Buffer\-Size (int {\em rsize}, int {\em wsize} = {\tt -2})\hspace{0.3cm}{\tt  [virtual]}}\label{classKBufferedIO_a2}


Sets the internal buffer size to value. 

Not all implementations support this.

The parameters may be 0 to make the class unbuffered or -1 to let the class choose the size (which may be unlimited) or -2 to leave the buffer size untouched.

Note that setting the write buffer size to any value smaller than the current size of the buffer will force it to flush first, which can make this call blocking.

The default implementation does not support setting the buffer sizes. You can only call this function with values -1 for \char`\"{}don't care\char`\"{} or -2 for \char`\"{}unchanged\char`\"{} \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em rsize}]the size of the read buffer \item[{\em wsize}]the size of the write buffer \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]true if setting both was ok. If false is returned, the buffers were left unchanged. \end{Desc}


Definition at line 102 of file kbufferedio.cpp.\index{KBufferedIO@{KBuffered\-IO}!bytesAvailable@{bytesAvailable}}
\index{bytesAvailable@{bytesAvailable}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KBuffered\-IO::bytes\-Available () const\hspace{0.3cm}{\tt  [virtual]}}\label{classKBufferedIO_a3}


Returns the number of bytes available for reading in the read buffer. 

\begin{Desc}
\item[Returns:]the number of bytes available for reading \end{Desc}


Definition at line 112 of file kbufferedio.cpp.

References read\-Buffer\-Size().

Referenced by can\-Read\-Line().\index{KBufferedIO@{KBuffered\-IO}!waitForMore@{waitForMore}}
\index{waitForMore@{waitForMore}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}virtual int KBuffered\-IO::wait\-For\-More (int {\em msec})\hspace{0.3cm}{\tt  [pure virtual]}}\label{classKBufferedIO_a4}


Waits for more data to be available and returns the amount of available data then. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em msec}]number of milliseconds to wait, -1 to wait forever \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]-1 if we cannot wait (e.g., that doesn't make sense in this stream) \end{Desc}
\index{KBufferedIO@{KBuffered\-IO}!bytesToWrite@{bytesToWrite}}
\index{bytesToWrite@{bytesToWrite}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KBuffered\-IO::bytes\-To\-Write () const\hspace{0.3cm}{\tt  [virtual]}}\label{classKBufferedIO_a5}


Returns the number of bytes yet to write, still in the write buffer. 

\begin{Desc}
\item[Returns:]the number of unwritten bytes in the write buffer \end{Desc}


Definition at line 117 of file kbufferedio.cpp.

References write\-Buffer\-Size().\index{KBufferedIO@{KBuffered\-IO}!canReadLine@{canReadLine}}
\index{canReadLine@{canReadLine}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}bool KBuffered\-IO::can\-Read\-Line () const\hspace{0.3cm}{\tt  [virtual]}}\label{classKBufferedIO_a6}


Checks whether there is enough data in the buffer to read a line. 

The default implementation reads directly from in\-Buf, so if your implementation changes the meaning of that member, then you must override this function. \begin{Desc}
\item[Returns:]true when there is enough data in the buffer to read a line \end{Desc}


Definition at line 123 of file kbufferedio.cpp.

References bytes\-Available(), QPtr\-List::first(), and QPtr\-List::next().\index{KBufferedIO@{KBuffered\-IO}!peekBlock@{peekBlock}}
\index{peekBlock@{peekBlock}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}virtual int KBuffered\-IO::peek\-Block (char $\ast$ {\em data}, uint {\em maxlen})\hspace{0.3cm}{\tt  [pure virtual]}}\label{classKBufferedIO_a7}


Reads into the user buffer at most maxlen bytes, but does not consume that data from the read buffer. 

This is useful to check whether we already have the needed data to process something.

This function may want to try and read more data from the system provided it won't block.

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em data}]the user buffer pointer, at least maxlen bytes long \item[{\em maxlen}]the maximum length to be peeked \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]the number of bytes actually copied. \end{Desc}
\index{KBufferedIO@{KBuffered\-IO}!unreadBlock@{unreadBlock}}
\index{unreadBlock@{unreadBlock}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}int KBuffered\-IO::unread\-Block (const char $\ast$ {\em data}, uint {\em len})\hspace{0.3cm}{\tt  [virtual]}}\label{classKBufferedIO_a8}


Unreads some data. 

That is, write the data to the beginning of the read buffer, so that next calls to read\-Block or peek\-Block will see this data instead.

Note not all devices implement this since this could mean a semantic problem. For instance, sockets are sequential devices, so they won't accept unreading. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em data}]the data to be unread \item[{\em len}]the size of the data \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]the number of bytes actually unread \end{Desc}


Definition at line 153 of file kbufferedio.cpp.

References feed\-Read\-Buffer().\index{KBufferedIO@{KBuffered\-IO}!bytesWritten@{bytesWritten}}
\index{bytesWritten@{bytesWritten}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KBuffered\-IO::bytes\-Written (int {\em nbytes})\hspace{0.3cm}{\tt  [signal]}}\label{classKBufferedIO_l0}


This signal gets sent whenever bytes are written from the buffer. 

\begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em nbytes}]the number of bytes sent. \end{description}
\end{Desc}
\index{KBufferedIO@{KBuffered\-IO}!closed@{closed}}
\index{closed@{closed}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KBuffered\-IO::closed (int {\em state})\hspace{0.3cm}{\tt  [signal]}}\label{classKBufferedIO_l1}


This signal gets sent when the stream is closed. 

The {\tt state} parameter will give the current state, in OR-ed bits: \begin{itemize}
\item avail\-Read: read buffer contains data to be read \item dirty\-Write: write buffer wasn't empty when the stream closed \item involuntary: the stream wasn't closed due to user request (i.e., call to close). Probably remote end closed it \item delayed: the stream was closed voluntarily by the user, but it happened only after the write buffer was emptied \item closed\-Now: the stream was closed voluntarily by the user, by explicitly calling close\-Now, which means the write buffer's contents may have been discarded \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em state}]the state (see function description) \end{description}
\end{Desc}
\end{itemize}
\index{KBufferedIO@{KBuffered\-IO}!consumeReadBuffer@{consumeReadBuffer}}
\index{consumeReadBuffer@{consumeReadBuffer}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}unsigned KBuffered\-IO::consume\-Read\-Buffer (unsigned {\em nbytes}, char $\ast$ {\em destbuffer}, bool {\em discard} = {\tt true})\hspace{0.3cm}{\tt  [protected, virtual]}}\label{classKBufferedIO_b1}


Consumes data from the input buffer. 

That is, this will copy the data stored in the input (read) buffer into the given {\tt destbuffer}, as much as {\tt nbytes}. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em nbytes}]the maximum amount of bytes to copy into the buffer \item[{\em destbuffer}]the destination buffer into which to copy the data \item[{\em discard}]whether to discard the copied data after the operation \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]the real amount of data copied. If it is less than nbytes, then all the buffer was copied. \end{Desc}


Definition at line 162 of file kbufferedio.cpp.

References QPtr\-List::first(), in\-Buf, QPtr\-List::next(), read\-Buffer\-Size(), and QPtr\-List::remove().\index{KBufferedIO@{KBuffered\-IO}!consumeWriteBuffer@{consumeWriteBuffer}}
\index{consumeWriteBuffer@{consumeWriteBuffer}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}void KBuffered\-IO::consume\-Write\-Buffer (unsigned {\em nbytes})\hspace{0.3cm}{\tt  [protected, virtual]}}\label{classKBufferedIO_b2}


Consumes data from the output buffer. 

Since this is called whenever we managed to send data out the wire, we can only discard this amount from the buffer. There is no copying and no \char`\"{}peeking\char`\"{} for the output buffer.

Note this function should be called AFTER the data was sent. After it is called, the data is no longer available in the buffer. And don't pass wrong nbytes values. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em nbytes}]the amount of bytes to discard \end{description}
\end{Desc}


Definition at line 212 of file kbufferedio.cpp.

References QPtr\-List::current(), QPtr\-List::first(), out\-Buf, QPtr\-List::remove(), and QIODevice::size().\index{KBufferedIO@{KBuffered\-IO}!feedReadBuffer@{feedReadBuffer}}
\index{feedReadBuffer@{feedReadBuffer}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}unsigned KBuffered\-IO::feed\-Read\-Buffer (unsigned {\em nbytes}, const char $\ast$ {\em buffer}, bool {\em at\-Beginning} = {\tt false})\hspace{0.3cm}{\tt  [protected, virtual]}}\label{classKBufferedIO_b3}


Feeds data into the input buffer. 

This happens when we detected available data in the device and read it.

The data will be appended to the buffer or inserted at the beginning, depending on whether {\tt at\-Beginning} is set or not. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em nbytes}]the number of bytes in the buffer \item[{\em buffer}]the data that was read \item[{\em at\-Beginning}]whether to append or insert at the beginning \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]the number of bytes that have been appended \end{Desc}


Definition at line 241 of file kbufferedio.cpp.

References QPtr\-List::append(), in\-Buf, and QPtr\-List::prepend().

Referenced by unread\-Block().\index{KBufferedIO@{KBuffered\-IO}!feedWriteBuffer@{feedWriteBuffer}}
\index{feedWriteBuffer@{feedWriteBuffer}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}unsigned KBuffered\-IO::feed\-Write\-Buffer (unsigned {\em nbytes}, const char $\ast$ {\em buffer})\hspace{0.3cm}{\tt  [protected, virtual]}}\label{classKBufferedIO_b4}


Feeds data into the output buffer. 

This happens when the user told us to write some data. The data will be appended to the buffer. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em nbytes}]the number of bytes in the buffer \item[{\em buffer}]the data that is to be written \end{description}
\end{Desc}
\begin{Desc}
\item[Returns:]the number of bytes that have been appended \end{Desc}


Definition at line 257 of file kbufferedio.cpp.

References QPtr\-List::append(), and out\-Buf.\index{KBufferedIO@{KBuffered\-IO}!readBufferSize@{readBufferSize}}
\index{readBufferSize@{readBufferSize}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}unsigned KBuffered\-IO::read\-Buffer\-Size () const\hspace{0.3cm}{\tt  [protected, virtual]}}\label{classKBufferedIO_b5}


Returns the number of bytes in the read buffer. 

\begin{Desc}
\item[Returns:]the size of the read buffer in bytes \end{Desc}


Definition at line 268 of file kbufferedio.cpp.

References QPtr\-List::first().

Referenced by bytes\-Available(), and consume\-Read\-Buffer().\index{KBufferedIO@{KBuffered\-IO}!writeBufferSize@{writeBufferSize}}
\index{writeBufferSize@{writeBufferSize}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}unsigned KBuffered\-IO::write\-Buffer\-Size () const\hspace{0.3cm}{\tt  [protected, virtual]}}\label{classKBufferedIO_b6}


Returns the number of bytes in the write buffer. 

\begin{Desc}
\item[Returns:]the size of the write buffer in bytes \end{Desc}


Definition at line 281 of file kbufferedio.cpp.

Referenced by bytes\-To\-Write().

\subsection{Member Data Documentation}
\index{KBufferedIO@{KBuffered\-IO}!inBuf@{inBuf}}
\index{inBuf@{inBuf}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf QPtr\-List}$<${\bf QByte\-Array}$>$ {\bf KBuffered\-IO::in\-Buf}\hspace{0.3cm}{\tt  [protected]}}\label{classKBufferedIO_p0}


For an explanation on how this buffer work, please refer to the comments at the top of {\bf kbufferedio.cpp}{\rm (p.\,\pageref{kbufferedio_8cpp})}. 



Definition at line 212 of file kbufferedio.h.

Referenced by consume\-Read\-Buffer(), and feed\-Read\-Buffer().\index{KBufferedIO@{KBuffered\-IO}!outBuf@{outBuf}}
\index{outBuf@{outBuf}!KBufferedIO@{KBuffered\-IO}}
\subsubsection{\setlength{\rightskip}{0pt plus 5cm}{\bf QPtr\-List}$<${\bf QByte\-Array}$>$ {\bf KBuffered\-IO::out\-Buf}\hspace{0.3cm}{\tt  [protected]}}\label{classKBufferedIO_p1}


For an explanation on how this buffer work, please refer to the comments at the top of {\bf kbufferedio.cpp}{\rm (p.\,\pageref{kbufferedio_8cpp})}. 



Definition at line 218 of file kbufferedio.h.

Referenced by consume\-Write\-Buffer(), and feed\-Write\-Buffer().

The documentation for this class was generated from the following files:\begin{CompactItemize}
\item 
kbufferedio.h\item 
kbufferedio.cpp\end{CompactItemize}
