Removing unnecessary chunking and stat'ing when reading QIODevice
[qt-netbsd.git] / src / corelib / io / qiodevice.cpp
blob43cc78d8dcf55a44f07ea917983a2adca2943cb5
1 /****************************************************************************
2 **
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtCore module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 //#define QIODEVICE_DEBUG
44 #include "qbytearray.h"
45 #include "qdebug.h"
46 #include "qiodevice_p.h"
47 #include "qfile.h"
48 #include "qstringlist.h"
49 #include <limits.h>
51 #ifdef QIODEVICE_DEBUG
52 # include <ctype.h>
53 #endif
55 QT_BEGIN_NAMESPACE
57 #ifdef QIODEVICE_DEBUG
58 void debugBinaryString(const QByteArray &input)
60 QByteArray tmp;
61 int startOffset = 0;
62 for (int i = 0; i < input.size(); ++i) {
63 tmp += input[i];
65 if ((i % 16) == 15 || i == (input.size() - 1)) {
66 printf("\n%15d:", startOffset);
67 startOffset += tmp.size();
69 for (int j = 0; j < tmp.size(); ++j)
70 printf(" %02x", int(uchar(tmp[j])));
71 for (int j = tmp.size(); j < 16 + 1; ++j)
72 printf(" ");
73 for (int j = 0; j < tmp.size(); ++j)
74 printf("%c", isprint(int(uchar(tmp[j]))) ? tmp[j] : '.');
75 tmp.clear();
78 printf("\n\n");
81 void debugBinaryString(const char *data, qint64 maxlen)
83 debugBinaryString(QByteArray(data, maxlen));
85 #endif
87 #ifndef QIODEVICE_BUFFERSIZE
88 #define QIODEVICE_BUFFERSIZE Q_INT64_C(16384)
89 #endif
91 #define Q_VOID
93 #define CHECK_MAXLEN(function, returnType) \
94 do { \
95 if (maxSize < 0) { \
96 qWarning("QIODevice::"#function": Called with maxSize < 0"); \
97 return returnType; \
98 } \
99 } while (0)
101 #define CHECK_WRITABLE(function, returnType) \
102 do { \
103 if ((d->openMode & WriteOnly) == 0) { \
104 if (d->openMode == NotOpen) \
105 return returnType; \
106 qWarning("QIODevice::"#function": ReadOnly device"); \
107 return returnType; \
109 } while (0)
111 #define CHECK_READABLE(function, returnType) \
112 do { \
113 if ((d->openMode & ReadOnly) == 0) { \
114 if (d->openMode == NotOpen) \
115 return returnType; \
116 qWarning("QIODevice::"#function": WriteOnly device"); \
117 return returnType; \
119 } while (0)
121 /*! \internal
123 QIODevicePrivate::QIODevicePrivate()
124 : openMode(QIODevice::NotOpen), buffer(QIODEVICE_BUFFERSIZE),
125 pos(0), devicePos(0)
126 , baseReadLineDataCalled(false)
127 , accessMode(Unset)
128 #ifdef QT_NO_QOBJECT
129 , q_ptr(0)
130 #endif
134 /*! \internal
136 QIODevicePrivate::~QIODevicePrivate()
141 \class QIODevice
142 \reentrant
144 \brief The QIODevice class is the base interface class of all I/O
145 devices in Qt.
147 \ingroup io
149 QIODevice provides both a common implementation and an abstract
150 interface for devices that support reading and writing of blocks
151 of data, such as QFile, QBuffer and QTcpSocket. QIODevice is
152 abstract and can not be instantiated, but it is common to use the
153 interface it defines to provide device-independent I/O features.
154 For example, Qt's XML classes operate on a QIODevice pointer,
155 allowing them to be used with various devices (such as files and
156 buffers).
158 Before accessing the device, open() must be called to set the
159 correct OpenMode (such as ReadOnly or ReadWrite). You can then
160 write to the device with write() or putChar(), and read by calling
161 either read(), readLine(), or readAll(). Call close() when you are
162 done with the device.
164 QIODevice distinguishes between two types of devices:
165 random-access devices and sequential devices.
167 \list
168 \o Random-access devices support seeking to arbitrary
169 positions using seek(). The current position in the file is
170 available by calling pos(). QFile and QBuffer are examples of
171 random-access devices.
173 \o Sequential devices don't support seeking to arbitrary
174 positions. The data must be read in one pass. The functions
175 pos() and size() don't work for sequential devices.
176 QTcpSocket and QProcess are examples of sequential devices.
177 \endlist
179 You can use isSequential() to determine the type of device.
181 QIODevice emits readyRead() when new data is available for
182 reading; for example, if new data has arrived on the network or if
183 additional data is appended to a file that you are reading
184 from. You can call bytesAvailable() to determine the number of
185 bytes that are currently available for reading. It's common to use
186 bytesAvailable() together with the readyRead() signal when
187 programming with asynchronous devices such as QTcpSocket, where
188 fragments of data can arrive at arbitrary points in
189 time. QIODevice emits the bytesWritten() signal every time a
190 payload of data has been written to the device. Use bytesToWrite()
191 to determine the current amount of data waiting to be written.
193 Certain subclasses of QIODevice, such as QTcpSocket and QProcess,
194 are asynchronous. This means that I/O functions such as write()
195 or read() always return immediately, while communication with the
196 device itself may happen when control goes back to the event loop.
197 QIODevice provides functions that allow you to force these
198 operations to be performed immediately, while blocking the
199 calling thread and without entering the event loop. This allows
200 QIODevice subclasses to be used without an event loop, or in
201 a separate thread:
203 \list
204 \o waitForReadyRead() - This function suspends operation in the
205 calling thread until new data is available for reading.
207 \o waitForBytesWritten() - This function suspends operation in the
208 calling thread until one payload of data has been written to the
209 device.
211 \o waitFor....() - Subclasses of QIODevice implement blocking
212 functions for device-specific operations. For example, QProcess
213 has a function called waitForStarted() which suspends operation in
214 the calling thread until the process has started.
215 \endlist
217 Calling these functions from the main, GUI thread, may cause your
218 user interface to freeze. Example:
220 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 0
222 By subclassing QIODevice, you can provide the same interface to
223 your own I/O devices. Subclasses of QIODevice are only required to
224 implement the protected readData() and writeData() functions.
225 QIODevice uses these functions to implement all its convenience
226 functions, such as getChar(), readLine() and write(). QIODevice
227 also handles access control for you, so you can safely assume that
228 the device is opened in write mode if writeData() is called.
230 Some subclasses, such as QFile and QTcpSocket, are implemented
231 using a memory buffer for intermediate storing of data. This
232 reduces the number of required device accessing calls, which are
233 often very slow. Buffering makes functions like getChar() and
234 putChar() fast, as they can operate on the memory buffer instead
235 of directly on the device itself. Certain I/O operations, however,
236 don't work well with a buffer. For example, if several users open
237 the same device and read it character by character, they may end
238 up reading the same data when they meant to read a separate chunk
239 each. For this reason, QIODevice allows you to bypass any
240 buffering by passing the Unbuffered flag to open(). When
241 subclassing QIODevice, remember to bypass any buffer you may use
242 when the device is open in Unbuffered mode.
244 \sa QBuffer QFile QTcpSocket
248 \typedef QIODevice::Offset
249 \compat
251 Use \c qint64 instead.
255 \typedef QIODevice::Status
256 \compat
258 Use QIODevice::OpenMode instead, or see the documentation for
259 specific devices.
263 \enum QIODevice::OpenModeFlag
265 This enum is used with open() to describe the mode in which a device
266 is opened. It is also returned by openMode().
268 \value NotOpen The device is not open.
269 \value ReadOnly The device is open for reading.
270 \value WriteOnly The device is open for writing.
271 \value ReadWrite The device is open for reading and writing.
272 \value Append The device is opened in append mode, so that all data is
273 written to the end of the file.
274 \value Truncate If possible, the device is truncated before it is opened.
275 All earlier contents of the device are lost.
276 \value Text When reading, the end-of-line terminators are
277 translated to '\n'. When writing, the end-of-line
278 terminators are translated to the local encoding, for
279 example '\r\n' for Win32.
280 \value Unbuffered Any buffer in the device is bypassed.
282 Certain flags, such as \c Unbuffered and \c Truncate, are
283 meaningless when used with some subclasses. Some of these
284 restrictions are implied by the type of device that is represented
285 by a subclass; for example, access to a QBuffer is always
286 unbuffered. In other cases, the restriction may be due to the
287 implementation, or may be imposed by the underlying platform; for
288 example, QTcpSocket does not support \c Unbuffered mode, and
289 limitations in the native API prevent QFile from supporting \c
290 Unbuffered on Windows.
293 /*! \fn QIODevice::bytesWritten(qint64 bytes)
295 This signal is emitted every time a payload of data has been
296 written to the device. The \a bytes argument is set to the number
297 of bytes that were written in this payload.
299 bytesWritten() is not emitted recursively; if you reenter the event loop
300 or call waitForBytesWritten() inside a slot connected to the
301 bytesWritten() signal, the signal will not be reemitted (although
302 waitForBytesWritten() may still return true).
304 \sa readyRead()
308 \fn QIODevice::readyRead()
310 This signal is emitted once every time new data is available for
311 reading from the device. It will only be emitted again once new
312 data is available, such as when a new payload of network data has
313 arrived on your network socket, or when a new block of data has
314 been appended to your device.
316 readyRead() is not emitted recursively; if you reenter the event loop or
317 call waitForReadyRead() inside a slot connected to the readyRead() signal,
318 the signal will not be reemitted (although waitForReadyRead() may still
319 return true).
321 Note for developers implementing classes derived from QIODevice:
322 you should always emit readyRead() when new data has arrived (do not
323 emit it only because there's data still to be read in your
324 buffers). Do not emit readyRead() in other conditions.
326 \sa bytesWritten()
329 /*! \fn QIODevice::aboutToClose()
331 This signal is emitted when the device is about to close. Connect
332 this signal if you have operations that need to be performed
333 before the device closes (e.g., if you have data in a separate
334 buffer that needs to be written to the device).
338 \fn QIODevice::readChannelFinished()
339 \since 4.4
341 This signal is emitted when the input (reading) stream is closed
342 in this device. It is emitted as soon as the closing is detected,
343 which means that there might still be data available for reading
344 with read().
346 \sa atEnd(), read()
349 #ifdef QT_NO_QOBJECT
350 QIODevice::QIODevice()
351 : d_ptr(new QIODevicePrivate)
353 d_ptr->q_ptr = this;
356 /*! \internal
358 QIODevice::QIODevice(QIODevicePrivate &dd)
359 : d_ptr(&dd)
361 d_ptr->q_ptr = this;
363 #else
366 Constructs a QIODevice object.
369 QIODevice::QIODevice()
370 : QObject(*new QIODevicePrivate, 0)
372 #if defined QIODEVICE_DEBUG
373 QFile *file = qobject_cast<QFile *>(this);
374 printf("%p QIODevice::QIODevice(\"%s\") %s\n", this, metaObject()->className(),
375 qPrintable(file ? file->fileName() : QString()));
376 #endif
380 Constructs a QIODevice object with the given \a parent.
383 QIODevice::QIODevice(QObject *parent)
384 : QObject(*new QIODevicePrivate, parent)
386 #if defined QIODEVICE_DEBUG
387 printf("%p QIODevice::QIODevice(%p \"%s\")\n", this, parent, metaObject()->className());
388 #endif
391 /*! \internal
393 QIODevice::QIODevice(QIODevicePrivate &dd, QObject *parent)
394 : QObject(dd, parent)
397 #endif
401 Destructs the QIODevice object.
403 QIODevice::~QIODevice()
405 #if defined QIODEVICE_DEBUG
406 printf("%p QIODevice::~QIODevice()\n", this);
407 #endif
411 Returns true if this device is sequential; otherwise returns
412 false.
414 Sequential devices, as opposed to a random-access devices, have no
415 concept of a start, an end, a size, or a current position, and they
416 do not support seeking. You can only read from the device when it
417 reports that data is available. The most common example of a
418 sequential device is a network socket. On Unix, special files such
419 as /dev/zero and fifo pipes are sequential.
421 Regular files, on the other hand, do support random access. They
422 have both a size and a current position, and they also support
423 seeking backwards and forwards in the data stream. Regular files
424 are non-sequential.
426 \sa bytesAvailable()
428 bool QIODevice::isSequential() const
430 return false;
434 Returns the mode in which the device has been opened;
435 i.e. ReadOnly or WriteOnly.
437 \sa OpenMode
439 QIODevice::OpenMode QIODevice::openMode() const
441 return d_func()->openMode;
445 Sets the OpenMode of the device to \a openMode. Call this
446 function to set the open mode if the flags change after the device
447 has been opened.
449 \sa openMode() OpenMode
451 void QIODevice::setOpenMode(OpenMode openMode)
453 #if defined QIODEVICE_DEBUG
454 printf("%p QIODevice::setOpenMode(0x%x)\n", this, int(openMode));
455 #endif
456 d_func()->openMode = openMode;
457 d_func()->accessMode = QIODevicePrivate::Unset;
461 If \a enabled is true, this function sets the \l Text flag on the device;
462 otherwise the \l Text flag is removed. This feature is useful for classes
463 that provide custom end-of-line handling on a QIODevice.
465 \sa open(), setOpenMode()
467 void QIODevice::setTextModeEnabled(bool enabled)
469 Q_D(QIODevice);
470 if (enabled)
471 d->openMode |= Text;
472 else
473 d->openMode &= ~Text;
477 Returns true if the \l Text flag is enabled; otherwise returns false.
479 \sa setTextModeEnabled()
481 bool QIODevice::isTextModeEnabled() const
483 return d_func()->openMode & Text;
487 Returns true if the device is open; otherwise returns false. A
488 device is open if it can be read from and/or written to. By
489 default, this function returns false if openMode() returns
490 \c NotOpen.
492 \sa openMode() OpenMode
494 bool QIODevice::isOpen() const
496 return d_func()->openMode != NotOpen;
500 Returns true if data can be read from the device; otherwise returns
501 false. Use bytesAvailable() to determine how many bytes can be read.
503 This is a convenience function which checks if the OpenMode of the
504 device contains the ReadOnly flag.
506 \sa openMode() OpenMode
508 bool QIODevice::isReadable() const
510 return (openMode() & ReadOnly) != 0;
514 Returns true if data can be written to the device; otherwise returns
515 false.
517 This is a convenience function which checks if the OpenMode of the
518 device contains the WriteOnly flag.
520 \sa openMode() OpenMode
522 bool QIODevice::isWritable() const
524 return (openMode() & WriteOnly) != 0;
528 Opens the device and sets its OpenMode to \a mode. Returns true if successful;
529 otherwise returns false. This function should be called from any
530 reimplementations of open() or other functions that open the device.
532 \sa openMode() OpenMode
534 bool QIODevice::open(OpenMode mode)
536 Q_D(QIODevice);
537 d->openMode = mode;
538 d->pos = (mode & Append) ? size() : qint64(0);
539 d->buffer.clear();
540 d->accessMode = QIODevicePrivate::Unset;
541 #if defined QIODEVICE_DEBUG
542 printf("%p QIODevice::open(0x%x)\n", this, quint32(mode));
543 #endif
544 return true;
548 First emits aboutToClose(), then closes the device and sets its
549 OpenMode to NotOpen. The error string is also reset.
551 \sa setOpenMode() OpenMode
553 void QIODevice::close()
555 Q_D(QIODevice);
556 if (d->openMode == NotOpen)
557 return;
559 #if defined QIODEVICE_DEBUG
560 printf("%p QIODevice::close()\n", this);
561 #endif
563 #ifndef QT_NO_QOBJECT
564 emit aboutToClose();
565 #endif
566 d->openMode = NotOpen;
567 d->errorString.clear();
568 d->pos = 0;
569 d->buffer.clear();
573 For random-access devices, this function returns the position that
574 data is written to or read from. For sequential devices or closed
575 devices, where there is no concept of a "current position", 0 is
576 returned.
578 The current read/write position of the device is maintained internally by
579 QIODevice, so reimplementing this function is not necessary. When
580 subclassing QIODevice, use QIODevice::seek() to notify QIODevice about
581 changes in the device position.
583 \sa isSequential(), seek()
585 qint64 QIODevice::pos() const
587 Q_D(const QIODevice);
588 #if defined QIODEVICE_DEBUG
589 printf("%p QIODevice::pos() == %d\n", this, int(d->pos));
590 #endif
591 return d->pos;
595 For open random-access devices, this function returns the size of the
596 device. For open sequential devices, bytesAvailable() is returned.
598 If the device is closed, the size returned will not reflect the actual
599 size of the device.
601 \sa isSequential(), pos()
603 qint64 QIODevice::size() const
605 return d_func()->isSequential() ? bytesAvailable() : qint64(0);
609 For random-access devices, this function sets the current position
610 to \a pos, returning true on success, or false if an error occurred.
611 For sequential devices, the default behavior is to do nothing and
612 return false.
614 When subclassing QIODevice, you must call QIODevice::seek() at the
615 start of your function to ensure integrity with QIODevice's
616 built-in buffer. The base implementation always returns true.
618 \sa pos(), isSequential()
620 bool QIODevice::seek(qint64 pos)
622 if (d_func()->openMode == NotOpen) {
623 qWarning("QIODevice::seek: The device is not open");
624 return false;
626 if (pos < 0) {
627 qWarning("QIODevice::seek: Invalid pos: %d", int(pos));
628 return false;
631 Q_D(QIODevice);
632 #if defined QIODEVICE_DEBUG
633 printf("%p QIODevice::seek(%d), before: d->pos = %d, d->buffer.size() = %d\n",
634 this, int(pos), int(d->pos), d->buffer.size());
635 #endif
637 qint64 offset = pos - d->pos;
638 if (!d->isSequential()) {
639 d->pos = pos;
640 d->devicePos = pos;
643 if (offset > 0 && !d->buffer.isEmpty()) {
644 // When seeking forwards, we need to pop bytes off the front of the
645 // buffer.
646 do {
647 int bytesToSkip = int(qMin<qint64>(offset, INT_MAX));
648 d->buffer.skip(bytesToSkip);
649 offset -= bytesToSkip;
650 } while (offset > 0);
651 } else if (offset < 0) {
652 // When seeking backwards, an operation that is only allowed for
653 // random-access devices, the buffer is cleared. The next read
654 // operation will then refill the buffer. We can optimize this, if we
655 // find that seeking backwards becomes a significant performance hit.
656 d->buffer.clear();
658 #if defined QIODEVICE_DEBUG
659 printf("%p \tafter: d->pos == %d, d->buffer.size() == %d\n", this, int(d->pos),
660 d->buffer.size());
661 #endif
662 return true;
666 Returns true if the current read and write position is at the end
667 of the device (i.e. there is no more data available for reading on
668 the device); otherwise returns false.
670 For some devices, atEnd() can return true even though there is more data
671 to read. This special case only applies to devices that generate data in
672 direct response to you calling read() (e.g., \c /dev or \c /proc files on
673 Unix and Mac OS X, or console input / \c stdin on all platforms).
675 \sa bytesAvailable(), read(), isSequential()
677 bool QIODevice::atEnd() const
679 Q_D(const QIODevice);
680 #if defined QIODEVICE_DEBUG
681 printf("%p QIODevice::atEnd() returns %s, d->openMode == %d, d->pos == %d\n", this, (d->openMode == NotOpen || d->pos == size()) ? "true" : "false",
682 int(d->openMode), int(d->pos));
683 #endif
684 return d->openMode == NotOpen || (d->buffer.isEmpty() && bytesAvailable() == 0);
688 Seeks to the start of input for random-access devices. Returns
689 true on success; otherwise returns false (for example, if the
690 device is not open).
692 Note that when using a QTextStream on a QFile, calling reset() on
693 the QFile will not have the expected result because QTextStream
694 buffers the file. Use the QTextStream::seek() function instead.
696 \sa seek()
698 bool QIODevice::reset()
700 #if defined QIODEVICE_DEBUG
701 printf("%p QIODevice::reset()\n", this);
702 #endif
703 return seek(0);
707 Returns the number of bytes that are available for reading. This
708 function is commonly used with sequential devices to determine the
709 number of bytes to allocate in a buffer before reading.
711 Subclasses that reimplement this function must call the base
712 implementation in order to include the size of QIODevices' buffer. Example:
714 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 1
716 \sa bytesToWrite(), readyRead(), isSequential()
718 qint64 QIODevice::bytesAvailable() const
720 Q_D(const QIODevice);
721 if (!d->isSequential())
722 return qMax(size() - d->pos, qint64(0));
723 return d->buffer.size();
727 For buffered devices, this function returns the number of bytes
728 waiting to be written. For devices with no buffer, this function
729 returns 0.
731 \sa bytesAvailable(), bytesWritten(), isSequential()
733 qint64 QIODevice::bytesToWrite() const
735 return qint64(0);
739 Reads at most \a maxSize bytes from the device into \a data, and
740 returns the number of bytes read. If an error occurs, such as when
741 attempting to read from a device opened in WriteOnly mode, this
742 function returns -1.
744 0 is returned when no more data is available for reading. However,
745 reading past the end of the stream is considered an error, so this
746 function returns -1 in those cases (that is, reading on a closed
747 socket or after a process has died).
749 \sa readData() readLine() write()
751 qint64 QIODevice::read(char *data, qint64 maxSize)
753 Q_D(QIODevice);
754 CHECK_READABLE(read, qint64(-1));
755 CHECK_MAXLEN(read, qint64(-1));
757 #if defined QIODEVICE_DEBUG
758 printf("%p QIODevice::read(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
759 this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
760 #endif
761 const bool sequential = d->isSequential();
763 // Short circuit for getChar()
764 if (maxSize == 1) {
765 int chint = d->buffer.getChar();
766 if (chint != -1) {
767 char c = char(uchar(chint));
768 if (c == '\r' && (d->openMode & Text)) {
769 d->buffer.ungetChar(c);
770 } else {
771 if (data)
772 *data = c;
773 if (!sequential)
774 ++d->pos;
775 #if defined QIODEVICE_DEBUG
776 printf("%p \tread 0x%hhx (%c) returning 1 (shortcut)\n", this,
777 int(c), isprint(c) ? c : '?');
778 #endif
779 return qint64(1);
784 qint64 readSoFar = 0;
785 bool moreToRead = true;
786 do {
787 int lastReadChunkSize = 0;
789 // Try reading from the buffer.
790 if (!d->buffer.isEmpty()) {
791 lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar);
792 readSoFar += lastReadChunkSize;
793 if (!sequential)
794 d->pos += lastReadChunkSize;
795 #if defined QIODEVICE_DEBUG
796 printf("%p \treading %d bytes from buffer into position %d\n", this, lastReadChunkSize,
797 int(readSoFar) - lastReadChunkSize);
798 #endif
799 } else if ((d->openMode & Unbuffered) == 0 && maxSize < QIODEVICE_BUFFERSIZE) {
800 // In buffered mode, we try to fill up the QIODevice buffer before
801 // we do anything else.
802 int bytesToBuffer = qMax(maxSize - readSoFar, QIODEVICE_BUFFERSIZE);
803 char *writePointer = d->buffer.reserve(bytesToBuffer);
805 // Make sure the device is positioned correctly.
806 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
807 return qint64(-1);
808 qint64 readFromDevice = readData(writePointer, bytesToBuffer);
809 d->buffer.chop(bytesToBuffer - (readFromDevice < 0 ? 0 : int(readFromDevice)));
811 if (readFromDevice > 0) {
812 if (!sequential)
813 d->devicePos += readFromDevice;
814 #if defined QIODEVICE_DEBUG
815 printf("%p \treading %d from device into buffer\n", this, int(readFromDevice));
816 #endif
818 if (readFromDevice < bytesToBuffer)
819 d->buffer.truncate(int(readFromDevice));
820 if (!d->buffer.isEmpty()) {
821 lastReadChunkSize = d->buffer.read(data + readSoFar, maxSize - readSoFar);
822 readSoFar += lastReadChunkSize;
823 if (!sequential)
824 d->pos += lastReadChunkSize;
825 #if defined QIODEVICE_DEBUG
826 printf("%p \treading %d bytes from buffer at position %d\n", this,
827 lastReadChunkSize, int(readSoFar));
828 #endif
833 // If we need more, try reading from the device.
834 if (readSoFar < maxSize) {
835 // Make sure the device is positioned correctly.
836 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
837 return qint64(-1);
838 qint64 readFromDevice = readData(data + readSoFar, maxSize - readSoFar);
839 #if defined QIODEVICE_DEBUG
840 printf("%p \treading %d bytes from device (total %d)\n", this, int(readFromDevice), int(readSoFar));
841 #endif
842 if (readFromDevice == -1 && readSoFar == 0) {
843 // error and we haven't read anything: return immediately
844 return -1;
846 if (readFromDevice <= 0) {
847 moreToRead = false;
848 } else {
849 // see if we read as much data as we asked for
850 if (readFromDevice < maxSize - readSoFar)
851 moreToRead = false;
853 lastReadChunkSize += int(readFromDevice);
854 readSoFar += readFromDevice;
855 if (!sequential) {
856 d->pos += readFromDevice;
857 d->devicePos += readFromDevice;
860 } else {
861 moreToRead = false;
864 if (readSoFar && d->openMode & Text) {
865 char *readPtr = data + readSoFar - lastReadChunkSize;
866 const char *endPtr = data + readSoFar;
868 if (readPtr < endPtr) {
869 // optimization to avoid initial self-assignment
870 while (*readPtr != '\r') {
871 if (++readPtr == endPtr)
872 return readSoFar;
875 char *writePtr = readPtr;
877 while (readPtr < endPtr) {
878 char ch = *readPtr++;
879 if (ch != '\r')
880 *writePtr++ = ch;
881 else
882 --readSoFar;
885 // Make sure we get more data if there is room for more. This
886 // is very important for when someone seeks to the start of a
887 // '\r\n' and reads one character - they should get the '\n'.
888 moreToRead = (readPtr != writePtr);
891 } while (moreToRead);
893 #if defined QIODEVICE_DEBUG
894 printf("%p \treturning %d, d->pos == %d, d->buffer.size() == %d\n", this,
895 int(readSoFar), int(d->pos), d->buffer.size());
896 debugBinaryString(data, readSoFar);
897 #endif
898 return readSoFar;
902 \overload
904 Reads at most \a maxSize bytes from the device, and returns the
905 data read as a QByteArray.
907 This function has no way of reporting errors; returning an empty
908 QByteArray() can mean either that no data was currently available
909 for reading, or that an error occurred.
911 QByteArray QIODevice::read(qint64 maxSize)
913 Q_D(QIODevice);
914 QByteArray result;
916 CHECK_MAXLEN(read, result);
918 #if defined QIODEVICE_DEBUG
919 printf("%p QIODevice::read(%d), d->pos = %d, d->buffer.size() = %d\n",
920 this, int(maxSize), int(d->pos), int(d->buffer.size()));
921 #else
922 Q_UNUSED(d);
923 #endif
925 if (maxSize != qint64(int(maxSize))) {
926 qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
927 maxSize = INT_MAX;
930 qint64 readBytes = 0;
931 if (maxSize) {
932 result.resize(int(maxSize));
933 if (!result.size()) {
934 // If resize fails, read incrementally.
935 qint64 readResult;
936 do {
937 result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
938 readResult = read(result.data() + readBytes, result.size() - readBytes);
939 if (readResult > 0 || readBytes == 0)
940 readBytes += readResult;
941 } while (readResult == QIODEVICE_BUFFERSIZE);
942 } else {
943 readBytes = read(result.data(), result.size());
947 if (readBytes <= 0)
948 result.clear();
949 else
950 result.resize(int(readBytes));
952 return result;
956 \overload
958 Reads all available data from the device, and returns it as a
959 QByteArray.
961 This function has no way of reporting errors; returning an empty
962 QByteArray() can mean either that no data was currently available
963 for reading, or that an error occurred.
965 QByteArray QIODevice::readAll()
967 Q_D(QIODevice);
968 #if defined QIODEVICE_DEBUG
969 printf("%p QIODevice::readAll(), d->pos = %d, d->buffer.size() = %d\n",
970 this, int(d->pos), int(d->buffer.size()));
971 #endif
973 QByteArray result;
974 qint64 readBytes = 0;
975 if (d->isSequential() || (readBytes = size()) == 0) {
976 // Size is unknown, read incrementally.
977 qint64 readResult;
978 do {
979 result.resize(result.size() + QIODEVICE_BUFFERSIZE);
980 readResult = read(result.data() + readBytes, result.size() - readBytes);
981 if (readResult > 0 || readBytes == 0)
982 readBytes += readResult;
983 } while (readResult > 0);
984 } else {
985 // Read it all in one go.
986 // If resize fails, don't read anything.
987 result.resize(int(readBytes - d->pos));
988 readBytes = read(result.data(), result.size());
991 if (readBytes <= 0)
992 result.clear();
993 else
994 result.resize(int(readBytes));
996 return result;
1000 This function reads a line of ASCII characters from the device, up
1001 to a maximum of \a maxSize - 1 bytes, stores the characters in \a
1002 data, and returns the number of bytes read. If a line could not be
1003 read but no error ocurred, this function returns 0. If an error
1004 occurs, this function returns what it could the length of what
1005 could be read, or -1 if nothing was read.
1007 A terminating '\0' byte is always appended to \a data, so \a
1008 maxSize must be larger than 1.
1010 Data is read until either of the following conditions are met:
1012 \list
1013 \o The first '\n' character is read.
1014 \o \a maxSize - 1 bytes are read.
1015 \o The end of the device data is detected.
1016 \endlist
1018 For example, the following code reads a line of characters from a
1019 file:
1021 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 2
1023 The newline character ('\n') is included in the buffer. If a
1024 newline is not encountered before maxSize - 1 bytes are read, a
1025 newline will not be inserted into the buffer. On windows newline
1026 characters are replaced with '\n'.
1028 This function calls readLineData(), which is implemented using
1029 repeated calls to getChar(). You can provide a more efficient
1030 implementation by reimplementing readLineData() in your own
1031 subclass.
1033 \sa getChar(), read(), write()
1035 qint64 QIODevice::readLine(char *data, qint64 maxSize)
1037 Q_D(QIODevice);
1038 if (maxSize < 2) {
1039 qWarning("QIODevice::readLine: Called with maxSize < 2");
1040 return qint64(-1);
1043 #if defined QIODEVICE_DEBUG
1044 printf("%p QIODevice::readLine(%p, %d), d->pos = %d, d->buffer.size() = %d\n",
1045 this, data, int(maxSize), int(d->pos), int(d->buffer.size()));
1046 #endif
1048 // Leave room for a '\0'
1049 --maxSize;
1051 const bool sequential = d->isSequential();
1053 qint64 readSoFar = 0;
1054 if (!d->buffer.isEmpty()) {
1055 readSoFar = d->buffer.readLine(data, maxSize);
1056 if (!sequential)
1057 d->pos += readSoFar;
1058 #if defined QIODEVICE_DEBUG
1059 printf("%p \tread from buffer: %d bytes, last character read: %hhx\n", this,
1060 int(readSoFar), data[int(readSoFar) - 1]);
1061 if (readSoFar)
1062 debugBinaryString(data, int(readSoFar));
1063 #endif
1064 #if defined(Q_OS_SYMBIAN)
1065 // Open C fgets strips '\r' but readSoFar gets returned as if it was still there
1066 if ((d->openMode & Text) &&
1067 readSoFar > 1 &&
1068 data[readSoFar - 1] == '\0' &&
1069 data[readSoFar - 2] == '\n') {
1070 --readSoFar;
1072 #endif
1073 if (readSoFar && data[readSoFar - 1] == '\n') {
1074 if (d->openMode & Text) {
1075 // QRingBuffer::readLine() isn't Text aware.
1076 if (readSoFar > 1 && data[readSoFar - 2] == '\r') {
1077 --readSoFar;
1078 data[readSoFar - 1] = '\n';
1081 data[readSoFar] = '\0';
1082 return readSoFar;
1086 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
1087 return qint64(-1);
1088 d->baseReadLineDataCalled = false;
1089 qint64 readBytes = readLineData(data + readSoFar, maxSize - readSoFar);
1090 #if defined QIODEVICE_DEBUG
1091 printf("%p \tread from readLineData: %d bytes, readSoFar = %d bytes\n", this,
1092 int(readBytes), int(readSoFar));
1093 if (readBytes > 0) {
1094 debugBinaryString(data, int(readSoFar + readBytes));
1096 #endif
1097 if (readBytes < 0) {
1098 data[readSoFar] = '\0';
1099 return readSoFar ? readSoFar : -1;
1101 readSoFar += readBytes;
1102 if (!d->baseReadLineDataCalled && !sequential) {
1103 d->pos += readBytes;
1104 // If the base implementation was not called, then we must
1105 // assume the device position is invalid and force a seek.
1106 d->devicePos = qint64(-1);
1108 data[readSoFar] = '\0';
1110 if (d->openMode & Text) {
1111 #if defined(Q_OS_SYMBIAN)
1112 // Open C fgets strips '\r' but readSoFar gets returned as if it was still there
1113 if (readSoFar > 1 && data[readSoFar - 1] == '\0' && data[readSoFar - 2] == '\n') {
1114 --readSoFar;
1116 #endif
1117 if (readSoFar > 1 && data[readSoFar - 1] == '\n' && data[readSoFar - 2] == '\r') {
1118 data[readSoFar - 2] = '\n';
1119 data[readSoFar - 1] = '\0';
1120 --readSoFar;
1124 #if defined QIODEVICE_DEBUG
1125 printf("%p \treturning %d, d->pos = %d, d->buffer.size() = %d, size() = %d\n",
1126 this, int(readSoFar), int(d->pos), d->buffer.size(), int(size()));
1127 debugBinaryString(data, int(readSoFar));
1128 #endif
1129 return readSoFar;
1133 \overload
1135 Reads a line from the device, but no more than \a maxSize characters,
1136 and returns the result as a QByteArray.
1138 This function has no way of reporting errors; returning an empty
1139 QByteArray() can mean either that no data was currently available
1140 for reading, or that an error occurred.
1142 QByteArray QIODevice::readLine(qint64 maxSize)
1144 Q_D(QIODevice);
1145 QByteArray result;
1147 CHECK_MAXLEN(readLine, result);
1149 #if defined QIODEVICE_DEBUG
1150 printf("%p QIODevice::readLine(%d), d->pos = %d, d->buffer.size() = %d\n",
1151 this, int(maxSize), int(d->pos), int(d->buffer.size()));
1152 #else
1153 Q_UNUSED(d);
1154 #endif
1156 if (maxSize > INT_MAX) {
1157 qWarning("QIODevice::read: maxSize argument exceeds QByteArray size limit");
1158 maxSize = INT_MAX;
1161 result.resize(int(maxSize));
1162 qint64 readBytes = 0;
1163 if (!result.size()) {
1164 // If resize fails or maxSize == 0, read incrementally
1165 if (maxSize == 0)
1166 maxSize = INT_MAX;
1167 qint64 readResult;
1168 do {
1169 result.resize(int(qMin(maxSize, result.size() + QIODEVICE_BUFFERSIZE)));
1170 readResult = readLine(result.data() + readBytes, result.size() - readBytes);
1171 if (readResult > 0 || readBytes == 0)
1172 readBytes += readResult;
1173 } while (readResult == QIODEVICE_BUFFERSIZE
1174 && result[int(readBytes)] != '\n');
1175 } else
1176 readBytes = readLine(result.data(), result.size());
1178 if (readBytes <= 0)
1179 result.clear();
1180 else
1181 result.resize(readBytes);
1183 return result;
1187 Reads up to \a maxSize characters into \a data and returns the
1188 number of characters read.
1190 This function is called by readLine(), and provides its base
1191 implementation, using getChar(). Buffered devices can improve the
1192 performance of readLine() by reimplementing this function.
1194 readLine() appends a '\0' byte to \a data; readLineData() does not
1195 need to do this.
1197 If you reimplement this function, be careful to return the correct
1198 value: it should return the number of bytes read in this line,
1199 including the terminating newline, or 0 if there is no line to be
1200 read at this point. If an error occurs, it should return -1 if and
1201 only if no bytes were read. Reading past EOF is considered an error.
1203 qint64 QIODevice::readLineData(char *data, qint64 maxSize)
1205 Q_D(QIODevice);
1206 qint64 readSoFar = 0;
1207 char c;
1208 int lastReadReturn = 0;
1209 d->baseReadLineDataCalled = true;
1211 while (readSoFar < maxSize && (lastReadReturn = read(&c, 1)) == 1) {
1212 *data++ = c;
1213 ++readSoFar;
1214 if (c == '\n')
1215 break;
1218 #if defined QIODEVICE_DEBUG
1219 printf("%p QIODevice::readLineData(%p, %d), d->pos = %d, d->buffer.size() = %d, returns %d\n",
1220 this, data, int(maxSize), int(d->pos), int(d->buffer.size()), int(readSoFar));
1221 #endif
1222 if (lastReadReturn != 1 && readSoFar == 0)
1223 return isSequential() ? lastReadReturn : -1;
1224 return readSoFar;
1228 Returns true if a complete line of data can be read from the device;
1229 otherwise returns false.
1231 Note that unbuffered devices, which have no way of determining what
1232 can be read, always return false.
1234 This function is often called in conjunction with the readyRead()
1235 signal.
1237 Subclasses that reimplement this function must call the base
1238 implementation in order to include the contents of the QIODevice's buffer. Example:
1240 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 3
1242 \sa readyRead(), readLine()
1244 bool QIODevice::canReadLine() const
1246 return d_func()->buffer.canReadLine();
1250 Writes at most \a maxSize bytes of data from \a data to the
1251 device. Returns the number of bytes that were actually written, or
1252 -1 if an error occurred.
1254 \sa read() writeData()
1256 qint64 QIODevice::write(const char *data, qint64 maxSize)
1258 Q_D(QIODevice);
1259 CHECK_WRITABLE(write, qint64(-1));
1260 CHECK_MAXLEN(write, qint64(-1));
1262 const bool sequential = d->isSequential();
1263 // Make sure the device is positioned correctly.
1264 if (d->pos != d->devicePos && !sequential && !seek(d->pos))
1265 return qint64(-1);
1267 #ifdef Q_OS_WIN
1268 if (d->openMode & Text) {
1269 const char *endOfData = data + maxSize;
1270 const char *startOfBlock = data;
1272 qint64 writtenSoFar = 0;
1274 forever {
1275 const char *endOfBlock = startOfBlock;
1276 while (endOfBlock < endOfData && *endOfBlock != '\n')
1277 ++endOfBlock;
1279 qint64 blockSize = endOfBlock - startOfBlock;
1280 if (blockSize > 0) {
1281 qint64 ret = writeData(startOfBlock, blockSize);
1282 if (ret <= 0) {
1283 if (writtenSoFar && !sequential)
1284 d->buffer.skip(writtenSoFar);
1285 return writtenSoFar ? writtenSoFar : ret;
1287 if (!sequential) {
1288 d->pos += ret;
1289 d->devicePos += ret;
1291 writtenSoFar += ret;
1294 if (endOfBlock == endOfData)
1295 break;
1297 qint64 ret = writeData("\r\n", 2);
1298 if (ret <= 0) {
1299 if (writtenSoFar && !sequential)
1300 d->buffer.skip(writtenSoFar);
1301 return writtenSoFar ? writtenSoFar : ret;
1303 if (!sequential) {
1304 d->pos += ret;
1305 d->devicePos += ret;
1307 ++writtenSoFar;
1309 startOfBlock = endOfBlock + 1;
1312 if (writtenSoFar && !sequential)
1313 d->buffer.skip(writtenSoFar);
1314 return writtenSoFar;
1316 #endif
1318 qint64 written = writeData(data, maxSize);
1319 if (written > 0) {
1320 if (!sequential) {
1321 d->pos += written;
1322 d->devicePos += written;
1324 if (!d->buffer.isEmpty() && !sequential)
1325 d->buffer.skip(written);
1327 return written;
1331 \since 4.5
1333 \overload
1335 Writes data from a zero-terminated string of 8-bit characters to the
1336 device. Returns the number of bytes that were actually written, or
1337 -1 if an error occurred. This is equivalent to
1338 \code
1340 QIODevice::write(data, qstrlen(data));
1342 \endcode
1344 \sa read() writeData()
1346 qint64 QIODevice::write(const char *data)
1348 return write(data, qstrlen(data));
1351 /*! \fn qint64 QIODevice::write(const QByteArray &byteArray)
1353 \overload
1355 Writes the content of \a byteArray to the device. Returns the number of
1356 bytes that were actually written, or -1 if an error occurred.
1358 \sa read() writeData()
1362 Puts the character \a c back into the device, and decrements the
1363 current position unless the position is 0. This function is
1364 usually called to "undo" a getChar() operation, such as when
1365 writing a backtracking parser.
1367 If \a c was not previously read from the device, the behavior is
1368 undefined.
1370 void QIODevice::ungetChar(char c)
1372 Q_D(QIODevice);
1373 CHECK_READABLE(read, Q_VOID);
1375 #if defined QIODEVICE_DEBUG
1376 printf("%p QIODevice::ungetChar(0x%hhx '%c')\n", this, c, isprint(c) ? c : '?');
1377 #endif
1379 d->buffer.ungetChar(c);
1380 if (!d->isSequential())
1381 --d->pos;
1384 /*! \fn bool QIODevice::putChar(char c)
1386 Writes the character \a c to the device. Returns true on success;
1387 otherwise returns false.
1389 \sa write() getChar() ungetChar()
1391 bool QIODevice::putChar(char c)
1393 return d_func()->putCharHelper(c);
1397 \internal
1399 bool QIODevicePrivate::putCharHelper(char c)
1401 return q_func()->write(&c, 1) == 1;
1404 /*! \fn bool QIODevice::getChar(char *c)
1406 Reads one character from the device and stores it in \a c. If \a c
1407 is 0, the character is discarded. Returns true on success;
1408 otherwise returns false.
1410 \sa read() putChar() ungetChar()
1412 bool QIODevice::getChar(char *c)
1414 Q_D(QIODevice);
1415 const OpenMode openMode = d->openMode;
1416 if (!(openMode & ReadOnly)) {
1417 if (openMode == NotOpen)
1418 qWarning("QIODevice::getChar: Closed device");
1419 else
1420 qWarning("QIODevice::getChar: WriteOnly device");
1421 return false;
1424 // Shortcut for QIODevice::read(c, 1)
1425 QRingBuffer *buffer = &d->buffer;
1426 const int chint = buffer->getChar();
1427 if (chint != -1) {
1428 char ch = char(uchar(chint));
1429 if ((openMode & Text) && ch == '\r') {
1430 buffer->ungetChar(ch);
1431 } else {
1432 if (c)
1433 *c = ch;
1434 if (!d->isSequential())
1435 ++d->pos;
1436 return true;
1440 // Fall back to read().
1441 char ch;
1442 if (read(&ch, 1) == 1) {
1443 if (c)
1444 *c = ch;
1445 return true;
1447 return false;
1451 \since 4.1
1453 Reads at most \a maxSize bytes from the device into \a data, without side
1454 effects (i.e., if you call read() after peek(), you will get the same
1455 data). Returns the number of bytes read. If an error occurs, such as
1456 when attempting to peek a device opened in WriteOnly mode, this function
1457 returns -1.
1459 0 is returned when no more data is available for reading.
1461 Example:
1463 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 4
1465 \sa read()
1467 qint64 QIODevice::peek(char *data, qint64 maxSize)
1469 qint64 readBytes = read(data, maxSize);
1470 int i = readBytes;
1471 while (i > 0)
1472 ungetChar(data[i-- - 1]);
1473 return readBytes;
1477 \since 4.1
1478 \overload
1480 Peeks at most \a maxSize bytes from the device, returning the data peeked
1481 as a QByteArray.
1483 Example:
1485 \snippet doc/src/snippets/code/src_corelib_io_qiodevice.cpp 5
1487 This function has no way of reporting errors; returning an empty
1488 QByteArray() can mean either that no data was currently available
1489 for peeking, or that an error occurred.
1491 \sa read()
1493 QByteArray QIODevice::peek(qint64 maxSize)
1495 QByteArray result = read(maxSize);
1496 int i = result.size();
1497 const char *data = result.constData();
1498 while (i > 0)
1499 ungetChar(data[i-- - 1]);
1500 return result;
1504 Blocks until new data is available for reading and the readyRead()
1505 signal has been emitted, or until \a msecs milliseconds have
1506 passed. If msecs is -1, this function will not time out.
1508 Returns true if new data is available for reading; otherwise returns
1509 false (if the operation timed out or if an error occurred).
1511 This function can operate without an event loop. It is
1512 useful when writing non-GUI applications and when performing
1513 I/O operations in a non-GUI thread.
1515 If called from within a slot connected to the readyRead() signal,
1516 readyRead() will not be reemitted.
1518 Reimplement this function to provide a blocking API for a custom
1519 device. The default implementation does nothing, and returns false.
1521 \warning Calling this function from the main (GUI) thread
1522 might cause your user interface to freeze.
1524 \sa waitForBytesWritten()
1526 bool QIODevice::waitForReadyRead(int msecs)
1528 Q_UNUSED(msecs);
1529 return false;
1533 For buffered devices, this function waits until a payload of
1534 buffered written data has been written to the device and the
1535 bytesWritten() signal has been emitted, or until \a msecs
1536 milliseconds have passed. If msecs is -1, this function will
1537 not time out. For unbuffered devices, it returns immediately.
1539 Returns true if a payload of data was written to the device;
1540 otherwise returns false (i.e. if the operation timed out, or if an
1541 error occurred).
1543 This function can operate without an event loop. It is
1544 useful when writing non-GUI applications and when performing
1545 I/O operations in a non-GUI thread.
1547 If called from within a slot connected to the bytesWritten() signal,
1548 bytesWritten() will not be reemitted.
1550 Reimplement this function to provide a blocking API for a custom
1551 device. The default implementation does nothing, and returns false.
1553 \warning Calling this function from the main (GUI) thread
1554 might cause your user interface to freeze.
1556 \sa waitForReadyRead()
1558 bool QIODevice::waitForBytesWritten(int msecs)
1560 Q_UNUSED(msecs);
1561 return false;
1565 Sets the human readable description of the last device error that
1566 occurred to \a str.
1568 \sa errorString()
1570 void QIODevice::setErrorString(const QString &str)
1572 d_func()->errorString = str;
1576 Returns a human-readable description of the last device error that
1577 occurred.
1579 \sa setErrorString()
1581 QString QIODevice::errorString() const
1583 Q_D(const QIODevice);
1584 if (d->errorString.isEmpty()) {
1585 #ifdef QT_NO_QOBJECT
1586 return QLatin1String(QT_TRANSLATE_NOOP(QIODevice, "Unknown error"));
1587 #else
1588 return tr("Unknown error");
1589 #endif
1591 return d->errorString;
1595 \fn qint64 QIODevice::readData(char *data, qint64 maxSize)
1597 Reads up to \a maxSize bytes from the device into \a data, and
1598 returns the number of bytes read or -1 if an error occurred. If
1599 there are no bytes to be read, this function should return -1 if
1600 there can never be more bytes available (for example: socket
1601 closed, pipe closed, sub-process finished).
1603 This function is called by QIODevice. Reimplement this function
1604 when creating a subclass of QIODevice.
1606 \sa read() readLine() writeData()
1610 \fn qint64 QIODevice::writeData(const char *data, qint64 maxSize)
1612 Writes up to \a maxSize bytes from \a data to the device. Returns
1613 the number of bytes written, or -1 if an error occurred.
1615 This function is called by QIODevice. Reimplement this function
1616 when creating a subclass of QIODevice.
1618 \sa read() write()
1622 \fn QIODevice::Offset QIODevice::status() const
1624 For device specific error handling, please refer to the
1625 individual device documentation.
1627 \sa qobject_cast()
1631 \fn QIODevice::Offset QIODevice::at() const
1633 Use pos() instead.
1637 \fn bool QIODevice::at(Offset offset)
1639 Use seek(\a offset) instead.
1642 /*! \fn int QIODevice::flags() const
1644 Use openMode() instead.
1647 /*! \fn int QIODevice::getch()
1649 Use getChar() instead.
1653 \fn bool QIODevice::isAsynchronous() const
1655 This functionality is no longer available. This function always
1656 returns true.
1660 \fn bool QIODevice::isBuffered() const
1662 Use !(openMode() & QIODevice::Unbuffered) instead.
1666 \fn bool QIODevice::isCombinedAccess() const
1668 Use openMode() instead.
1672 \fn bool QIODevice::isDirectAccess() const
1674 Use !isSequential() instead.
1678 \fn bool QIODevice::isInactive() const
1680 Use isOpen(), isReadable(), or isWritable() instead.
1684 \fn bool QIODevice::isRaw() const
1686 Use openMode() instead.
1690 \fn bool QIODevice::isSequentialAccess() const
1692 Use isSequential() instead.
1696 \fn bool QIODevice::isSynchronous() const
1698 This functionality is no longer available. This function always
1699 returns false.
1703 \fn bool QIODevice::isTranslated() const
1705 Use openMode() instead.
1709 \fn bool QIODevice::mode() const
1711 Use openMode() instead.
1714 /*! \fn int QIODevice::putch(int ch)
1716 Use putChar(\a ch) instead.
1719 /*! \fn int QIODevice::ungetch(int ch)
1721 Use ungetChar(\a ch) instead.
1725 \fn quint64 QIODevice::readBlock(char *data, quint64 size)
1727 Use read(\a data, \a size) instead.
1730 /*! \fn int QIODevice::state() const
1732 Use isOpen() instead.
1736 \fn qint64 QIODevice::writeBlock(const char *data, quint64 size)
1738 Use write(\a data, \a size) instead.
1742 \fn qint64 QIODevice::writeBlock(const QByteArray &data)
1744 Use write(\a data) instead.
1747 #if defined QT3_SUPPORT
1748 QIODevice::Status QIODevice::status() const
1750 #if !defined(QT_NO_QOBJECT)
1751 const QFile *f = qobject_cast<const QFile *>(this);
1752 if (f) return (int) f->error();
1753 #endif
1754 return isOpen() ? 0 /* IO_Ok */ : 8 /* IO_UnspecifiedError */;
1758 For device specific error handling, please refer to the
1759 individual device documentation.
1761 \sa qobject_cast()
1763 void QIODevice::resetStatus()
1765 #if !defined(QT_NO_QOBJECT)
1766 QFile *f = qobject_cast<QFile *>(this);
1767 if (f) f->unsetError();
1768 #endif
1770 #endif
1772 #if !defined(QT_NO_DEBUG_STREAM)
1773 QDebug operator<<(QDebug debug, QIODevice::OpenMode modes)
1775 debug << "OpenMode(";
1776 QStringList modeList;
1777 if (modes == QIODevice::NotOpen) {
1778 modeList << QLatin1String("NotOpen");
1779 } else {
1780 if (modes & QIODevice::ReadOnly)
1781 modeList << QLatin1String("ReadOnly");
1782 if (modes & QIODevice::WriteOnly)
1783 modeList << QLatin1String("WriteOnly");
1784 if (modes & QIODevice::Append)
1785 modeList << QLatin1String("Append");
1786 if (modes & QIODevice::Truncate)
1787 modeList << QLatin1String("Truncate");
1788 if (modes & QIODevice::Text)
1789 modeList << QLatin1String("Text");
1790 if (modes & QIODevice::Unbuffered)
1791 modeList << QLatin1String("Unbuffered");
1793 qSort(modeList);
1794 debug << modeList.join(QLatin1String("|"));
1795 debug << ')';
1796 return debug;
1798 #endif
1800 QT_END_NAMESPACE