1 /****************************************************************************
4 ** Implementation of QBuffer class
8 ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
10 ** This file is part of the tools module of the Qt GUI Toolkit.
12 ** This file may be distributed under the terms of the Q Public License
13 ** as defined by Trolltech AS of Norway and appearing in the file
14 ** LICENSE.QPL included in the packaging of this file.
16 ** This file may be distributed and/or modified under the terms of the
17 ** GNU General Public License version 2 as published by the Free Software
18 ** Foundation and appearing in the file LICENSE.GPL included in the
19 ** packaging of this file.
21 ** Licensees holding valid Qt Enterprise Edition or Qt Professional Edition
22 ** licenses may use this file in accordance with the Qt Commercial License
23 ** Agreement provided with the Software.
25 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
28 ** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
29 ** information about Qt Commercial License Agreements.
30 ** See http://www.trolltech.com/qpl/ for QPL licensing information.
31 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
33 ** Contact info@trolltech.com if any conditions of this licensing are
36 **********************************************************************/
43 \class QBuffer qbuffer.h
44 \brief The QBuffer class is an I/O device that operates on a QByteArray
48 QBuffer allows reading and writing a memory buffer. It is normally
49 used together with a QTextStream or a QDataStream. QBuffer has an
50 associated QByteArray which holds the buffer data. The size() of the
51 buffer is automatically adjusted as data is written.
53 The constructor \c QBuffer(QByteArray) creates a QBuffer with an
54 existing byte array. The byte array can also be set with setBuffer().
55 Writing to the QBuffer will modify the original byte array, since
56 QByteArray is \link shclass.html explicitly shared.\endlink
58 Use open() to open the buffer before use, and to set the mode
59 (read-only,write-only, etc.). close() closes the buffer. The buffer
60 must be closed before reopening or calling setBuffer().
62 The common way to use QBuffer is through \l QDataStream or \l QTextStream
63 which have constructors that take a QBuffer parameter. For
64 convenience, there are also QDataStream and QTextStream constructors
65 that take a QByteArray parameter. These constructors create and open
68 Note that QTextStream can also operate on a QString (a Unicode
69 string); a QBuffer cannot.
71 You can also use QBuffer directly through the standard QIODevice
72 functions readBlock(), writeBlock() readLine(), at(), getch(), putch() and
75 \sa QFile, QDataStream, QTextStream, QByteArray, \link shclass.html Shared Classes\endlink
80 Constructs an empty buffer.
85 setFlags( IO_Direct );
86 a_inc = 16; // initial increment
93 Constructs a buffer that operates on \a buf.
94 If you open the buffer in write mode (\c IO_WriteOnly or
95 \c IO_ReadWrite) and write something into the buffer, \a buf
101 QCString str = "abc";
103 b.open( IO_WriteOnly );
104 b.at( 3 ); // position at \0
105 b.writeBlock( "def", 4 ); // write including \0
107 // Now, str == "abcdef"
114 QBuffer::QBuffer( QByteArray buf ) : a(buf)
116 setFlags( IO_Direct );
118 a_inc = (a_len > 512) ? 512 : a_len; // initial increment
125 Destructs the buffer.
134 Replaces the buffer's contents with \a buf.
136 This may not be done when isOpen() is TRUE.
138 Note that if you open the buffer in write mode (\c IO_WriteOnly or
139 IO_ReadWrite) and write something into the buffer, \a buf is also
140 modified because QByteArray is an explicitly shared class.
142 \sa buffer(), open(), close()
145 bool QBuffer::setBuffer( QByteArray buf )
148 #if defined(CHECK_STATE)
149 qWarning( "QBuffer::setBuffer: Buffer is open");
155 a_inc = (a_len > 512) ? 512 : a_len; // initial increment
163 \fn QByteArray QBuffer::buffer() const
165 Returns this buffer's byte array.
172 Opens the buffer in the mode \a m. Returns TRUE if successful,
173 otherwise FALSE. The buffer must be opened before use.
175 The mode parameter \a m must be a combination of the following flags.
177 <li>\c IO_ReadOnly opens a buffer in read-only mode.
178 <li>\c IO_WriteOnly opens a buffer in write-only mode.
179 <li>\c IO_ReadWrite opens a buffer in read/write mode.
180 <li>\c IO_Append sets the buffer index to the end of the buffer.
181 <li>\c IO_Truncate truncates the buffer.
184 \sa close(), isOpen()
187 bool QBuffer::open( int m )
189 if ( isOpen() ) { // buffer already open
190 #if defined(CHECK_STATE)
191 qWarning( "QBuffer::open: Buffer already open" );
196 if ( m & IO_Truncate ) { // truncate buffer
200 if ( m & IO_Append ) { // append to end of buffer
213 Closes an open buffer.
217 void QBuffer::close()
220 setFlags( IO_Direct );
228 The flush function does nothing for a QBuffer.
231 void QBuffer::flush()
238 \fn int QBuffer::at() const
243 \fn uint QBuffer::size() const
251 bool QBuffer::at( int pos )
253 #if defined(CHECK_STATE)
255 qWarning( "QBuffer::at: Buffer is not open" );
259 if ( (uint)pos > a_len ) {
260 #if defined(CHECK_RANGE)
261 qWarning( "QBuffer::at: Index %d out of range", pos );
274 int QBuffer::readBlock( char *p, uint len )
276 #if defined(CHECK_STATE)
278 if ( !isOpen() ) { // buffer not open
279 qWarning( "QBuffer::readBlock: Buffer not open" );
282 if ( !isReadable() ) { // reading not permitted
283 qWarning( "QBuffer::readBlock: Read operation not permitted" );
287 if ( (uint)ioIndex + len > a.size() ) { // overflow
288 if ( (uint)ioIndex >= a.size() ) {
289 setStatus( IO_ReadError );
292 len = a.size() - (uint)ioIndex;
295 memcpy( p, a.data()+ioIndex, len );
303 Writes \a len bytes from \a p into the buffer at the current index,
304 overwriting any characters there and extending the buffer if necessary.
305 Returns the number of bytes actually written.
307 Returns -1 if a serious error occurred.
312 int QBuffer::writeBlock( const char *p, uint len )
314 #if defined(CHECK_NULL)
315 if ( p == 0 && len != 0 )
316 qWarning( "QBuffer::writeBlock: Null pointer error" );
318 #if defined(CHECK_STATE)
319 if ( !isOpen() ) { // buffer not open
320 qWarning( "QBuffer::writeBlock: Buffer not open" );
323 if ( !isWritable() ) { // writing not permitted
324 qWarning( "QBuffer::writeBlock: Write operation not permitted" );
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" );
334 setStatus( IO_ResourceError );
337 a_inc *= 2; // double increment
339 a.shd->len = (uint)ioIndex + len;
341 memcpy( a.data()+ioIndex, p, len );
343 if ( a.shd->len < (uint)ioIndex )
344 a.shd->len = (uint)ioIndex; // fake (not alloc'd) length
353 int QBuffer::readLine( char *p, uint maxlen )
355 #if defined(CHECK_STATE)
357 if ( !isOpen() ) { // buffer not open
358 qWarning( "QBuffer::readLine: Buffer not open" );
361 if ( !isReadable() ) { // reading not permitted
362 qWarning( "QBuffer::readLine: Read operation not permitted" );
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;
374 if ( (*p++ = *d++) == '\n' )
378 ioIndex = d - a.data();
379 return (uint)ioIndex - start;
389 #if defined(CHECK_STATE)
390 if ( !isOpen() ) { // buffer not open
391 qWarning( "QBuffer::getch: Buffer not open" );
394 if ( !isReadable() ) { // reading not permitted
395 qWarning( "QBuffer::getch: Read operation not permitted" );
399 if ( (uint)ioIndex+1 > a.size() ) { // overflow
400 setStatus( IO_ReadError );
403 return uchar(*(a.data()+ioIndex++));
408 Writes the character \a ch into the buffer, overwriting
409 the character at the current index, extending the buffer
412 Returns \a ch, or -1 if some error occurred.
414 \sa getch(), ungetch()
417 int QBuffer::putch( int ch )
419 #if defined(CHECK_STATE)
420 if ( !isOpen() ) { // buffer not open
421 qWarning( "QBuffer::putch: Buffer not open" );
424 if ( !isWritable() ) { // writing not permitted
425 qWarning( "QBuffer::putch: Write operation not permitted" );
429 if ( (uint)ioIndex + 1 >= a_len ) { // overflow
432 if ( writeBlock(buf,1) != 1 )
433 return -1; // write error
435 *(a.data() + ioIndex++) = (char)ch;
436 if ( a.shd->len < (uint)ioIndex )
437 a.shd->len = (uint)ioIndex;
446 int QBuffer::ungetch( int ch )
448 #if defined(CHECK_STATE)
449 if ( !isOpen() ) { // buffer not open
450 qWarning( "QBuffer::ungetch: Buffer not open" );
453 if ( !isReadable() ) { // reading not permitted
454 qWarning( "QBuffer::ungetch: Read operation not permitted" );