Qt写文件的缓存buffer大小与编码格式

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu) 


几个编程经常碰到的数字:

1024     = 2^10

2048         = 2^11

4096         = 2^12

8192         = 2^13

16384       = 2^14

32768       = 2^15

65536       = 2^16

 

而QT中写文件的缺省缓存大小就是这些数字中的一个:16384 (16k)

 

QFile file(strFile);

if (file.open(QIODevice::WriteOnly|QIODevice::Text|QIODevice::Truncate))

{

   QTextStream stream(&file);

   stream << << endl;

   …

   file.close();

}

 

 

这其中涉及两个qt类:QFile与QTextStream

 

QFile中的buffer控制在read时指定,缺省使用buffer,不使用时openmode指定Unbuffered

    enum OpenModeFlag {

        NotOpen = 0x0000,

        ReadOnly = 0x0001,

        WriteOnly = 0x0002,

        ReadWrite = ReadOnly | WriteOnly,

        Append = 0x0004,

        Truncate = 0x0008,

        Text = 0x0010,

        Unbuffered = 0x0020

    };

 

QFile的buffer size宏使用的:QIODEVICE_BUFFERSIZE

定义参见qiodevice_p.h

#ifndef QIODEVICE_BUFFERSIZE

#define QIODEVICE_BUFFERSIZE Q_INT64_C(16384)

#endif

 

 

QTextStream的buffer size宏使用的:QTEXTSTREAM_BUFFERSIZE,未提供禁用buffer的处理,buffer总是有效的。

定义参见 qtextstream.cpp

static const int QTEXTSTREAM_BUFFERSIZE = 16384;

 

inline void QTextStreamPrivate::write(const QString &data)

{

    if (string) {

        // ### What about seek()??

        string->append(data);

    } else {

        writeBuffer += data;

        if (writeBuffer.size() > QTEXTSTREAM_BUFFERSIZE)

            flushWriteBuffer();

    }

}

 

 

QTextStream的文件格式转换缺省使用的是toLocal8Bit,如果未定义QT_NO_TEXTCODEC并且设置了setCodec,则使用转换格式:

#ifndef QT_NO_TEXTCODEC

    if (!codec)

        codec = QTextCodec::codecForLocale();

#if defined (QTEXTSTREAM_DEBUG)

    qDebug("QTextStreamPrivate::flushWriteBuffer(), using %s codec (%s generating BOM)",

           codec->name().constData(), writeConverterState.flags & QTextCodec::IgnoreHeader ? "not" : "");

#endif

 

    // convert from unicode to raw data

    QByteArray data = codec->fromUnicode(writeBuffer.data(), writeBuffer.size(), &writeConverterState);

#else

    QByteArray data = writeBuffer.toLocal8Bit();

#endif

 

CodeC在setCodec方法中设置:

/*!

    Sets the codec for this stream to \a codec. The codec is used for

    decoding any data that is read from the assigned device, and for

    encoding any data that is written. By default,

    QTextCodec::codecForLocale() is used, and automatic unicode

    detection is enabled.

 

    If QTextStream operates on a string, this function does nothing.

 

    \warning If you call this function while the text stream is reading

    from an open sequential socket, the internal buffer may still contain

    text decoded using the old codec.

 

    \sa codec(), setAutoDetectUnicode(), setLocale()

*/

void QTextStream::setCodec(QTextCodec *codec)

{

    Q_D(QTextStream);

    qint64 seekPos = -1;

    if (!d->readBuffer.isEmpty()) {

        if (!d->device->isSequential()) {

            seekPos = pos();

        }

    }

    d->codec = codec;

    if (seekPos >=0 && !d->readBuffer.isEmpty())

        seek(seekPos);

}

 

 

/*!

    Sets the codec for this stream to the QTextCodec for the encoding

    specified by \a codecName. Common values for \c codecName include

    "ISO 8859-1", "UTF-8", and "UTF-16". If the encoding isn't

    recognized, nothing happens.

 

    Example:

 

    \snippet code/src_corelib_io_qtextstream.cpp 10

 

    \sa QTextCodec::codecForName(), setLocale()

*/

void QTextStream::setCodec(const char *codecName)

{

    QTextCodec *codec = QTextCodec::codecForName(codecName);

    if (codec)

        setCodec(codec);

}

 

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)


你可能感兴趣的:(Qt,内存分配)