Public Member Functions | Protected Attributes | Private Member Functions | Private Attributes | List of all members
QBuffer Class Reference

The QBuffer class is an I/O device that operates on a QByteArray. More...

#include <qbuffer.h>

Inheritance diagram for QBuffer:
QIODevice

Public Member Functions

 QBuffer ()
 
 QBuffer (QByteArray)
 
 ~QBuffer ()
 
QByteArray buffer () const
 
bool setBuffer (QByteArray)
 
bool open (int)
 
void close ()
 
void flush ()
 
uint size () const
 
int at () const
 
bool at (int)
 
int readBlock (char *p, uint)
 
int writeBlock (const char *p, uint)
 
int readLine (char *p, uint)
 
int getch ()
 
int putch (int)
 
int ungetch (int)
 
- Public Member Functions inherited from QIODevice
 QIODevice ()
 
virtual ~QIODevice ()
 
int flags () const
 
int mode () const
 
int state () const
 
bool isDirectAccess () const
 
bool isSequentialAccess () const
 
bool isCombinedAccess () const
 
bool isBuffered () const
 
bool isRaw () const
 
bool isSynchronous () const
 
bool isAsynchronous () const
 
bool isTranslated () const
 
bool isReadable () const
 
bool isWritable () const
 
bool isReadWrite () const
 
bool isInactive () const
 
bool isOpen () const
 
int status () const
 
void resetStatus ()
 
virtual bool atEnd () const
 
bool reset ()
 
int writeBlock (const QByteArray &data)
 
QByteArray readAll ()
 

Protected Attributes

QByteArray a
 
- Protected Attributes inherited from QIODevice
int ioIndex
 

Private Member Functions

 QBuffer (const QBuffer &)
 
QBufferoperator= (const QBuffer &)
 

Private Attributes

uint a_len
 
uint a_inc
 

Additional Inherited Members

- Protected Member Functions inherited from QIODevice
void setFlags (int f)
 
void setType (int)
 
void setMode (int)
 
void setState (int)
 
void setStatus (int)
 

Detailed Description

The QBuffer class is an I/O device that operates on a QByteArray.

QBuffer allows reading and writing a memory buffer. It is normally used together with a QTextStream or a QDataStream. QBuffer has an associated QByteArray which holds the buffer data. The size() of the buffer is automatically adjusted as data is written.

The constructor QBuffer(QByteArray) creates a QBuffer with an existing byte array. The byte array can also be set with setBuffer(). Writing to the QBuffer will modify the original byte array, since QByteArray is explicitly shared.

Use open() to open the buffer before use, and to set the mode (read-only,write-only, etc.). close() closes the buffer. The buffer must be closed before reopening or calling setBuffer().

The common way to use QBuffer is through QDataStream or QTextStream which have constructors that take a QBuffer parameter. For convenience, there are also QDataStream and QTextStream constructors that take a QByteArray parameter. These constructors create and open an internal QBuffer.

Note that QTextStream can also operate on a QString (a Unicode string); a QBuffer cannot.

You can also use QBuffer directly through the standard QIODevice functions readBlock(), writeBlock() readLine(), at(), getch(), putch() and ungetch().

See also
QFile, QDataStream, QTextStream, QByteArray, Shared Classes

Definition at line 47 of file qbuffer.h.

Constructor & Destructor Documentation

QBuffer::QBuffer ( )

Constructs an empty buffer.

Definition at line 83 of file qbuffer.cpp.

84 {
86  a_inc = 16; // initial increment
87  a_len = 0;
88  ioIndex = 0;
89 }
void setFlags(int f)
Definition: qiodevice.h:136
#define IO_Direct
Definition: qiodevice.h:49
uint a_len
Definition: qbuffer.h:77
uint a_inc
Definition: qbuffer.h:78
int ioIndex
Definition: qiodevice.h:141
QBuffer::QBuffer ( QByteArray  buf)

Constructs a buffer that operates on buf. If you open the buffer in write mode (IO_WriteOnly or IO_ReadWrite) and write something into the buffer, buf will be modified.

Example:

QCString str = "abc";
QBuffer b( str );
b.open( IO_WriteOnly );
b.at( 3 ); // position at \0
b.writeBlock( "def", 4 ); // write including \0
b.close();
// Now, str == "abcdef"
See also
setBuffer()

Definition at line 114 of file qbuffer.cpp.

114  : a(buf)
115 {
116  setFlags( IO_Direct );
117  a_len = a.size();
118  a_inc = (a_len > 512) ? 512 : a_len; // initial increment
119  if ( a_inc < 16 )
120  a_inc = 16;
121  ioIndex = 0;
122 }
void setFlags(int f)
Definition: qiodevice.h:136
#define IO_Direct
Definition: qiodevice.h:49
uint a_len
Definition: qbuffer.h:77
QByteArray a
Definition: qbuffer.h:74
uint size() const
Definition: qarray.h:65
uint a_inc
Definition: qbuffer.h:78
int ioIndex
Definition: qiodevice.h:141
QBuffer::~QBuffer ( )

Destructs the buffer.

Definition at line 128 of file qbuffer.cpp.

129 {
130 }
QBuffer::QBuffer ( const QBuffer )
private

Member Function Documentation

int QBuffer::at ( ) const
inlinevirtual

Reimplemented from QIODevice.

Definition at line 94 of file qbuffer.h.

95 { return ioIndex; }
int ioIndex
Definition: qiodevice.h:141
bool QBuffer::at ( int  pos)
virtual

Reimplemented from QIODevice.

Definition at line 251 of file qbuffer.cpp.

252 {
253 #if defined(CHECK_STATE)
254  if ( !isOpen() ) {
255  qWarning( "QBuffer::at: Buffer is not open" );
256  return FALSE;
257  }
258 #endif
259  if ( (uint)pos > a_len ) {
260 #if defined(CHECK_RANGE)
261  qWarning( "QBuffer::at: Index %d out of range", pos );
262 #endif
263  return FALSE;
264  }
265  ioIndex = pos;
266  return TRUE;
267 }
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
uint a_len
Definition: qbuffer.h:77
bool isOpen() const
Definition: qiodevice.h:110
unsigned uint
Definition: qglobal.h:351
const bool TRUE
Definition: qglobal.h:371
int ioIndex
Definition: qiodevice.h:141
QByteArray QBuffer::buffer ( ) const
inline

Returns this buffer's byte array.

See also
setBuffer()

Definition at line 88 of file qbuffer.h.

89 { return a; }
QByteArray a
Definition: qbuffer.h:74
void QBuffer::close ( )
virtual

Closes an open buffer.

See also
open()

Implements QIODevice.

Definition at line 217 of file qbuffer.cpp.

218 {
219  if ( isOpen() ) {
220  setFlags( IO_Direct );
221  ioIndex = 0;
222  a_inc = 16;
223  }
224 }
void setFlags(int f)
Definition: qiodevice.h:136
#define IO_Direct
Definition: qiodevice.h:49
bool isOpen() const
Definition: qiodevice.h:110
uint a_inc
Definition: qbuffer.h:78
int ioIndex
Definition: qiodevice.h:141
void QBuffer::flush ( )
virtual

The flush function does nothing for a QBuffer.

Implements QIODevice.

Definition at line 231 of file qbuffer.cpp.

232 {
233  return;
234 }
int QBuffer::getch ( )
virtual

Implements QIODevice.

Definition at line 387 of file qbuffer.cpp.

388 {
389 #if defined(CHECK_STATE)
390  if ( !isOpen() ) { // buffer not open
391  qWarning( "QBuffer::getch: Buffer not open" );
392  return -1;
393  }
394  if ( !isReadable() ) { // reading not permitted
395  qWarning( "QBuffer::getch: Read operation not permitted" );
396  return -1;
397  }
398 #endif
399  if ( (uint)ioIndex+1 > a.size() ) { // overflow
401  return -1;
402  }
403  return uchar(*(a.data()+ioIndex++));
404 }
#define IO_ReadError
Definition: qiodevice.h:78
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
bool isReadable() const
Definition: qiodevice.h:106
unsigned char uchar
Definition: nybbler.cc:11
type * data() const
Definition: qarray.h:63
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
uint size() const
Definition: qarray.h:65
unsigned uint
Definition: qglobal.h:351
int ioIndex
Definition: qiodevice.h:141
bool QBuffer::open ( int  m)
virtual

Opens the buffer in the mode m. Returns TRUE if successful, otherwise FALSE. The buffer must be opened before use.

The mode parameter m must be a combination of the following flags.

  • IO_ReadOnly opens a buffer in read-only mode.
  • IO_WriteOnly opens a buffer in write-only mode.
  • IO_ReadWrite opens a buffer in read/write mode.
  • IO_Append sets the buffer index to the end of the buffer.
  • IO_Truncate truncates the buffer.
See also
close(), isOpen()

Implements QIODevice.

Definition at line 187 of file qbuffer.cpp.

188 {
189  if ( isOpen() ) { // buffer already open
190 #if defined(CHECK_STATE)
191  qWarning( "QBuffer::open: Buffer already open" );
192 #endif
193  return FALSE;
194  }
195  setMode( m );
196  if ( m & IO_Truncate ) { // truncate buffer
197  a.resize( 0 );
198  a_len = 0;
199  }
200  if ( m & IO_Append ) { // append to end of buffer
201  ioIndex = a.size();
202  } else {
203  ioIndex = 0;
204  }
205  a_inc = 16;
206  setState( IO_Open );
207  setStatus( 0 );
208  return TRUE;
209 }
bool resize(uint size)
Definition: qarray.h:69
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setMode(int)
Definition: qiodevice.cpp:378
void setStatus(int)
Definition: qiodevice.cpp:408
#define IO_Truncate
Definition: qiodevice.h:65
#define IO_Append
Definition: qiodevice.h:64
void setState(int)
Definition: qiodevice.cpp:393
uint a_len
Definition: qbuffer.h:77
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
uint size() const
Definition: qarray.h:65
#define IO_Open
Definition: qiodevice.h:71
const bool TRUE
Definition: qglobal.h:371
uint a_inc
Definition: qbuffer.h:78
int ioIndex
Definition: qiodevice.h:141
QBuffer& QBuffer::operator= ( const QBuffer )
private
int QBuffer::putch ( int  ch)
virtual

Writes the character ch into the buffer, overwriting the character at the current index, extending the buffer if necessary.

Returns ch, or -1 if some error occurred.

See also
getch(), ungetch()

Implements QIODevice.

Definition at line 417 of file qbuffer.cpp.

418 {
419 #if defined(CHECK_STATE)
420  if ( !isOpen() ) { // buffer not open
421  qWarning( "QBuffer::putch: Buffer not open" );
422  return -1;
423  }
424  if ( !isWritable() ) { // writing not permitted
425  qWarning( "QBuffer::putch: Write operation not permitted" );
426  return -1;
427  }
428 #endif
429  if ( (uint)ioIndex + 1 >= a_len ) { // overflow
430  char buf[1];
431  buf[0] = (char)ch;
432  if ( writeBlock(buf,1) != 1 )
433  return -1; // write error
434  } else {
435  *(a.data() + ioIndex++) = (char)ch;
436  if ( a.shd->len < (uint)ioIndex )
437  a.shd->len = (uint)ioIndex;
438  }
439  return ch;
440 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
int writeBlock(const char *p, uint)
Definition: qbuffer.cpp:312
type * data() const
Definition: qarray.h:63
bool isWritable() const
Definition: qiodevice.h:107
uint a_len
Definition: qbuffer.h:77
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
unsigned uint
Definition: qglobal.h:351
int ioIndex
Definition: qiodevice.h:141
int QBuffer::readBlock ( char *  p,
uint  len 
)
virtual

Implements QIODevice.

Definition at line 274 of file qbuffer.cpp.

275 {
276 #if defined(CHECK_STATE)
277  CHECK_PTR( p );
278  if ( !isOpen() ) { // buffer not open
279  qWarning( "QBuffer::readBlock: Buffer not open" );
280  return -1;
281  }
282  if ( !isReadable() ) { // reading not permitted
283  qWarning( "QBuffer::readBlock: Read operation not permitted" );
284  return -1;
285  }
286 #endif
287  if ( (uint)ioIndex + len > a.size() ) { // overflow
288  if ( (uint)ioIndex >= a.size() ) {
290  return -1;
291  } else {
292  len = a.size() - (uint)ioIndex;
293  }
294  }
295  memcpy( p, a.data()+ioIndex, len );
296  ioIndex += len;
297  return len;
298 }
#define IO_ReadError
Definition: qiodevice.h:78
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
bool isReadable() const
Definition: qiodevice.h:106
type * data() const
Definition: qarray.h:63
p
Definition: test.py:223
#define CHECK_PTR(p)
Definition: qglobal.h:601
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
uint size() const
Definition: qarray.h:65
unsigned uint
Definition: qglobal.h:351
int ioIndex
Definition: qiodevice.h:141
int QBuffer::readLine ( char *  p,
uint  maxlen 
)
virtual

Reimplemented from QIODevice.

Definition at line 353 of file qbuffer.cpp.

354 {
355 #if defined(CHECK_STATE)
356  CHECK_PTR( p );
357  if ( !isOpen() ) { // buffer not open
358  qWarning( "QBuffer::readLine: Buffer not open" );
359  return -1;
360  }
361  if ( !isReadable() ) { // reading not permitted
362  qWarning( "QBuffer::readLine: Read operation not permitted" );
363  return -1;
364  }
365 #endif
366  if ( maxlen == 0 )
367  return 0;
368  uint start = (uint)ioIndex;
369  char *d = a.data() + ioIndex;
370  maxlen--; // make room for 0-terminator
371  if ( a.size() - (uint)ioIndex < maxlen )
372  maxlen = a.size() - (uint)ioIndex;
373  while ( maxlen-- ) {
374  if ( (*p++ = *d++) == '\n' )
375  break;
376  }
377  *p = '\0';
378  ioIndex = (int)(d - a.data());
379  return (uint)ioIndex - start;
380 }
static const int maxlen
Definition: qregexp.cpp:904
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
bool isReadable() const
Definition: qiodevice.h:106
type * data() const
Definition: qarray.h:63
p
Definition: test.py:223
#define CHECK_PTR(p)
Definition: qglobal.h:601
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
uint size() const
Definition: qarray.h:65
unsigned uint
Definition: qglobal.h:351
int ioIndex
Definition: qiodevice.h:141
bool QBuffer::setBuffer ( QByteArray  buf)

Replaces the buffer's contents with buf.

This may not be done when isOpen() is TRUE.

Note that if you open the buffer in write mode (IO_WriteOnly or IO_ReadWrite) and write something into the buffer, buf is also modified because QByteArray is an explicitly shared class.

See also
buffer(), open(), close()

Definition at line 145 of file qbuffer.cpp.

146 {
147  if ( isOpen() ) {
148 #if defined(CHECK_STATE)
149  qWarning( "QBuffer::setBuffer: Buffer is open");
150 #endif
151  return FALSE;
152  }
153  a = buf;
154  a_len = a.size();
155  a_inc = (a_len > 512) ? 512 : a_len; // initial increment
156  if ( a_inc < 16 )
157  a_inc = 16;
158  ioIndex = 0;
159  return TRUE;
160 }
const bool FALSE
Definition: qglobal.h:370
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
uint a_len
Definition: qbuffer.h:77
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
uint size() const
Definition: qarray.h:65
const bool TRUE
Definition: qglobal.h:371
uint a_inc
Definition: qbuffer.h:78
int ioIndex
Definition: qiodevice.h:141
uint QBuffer::size ( ) const
inlinevirtual

Implements QIODevice.

Definition at line 91 of file qbuffer.h.

92 { return a.size(); }
QByteArray a
Definition: qbuffer.h:74
uint size() const
Definition: qarray.h:65
int QBuffer::ungetch ( int  ch)
virtual

Implements QIODevice.

Definition at line 446 of file qbuffer.cpp.

447 {
448 #if defined(CHECK_STATE)
449  if ( !isOpen() ) { // buffer not open
450  qWarning( "QBuffer::ungetch: Buffer not open" );
451  return -1;
452  }
453  if ( !isReadable() ) { // reading not permitted
454  qWarning( "QBuffer::ungetch: Read operation not permitted" );
455  return -1;
456  }
457 #endif
458  if ( ch != -1 ) {
459  if ( ioIndex )
460  ioIndex--;
461  else
462  ch = -1;
463  }
464  return ch;
465 }
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
bool isReadable() const
Definition: qiodevice.h:106
bool isOpen() const
Definition: qiodevice.h:110
int ioIndex
Definition: qiodevice.h:141
int QBuffer::writeBlock ( const char *  p,
uint  len 
)
virtual

Writes len bytes from p into the buffer at the current index, overwriting any characters there and extending the buffer if necessary. Returns the number of bytes actually written.

Returns -1 if a serious error occurred.

See also
readBlock()

Implements QIODevice.

Definition at line 312 of file qbuffer.cpp.

313 {
314 #if defined(CHECK_NULL)
315  if ( p == 0 && len != 0 )
316  qWarning( "QBuffer::writeBlock: Null pointer error" );
317 #endif
318 #if defined(CHECK_STATE)
319  if ( !isOpen() ) { // buffer not open
320  qWarning( "QBuffer::writeBlock: Buffer not open" );
321  return -1;
322  }
323  if ( !isWritable() ) { // writing not permitted
324  qWarning( "QBuffer::writeBlock: Write operation not permitted" );
325  return -1;
326  }
327 #endif
328  if ( (uint)ioIndex + len >= a_len ) { // overflow
329  uint new_len = a_len + a_inc*(((uint)ioIndex+len-a_len)/a_inc+1);
330  if ( !a.resize( new_len ) ) { // could not resize
331 #if defined(CHECK_NULL)
332  qWarning( "QBuffer::writeBlock: Memory allocation error" );
333 #endif
335  return -1;
336  }
337  a_inc *= 2; // double increment
338  a_len = new_len;
339  a.shd->len = (uint)ioIndex + len;
340  }
341  memcpy( a.data()+ioIndex, p, len );
342  ioIndex += len;
343  if ( a.shd->len < (uint)ioIndex )
344  a.shd->len = (uint)ioIndex; // fake (not alloc'd) length
345  return len;
346 }
bool resize(uint size)
Definition: qarray.h:69
void qWarning(const char *msg,...)
Definition: qglobal.cpp:409
void setStatus(int)
Definition: qiodevice.cpp:408
type * data() const
Definition: qarray.h:63
#define IO_ResourceError
Definition: qiodevice.h:81
bool isWritable() const
Definition: qiodevice.h:107
p
Definition: test.py:223
uint a_len
Definition: qbuffer.h:77
QByteArray a
Definition: qbuffer.h:74
bool isOpen() const
Definition: qiodevice.h:110
unsigned uint
Definition: qglobal.h:351
uint a_inc
Definition: qbuffer.h:78
int ioIndex
Definition: qiodevice.h:141

Member Data Documentation

QByteArray QBuffer::a
protected

Definition at line 74 of file qbuffer.h.

uint QBuffer::a_inc
private

Definition at line 78 of file qbuffer.h.

uint QBuffer::a_len
private

Definition at line 77 of file qbuffer.h.


The documentation for this class was generated from the following files: