惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

小众软件
小众软件
IT之家
IT之家
博客园 - 聂微东
www.infosecurity-magazine.com
www.infosecurity-magazine.com
P
Privacy International News Feed
人人都是产品经理
人人都是产品经理
PCI Perspectives
PCI Perspectives
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
美团技术团队
S
Secure Thoughts
N
News | PayPal Newsroom
L
LINUX DO - 最新话题
腾讯CDC
Application and Cybersecurity Blog
Application and Cybersecurity Blog
雷峰网
雷峰网
B
Blog
MyScale Blog
MyScale Blog
T
The Blog of Author Tim Ferriss
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
Blog — PlanetScale
Blog — PlanetScale
C
Check Point Blog
T
Tailwind CSS Blog
月光博客
月光博客
Simon Willison's Weblog
Simon Willison's Weblog
Hacker News: Ask HN
Hacker News: Ask HN
The Last Watchdog
The Last Watchdog
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
S
Security @ Cisco Blogs
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
S
Security Affairs
Forbes - Security
Forbes - Security
P
Palo Alto Networks Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
博客园 - 司徒正美
博客园 - 三生石上(FineUI控件)
T
Tor Project blog
O
OpenAI News
L
Lohrmann on Cybersecurity
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LangChain Blog
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More

博客园 - chinese_submarine

QT项目性能调优小记 Windows下HG服务器的搭建 svn+tp-link+花生壳搭建外网服务器 udev介绍 极大极小博弈树的简洁(附Tic-Tac-Toe源码) 程序优化小记 Beating the Average------为什么要学习Lisp[转] 巧用qmake工具生成专业的makefile QT中拖拽的实现(附示例代码) 如何保持GUI的响应流畅(QT平台) 也谈线程同步变量 windows7到期的问题 简述FPS的计算方法 QT中的View Model模型系列一 从农夫养牛问题推广到斐波那契数列 TimeZoneChange事件的捕获 浏览器扩展系列————透明浏览器窗口的实现 浏览器扩展系列————异步可插入协议(pluggable protocol)的实现 浏览器扩展系列————给MSTHML添加内置脚本对象【包括自定义事件】
从QDataStream向QByteArray中写入数据时的注意点(QT)
chinese_submarine · 2010-05-29 · via 博客园 - chinese_submarine

最近发现从QDataStream向QByteArray中写入数据常常是写不进去的,通过查看QT的源码:

代码

QDataStream &operator>>(QDataStream &in, QByteArray &ba)
{
ba.clear();
quint32 len;
in >> len;
if (len == 0xffffffff)
return in;const quint32 Step = 1024 * 1024;
quint32 allocated
= 0;do {
int blockSize = qMin(Step, len - allocated);
ba.resize(allocated
+ blockSize);
if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {
ba.clear();
in.setStatus(QDataStream::ReadPastEnd);
return in;
}
allocated
+= blockSize;
}
while (allocated < len);return in;
}

发现原来其中有一句:

if (in.readRawData(ba.data() + allocated, blockSize) != blockSize) {

它检查读入的数据是不是整数个blockSize,如果不是,那么下面它会将QByteArray清空:

 ba.clear();

所以如果想从QDataStream读入数据岛QByteArray中,可以采用下面的方法:

代码

QDataStream &operator>>(QDataStream &in, QByteArray &ba)
{
ba.clear();
quint32 len;
in >> len;
if (len == 0xffffffff)
return in;const quint32 Step = 1024 * 1024;
quint32 allocated
= 0;do {
int blockSize = qMin(Step, len - allocated);
ba.resize(allocated
+ blockSize);
if (in.readRawData(ba.data() + allocated, blockSize) < blockSize) {
return in;
}
allocated
+= blockSize;
}
while (allocated < len);return in;
}

最后回过头来看一下将QByteArray写入QDataStream的方法

代码

QDataStream &operator<<(QDataStream &out, const QByteArray &ba)
{
if (ba.isNull() && out.version() >= 6) {
out << (quint32)0xffffffff;
return out;
}
return out.writeBytes(ba, ba.size());
}

其实是将QByteArray中所有的数据都写入QDatStream中,并没有扩展QByteArray的大小到blockSize,

所以针对这种情况,我们在使用QDataStream向QByteArray中读写数据的时候,需要手动将QByteArray

扩展到blockSize(1024*1024)大小。

posted on 2010-05-29 10:19  chinese_submarine  阅读(9151)  评论()    收藏  举报