|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcom.trolltech.qt.QSignalEmitter
com.trolltech.qt.QtJambiObject
com.trolltech.qt.core.QObject
com.trolltech.qt.core.QIODevice
public abstract class QIODevice
The QIODevice
class is the base interface class of all I/O devices in Qt. QIODevice
provides both a common implementation and an abstract interface for devices that support reading and writing of blocks of data, such as QFile
, QBuffer
and QTcpSocket
. QIODevice
is abstract and can not be instantiated, but it is common to use the interface it defines to provide device-independent I/O features. For example, Qt's XML classes operate on a QIODevice
pointer, allowing them to be used with various devices (such as files and buffers).
Before accessing the device, open()
must be called to set the correct OpenMode (such as ReadOnly
or ReadWrite
). You can then write to the device with write()
or putChar(), and read by calling either read()
, readLine()
, or readAll()
. Call close()
when you are done with the device.
QIODevice
distinguishes between two types of devices: random-access devices and sequential devices.
seek()
. The current position in the file is available by calling pos()
. QFile
and QBuffer
are examples of random-access devices.pos()
and size()
don't work for sequential devices. QTcpSocket
and QProcess
are examples of sequential devices.isSequential()
to determine the type of device. QIODevice
emits readyRead()
when new data is available for reading; for example, if new data has arrived on the network or if additional data is appended to a file that you are reading from. You can call bytesAvailable()
to determine the number of bytes that currently available for reading. It's common to use bytesAvailable()
together with the readyRead()
signal when programming with asynchronous devices such as QTcpSocket
, where fragments of data can arrive at arbitrary points in time. QIODevice
emits the bytesWritten()
signal every time a payload of data has been written to the device. Use bytesToWrite()
to determine the current amount of data waiting to be written.
Certain subclasses of QIODevice
, such as QTcpSocket
and QProcess
, are asynchronous. This means that I/O functions such as write()
or read()
always return immediately, while communication with the device itself may happen when control goes back to the event loop. QIODevice
provides functions that allow you to force these operations to be performed immediately, while blocking the calling thread and without entering the event loop. This allows QIODevice
subclasses to be used without an event loop, or in a separate thread:
waitForReadyRead()
- This function suspends operation in the calling thread until new data is available for reading.waitForBytesWritten()
- This function suspends operation in the calling thread until one payload of data has been written to the device.waitFor
....() - Subclasses of QIODevice
implement blocking functions for device-specific operations. For example, QProcess
has a function called waitForStarted()
which suspends operation in the calling thread until the process has started.QProcess gzip = new QProcess(); gzip.start("gzip", Arrays.asList("-c")); if (!gzip.waitForStarted()) return false; gzip.write(new QByteArray("uncompressed data")); QByteArray compressed = new QByteArray(); while (gzip.waitForReadyRead(5000)) compressed.append(gzip.readAll());By subclassing
QIODevice
, you can provide the same interface to your own I/O devices. Subclasses of QIODevice
are only required to implement the protected readData()
and writeData()
functions. QIODevice
uses these functions to implement all its convenience functions, such as getChar(), readLine()
and write()
. QIODevice
also handles access control for you, so you can safely assume that the device is opened in write mode if writeData()
is called. Some subclasses, such as QFile
and QTcpSocket
, are implemented using a memory buffer for intermediate storing of data. This reduces the number of required device accessing calls, which are often very slow. Buffering makes functions like getChar() and putChar() fast, as they can operate on the memory buffer instead of directly on the device itself. Certain I/O operations, however, don't work well with a buffer. For example, if several users open the same device and read it character by character, they may end up reading the same data when they meant to read a separate chunk each. For this reason, QIODevice
allows you to bypass any buffering by passing the Unbuffered flag to open()
. When subclassing QIODevice
, remember to bypass any buffer you may use when the device is open in Unbuffered mode.
QBuffer
, QFile
, and QTcpSocket
.
Nested Class Summary | |
---|---|
static class |
QIODevice.OpenMode
This is a flags class for com.trolltech.qt.core.QIODevice.OpenModeFlag |
static class |
QIODevice.OpenModeFlag
This enum is used with open() to describe the mode in which a device is opened. |
Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter |
---|
QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9 |
Field Summary | |
---|---|
QSignalEmitter.Signal0 |
aboutToClose
This signal is emitted when the device is about to close. |
QSignalEmitter.Signal1 |
bytesWritten
This signal is emitted every time a payload of data has been written to the device. |
QSignalEmitter.Signal0 |
readChannelFinished
This signal is emitted when the input (reading) stream is closed in this device. |
QSignalEmitter.Signal0 |
readyRead
This signal is emitted once every time new data is available for reading from the device. |
Constructor Summary | |
---|---|
QIODevice()
Constructs a QIODevice object. |
|
QIODevice(QObject parent)
Constructs a QIODevice object with the given parent. |
Method Summary | |
---|---|
boolean |
atEnd()
Returns true if the current read and write position is at the end of the device (i.e. |
long |
bytesAvailable()
Returns the number of bytes that are available for reading. |
long |
bytesToWrite()
For buffered devices, this function returns the number of bytes waiting to be written. |
boolean |
canReadLine()
Returns true if a complete line of data can be read from the device; otherwise returns false. |
void |
close()
First emits aboutToClose() , then closes the device and sets its OpenMode to NotOpen . |
java.lang.String |
errorString()
Returns a human-readable description of the last device error that occurred. |
static QIODevice |
fromNativePointer(QNativePointer nativePointer)
|
int |
getByte()
Gets a byte from the device. |
boolean |
isOpen()
Returns true if the device is open; otherwise returns false. |
boolean |
isReadable()
Returns true if data can be read from the device; otherwise returns false. |
boolean |
isSequential()
Returns true if this device is sequential; otherwise returns false. |
boolean |
isTextModeEnabled()
Returns true if the Text flag is enabled; otherwise returns false. |
boolean |
isWritable()
Returns true if data can be written to the device; otherwise returns false. |
boolean |
open(QIODevice.OpenMode mode)
Opens the device and sets its OpenMode to mode. |
boolean |
open(QIODevice.OpenModeFlag[] mode)
Opens the device and sets its OpenMode to mode. |
QIODevice.OpenMode |
openMode()
Returns the mode in which the device has been opened; i.e. |
int |
peek(byte[] data)
|
QByteArray |
peek(long maxlen)
Peeks at most maxSize bytes from the device, returning the data peeked as a QByteArray . |
long |
pos()
For random-access devices, this function returns the position that data is written to or read from. |
boolean |
putByte(byte c)
Writes the character c to the device. |
int |
read(byte[] data)
|
QByteArray |
read(long maxlen)
Reads at most maxSize bytes from the device, and returns the data read as a QByteArray . |
QByteArray |
readAll()
Reads all available data from the device, and returns it as a QByteArray . |
protected abstract int |
readData(byte[] data)
Reads up to maxSize bytes from the device into data, and returns the number of bytes read or -1 if an error occurred. |
QByteArray |
readLine()
Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray . |
int |
readLine(byte[] data)
|
QByteArray |
readLine(long maxlen)
Reads a line from the device, but no more than maxSize characters, and returns the result as a QByteArray . |
protected int |
readLineData(byte[] data)
Reads up to maxSize characters into data and returns the number of characters read. |
boolean |
reset()
Seeks to the start of input for random-access devices. |
boolean |
seek(long pos)
For random-access devices, this function sets the current position to pos, returning true on success, or false if an error occurred. |
protected void |
setErrorString(java.lang.String errorString)
Sets the human readable description of the last device error that occurred to str. |
protected void |
setOpenMode(QIODevice.OpenMode openMode)
Sets the OpenMode of the device to openMode. |
protected void |
setOpenMode(QIODevice.OpenModeFlag[] openMode)
Sets the OpenMode of the device to openMode. |
void |
setTextModeEnabled(boolean enabled)
If enabled is true, this function sets the Text flag on the device; otherwise the Text flag is removed. |
long |
size()
For open random-access devices, this function returns the size of the device. |
void |
ungetByte(byte c)
Puts the character c back into the device, and decrements the current position unless the position is 0. |
boolean |
waitForBytesWritten(int msecs)
For buffered devices, this function waits until a payload of buffered written data has been written to the device and the bytesWritten() signal has been emitted, or until msecs milliseconds have passed. |
boolean |
waitForReadyRead(int msecs)
Blocks until data is available for reading and the readyRead() signal has been emitted, or until msecs milliseconds have passed. |
int |
write(byte[] data)
|
long |
write(QByteArray data)
Writes the content of byteArray to the device. |
protected abstract int |
writeData(byte[] data)
Writes up to maxSize bytes from data to the device. |
Methods inherited from class com.trolltech.qt.core.QObject |
---|
childEvent, children, connectSlotsByName, customEvent, disposeLater, dumpObjectInfo, dumpObjectTree, dynamicPropertyNames, event, eventFilter, findChild, findChild, findChild, findChildren, findChildren, findChildren, findChildren, indexOfProperty, installEventFilter, isWidgetType, killTimer, moveToThread, objectName, parent, properties, property, removeEventFilter, setObjectName, setParent, setProperty, startTimer, timerEvent, toString, userProperty |
Methods inherited from class com.trolltech.qt.QtJambiObject |
---|
dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr |
Methods inherited from class com.trolltech.qt.QSignalEmitter |
---|
blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread |
Methods inherited from class java.lang.Object |
---|
clone, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface com.trolltech.qt.QtJambiInterface |
---|
disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership |
Field Detail |
---|
public final QSignalEmitter.Signal0 aboutToClose
public final QSignalEmitter.Signal1 bytesWritten
bytesWritten()
is not emitted recursively; if you reenter the event loop or call waitForBytesWritten()
inside a slot connected to the bytesWritten()
signal, the signal will not be reemitted (although waitForBytesWritten()
may still return true).
readyRead()
.
public final QSignalEmitter.Signal0 readChannelFinished
read()
. atEnd()
, and read()
.
public final QSignalEmitter.Signal0 readyRead
readyRead()
is not emitted recursively; if you reenter the event loop or call waitForReadyRead()
inside a slot connected to the readyRead()
signal, the signal will not be reemitted (although waitForReadyRead()
may still return true).
Note for developers implementing classes derived from QIODevice
: you should always emit readyRead()
when new data has arrived (do not emit it only because there's data still to be read in your buffers). Do not emit readyRead()
in other conditions.
bytesWritten()
.
Constructor Detail |
---|
public QIODevice()
QIODevice
object.
public QIODevice(QObject parent)
QIODevice
object with the given parent.
Method Detail |
---|
public final java.lang.String errorString()
setErrorString()
.
public final boolean isOpen()
openMode()
returns NotOpen. openMode()
, and OpenMode.
public final boolean isReadable()
bytesAvailable()
to determine how many bytes can be read. This is a convenience function which checks if the OpenMode of the device contains the ReadOnly
flag.
openMode()
, and OpenMode.
public final boolean isTextModeEnabled()
Text
flag is enabled; otherwise returns false. setTextModeEnabled()
.
public final boolean isWritable()
This is a convenience function which checks if the OpenMode of the device contains the WriteOnly
flag.
openMode()
, and OpenMode.
public final QIODevice.OpenMode openMode()
ReadOnly
or WriteOnly
. setOpenMode()
, and OpenMode.
public final QByteArray peek(long maxlen)
QByteArray
. Example:
public boolean isExeFile(QFile file) { return file.peek(2).equals("MZ"); }This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for peeking, or that an error occurred.
read()
.
public final boolean putByte(byte c)
write()
, getChar(), and ungetChar().
public final QByteArray read(long maxlen)
QByteArray
. This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.
public final QByteArray readAll()
QByteArray
. This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.
public final QByteArray readLine()
QByteArray
. This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.
public final QByteArray readLine(long maxlen)
QByteArray
. This function has no way of reporting errors; returning an empty QByteArray() can mean either that no data was currently available for reading, or that an error occurred.
protected final void setErrorString(java.lang.String errorString)
errorString()
.
protected final void setOpenMode(QIODevice.OpenModeFlag[] openMode)
openMode()
, and OpenMode.
protected final void setOpenMode(QIODevice.OpenMode openMode)
openMode()
, and OpenMode.
public final void setTextModeEnabled(boolean enabled)
Text
flag on the device; otherwise the Text
flag is removed. This feature is useful for classes that provide custom end-of-line handling on a QIODevice
. isTextModeEnabled()
, open()
, and setOpenMode()
.
public final void ungetByte(byte c)
If c was not previously read from the device, the behavior is undefined.
public final long write(QByteArray data)
read()
, and writeData()
.
public boolean atEnd()
For some devices, atEnd()
can return true even though there is more data to read. This special case only applies to devices that generate data in direct response to you calling read()
(e.g., /dev or /proc files on Unix and Mac OS X, or console input / stdin on all platforms).
bytesAvailable()
, read()
, and isSequential()
.
public long bytesAvailable()
Subclasses that reimplement this function must call the base implementation in order to include the size of QIODevices
' buffer. Example:
public long bytesAvailable() { return buffer.size() + super.bytesAvailable(); }
bytesToWrite()
, readyRead()
, and isSequential()
.
public long bytesToWrite()
bytesAvailable()
, bytesWritten()
, and isSequential()
.
public boolean canReadLine()
Note that unbuffered devices, which have no way of determining what can be read, always return false.
This function is often called in conjunction with the readyRead()
signal.
Subclasses that reimplement this function must call the base implementation in order to include the size of the QIODevice
's buffer. Example:
public boolean canReadLine() { return buffer.contains("\n") || super.canReadLine(); }
readyRead()
, and readLine()
.
public void close()
aboutToClose()
, then closes the device and sets its OpenMode to NotOpen
. The error string is also reset. setOpenMode()
, and OpenMode.
public boolean isSequential()
Sequential devices, as opposed to a random-access devices, have no concept of a start, an end, a size, or a current position, and they do not support seeking. You can only read from the device when it reports that data is available. The most common example of a sequential device is a network socket. On Unix, special files such as /dev/zero and fifo pipes are sequential.
Regular files, on the other hand, do support random access. They have both a size and a current position, and they also support seeking backwards and forwards in the data stream. Regular files are non-sequential.
bytesAvailable()
.
public final boolean open(QIODevice.OpenModeFlag[] mode)
open()
or other functions that open the device. openMode()
, and OpenMode.
public boolean open(QIODevice.OpenMode mode)
open()
or other functions that open the device. openMode()
, and OpenMode.
public long pos()
The current read/write position of the device is maintained internally by QIODevice
, so reimplementing this function is not necessary. When subclassing QIODevice
, use QIODevice::seek()
to notify QIODevice
about changes in the device position.
isSequential()
, and seek()
.
protected abstract int readData(byte[] data)
This function is called by QIODevice
. Reimplement this function when creating a subclass of QIODevice
.
read()
, readLine()
, and writeData()
.
protected int readLineData(byte[] data)
This function is called by readLine()
, and provides its base implementation, using getChar(). Buffered devices can improve the performance of readLine()
by reimplementing this function.
readLine()
appends a '\0' byte to data; readLineData()
does not need to do this.
If you reimplement this function, be careful to return the correct value: it should return the number of bytes read in this line, including the terminating newline, or 0 if there is no line to be read at this point. If an error occurs, it should return -1 if and only if no bytes were read. Reading past EOF is considered an error.
public boolean reset()
Note that when using a QTextStream
on a QFile
, calling reset()
on the QFile
will not have the expected result because QTextStream
buffers the file. Use the QTextStream::seek()
function instead.
seek()
.
public boolean seek(long pos)
When subclassing QIODevice
, you must call QIODevice::seek()
at the start of your function to ensure integrity with QIODevice
's built-in buffer. The base implementation always returns true.
pos()
, and isSequential()
.
public long size()
bytesAvailable()
is returned. If the device is closed, the size returned will not reflect the actual size of the device.
isSequential()
, and pos()
.
public boolean waitForBytesWritten(int msecs)
bytesWritten()
signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out. For unbuffered devices, it returns immediately. Returns true if a payload of data was written to the device; otherwise returns false (i.e. if the operation timed out, or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
If called from within a slot connected to the bytesWritten()
signal, bytesWritten()
will not be reemitted.
Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
waitForReadyRead()
.
public boolean waitForReadyRead(int msecs)
readyRead()
signal has been emitted, or until msecs milliseconds have passed. If msecs is -1, this function will not time out. Returns true if data is available for reading; otherwise returns false (if the operation timed out or if an error occurred).
This function can operate without an event loop. It is useful when writing non-GUI applications and when performing I/O operations in a non-GUI thread.
If called from within a slot connected to the readyRead()
signal, readyRead()
will not be reemitted.
Reimplement this function to provide a blocking API for a custom device. The default implementation does nothing, and returns false.
Warning: Calling this function from the main (GUI) thread might cause your user interface to freeze.
waitForBytesWritten()
.
protected abstract int writeData(byte[] data)
This function is called by QIODevice
. Reimplement this function when creating a subclass of QIODevice
.
read()
, and write()
.
public static QIODevice fromNativePointer(QNativePointer nativePointer)
public final int getByte()
public final int peek(byte[] data)
public final int read(byte[] data)
public final int readLine(byte[] data)
public final int write(byte[] data)
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |