class KBufferedIO

Buffered I/O. More...

Contains pure virtuals
Definition#include <kbufferedio.h>
InheritsKAsyncIO [public ]
Inherited byKExtendedSocket
List of all Methods
Annotated List
Files
Globals
Hierarchy
Index

Public Types

Public Methods

Signals

Protected Methods

Protected Members


Detailed Description

This abstract class implements basic functionality for buffered input/output.

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 QSocket, because KExtendedSocket 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 KExtendedSocket class.

KBufferedIO 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 QIODevice methods, these two:

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 feedReadBuffer and consumeReadBuffer.

 KBufferedIO ()

KBufferedIO

[protected]

enum closeModes { availRead = 0x01, dirtyWrite = 0x02, involuntary = 0x10, delayed = 0x20, closedNow = 0x40 }

closeModes

The modes for closed() signal

 ~KBufferedIO ()

~KBufferedIO

[virtual]

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

void  closeNow ()

closeNow

[pure virtual]

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 flush, which is blocking, and then closeNow, or you can call close for a delayed close.

bool  setBufferSize (int rsize, int wsize = -2)

setBufferSize

[virtual]

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.

Returns true if setting both was ok. If false is returned, the buffers were left unchanged.

The default implementation does not support setting the buffer sizes. You can only call this function with values -1 for "don't care" or -2 for "unchanged"

Parameters:
rsizethe size of the read buffer
wsizethe size of the write buffer

int  bytesAvailable ()

bytesAvailable

[const virtual]

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

int  waitForMore (int msec)

waitForMore

[pure virtual]

Waits for more data to be available and returns the amount of available data then. Returns -1 if we cannot wait (e.g., that doesn't make sense in this stream)

Parameters:
msecnumber of milliseconds to wait, -1 to wait forever

int  bytesToWrite ()

bytesToWrite

[const virtual]

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

bool  canReadLine ()

canReadLine

[const virtual]

Returns true when there is enough data in the buffer to read a line

The default implementation reads directly from inBuf, so if your implementation changes the meaning of that member, then you must override this function.

int  peekBlock (char *data, uint maxlen)

peekBlock

[pure virtual]

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.

Returns the number of bytes actually copied.

Parameters:
datathe user buffer pointer, at least maxlen bytes long
maxlenthe maximum length to be peeked

int  unreadBlock (const char *data, uint len)

unreadBlock

[virtual]

Unreads some data. That is, write the data to the beginning of the read buffer, so that next calls to readBlock or peekBlock 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.

Parameters:
datathe data to be unread
sizethe size of the data

void  bytesWritten (int nbytes)

bytesWritten

[signal]

This signal gets sent whenever bytes are written from the buffer. The nbytes parameter contains the number of bytes sent.

void  closed (int state)

closed

[signal]

This signal gets sent when the stream is closed. The state parameter will give the current state, in OR-ed bits:

QPtrList<QByteArray> inBuf

inBuf

[protected]

QPtrList<QByteArray> outBuf

outBuf

[protected]

unsigned inBufIndex

inBufIndex

[protected]

unsigned outBufIndex

outBufIndex

[protected]

For an explanation on how these buffers work, please refer to the comments at the top of kbufferedio.cpp

unsigned  consumeReadBuffer (unsigned nbytes, char *destbuffer, bool discard = true)

consumeReadBuffer

[protected virtual]

Consumes data from the input buffer. That is, this will copy the data stored in the input (read) buffer into the given destbuffer, as much as nbytes. The return value is the real amount of data copied. If it is less than nbytes, then all the buffer was copied.

Parameters:
nbytesthe maximum amount of bytes to copy into the buffer
destbufferthe destination buffer into which to copy the data
discardwhether to discard the copied data after the operation

void  consumeWriteBuffer (unsigned nbytes)

consumeWriteBuffer

[protected virtual]

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 "peeking" 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.

Parameters:
nbytesthe amount of bytes to discard

unsigned  feedReadBuffer (unsigned nbytes, const char *buffer, bool atBeginning = false)

feedReadBuffer

[protected virtual]

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 atBeginning is set or not.

Parameters:
nbytesthe number of bytes in the buffer
bufferthe data that was read
atBeginningwhether to append or insert at the beginning

unsigned  feedWriteBuffer (unsigned nbytes, const char *buffer)

feedWriteBuffer

[protected virtual]

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.

Parameters:
nbytesthe number of bytes in the buffer
bufferthe data that is to be written

unsigned  readBufferSize ()

readBufferSize

[protected const virtual]

Returns the number of bytes in the read buffer

unsigned  writeBufferSize ()

writeBufferSize

[protected const virtual]

Returns the number of bytes in the write buffer

void  virtual_hook ( int id, void* data )

virtual_hook

[protected virtual]

Reimplemented from KAsyncIO.