I want to create istream
from QByteArray
at runtime, without saving a physical file in memory of QByteArray
.
I found that there are many ways to do the opposite conversion, i.e. istream
to QByteArray
, but not this one.
How to accomplish that?
To read via
std::istringstream
fromQByteArray
seems quite easy:testQByteArray-istream.cc
:testQByteArray-istream.pro
:Compiled and tested on cygwin64:
Done. Stop, wait!
I'm not quite sure how to read this. Probably, it means
without copying data saved in
QByteArray
I see only two solutions:
Use a
QDataStream
instead ofstd::stream
. According to doc.QDataStream::QDataStream(const QByteArray &a)
This sounds very promising that data is not copied.
DIY. Make a class derived from
std::stream
which may read from aQByteArray
without copying.Concerning 2. option, I found Dietmar Kühl's answer to SO: Creating an input stream from constant memory. Applying this to the above sample, it would look like this:
Compiled and tested again on cygwin64: