From QDataStream to QDataStream (Merging streams)



  • Hi fellas,

    Is it somehow possible to write from a datastream into a datastream!?
    So to say: I want to "merge" two QDataStreams!

    Something like this:
    @QByteArray b;

    QDataStream out(&b, QIODevice::ReadWrite);

    //serialize some data to b
    out<<(int)2;
    out<<(int)3;
    out<<(int)4;

    //message will contain some aditional info + the serialized data
    QByteArray message;
    QDataStream msgOut(&message, QIODevice::WriteOnly);

    msgOut<<(int)1;
    out.device()->seek(0);
    msgOut<<out; //write serialized to the stream

    //now try to deserialize from message
    QDataStream msgIn(&message, QIODevice::ReadOnly);

    int var1;
    int var2;
    int var3;
    int var4;

    msgIn>>var1;
    msgIn>>var2;
    msgIn>>var3;
    msgIn>>var4;

    std::cout<<"Var1: "<<var1<<std::endl; //contains 1 (correct)
    std::cout<<"Var2: "<<var2<<std::endl; //contains 2 (correct)
    std::cout<<"Var3: "<<var3<<std::endl; //contains 3 (correct)
    std::cout<<"Var4: "<<var4<<std::endl; //contains some bullsh@

    Some values are allways missing. Mostly it is exactly the last value!
    Do I have to add something?

    A working but slow solution is:
    @
    QByteArray b;
    QDataStream out(&b, QIODevice::ReadWrite);

    //serialize some data to b
    out<<(int)2;
    out<<(int)3;
    out<<(int)4;

    //message will contain some aditional info + the serialized data
    QByteArray message;
    QDataStream msgOut(&message, QIODevice::WriteOnly);

    msgOut<<(int)1;
    msgOut<<b; //here is the difference! Writing b insteat of out

    //now try to deserialize from message
    QDataStream msgIn(&message, QIODevice::ReadOnly);

    int var1;
    int var2;
    int var3;
    int var4;

    b.clear();

    msgIn>>var1;
    msgIn>>b; //i have to deserialize b first! That is a circumstance!

    QDataStream in(&b, QIODevice::ReadOnly);
    in>>var2;
    in>>var3;
    in>>var4;

    std::cout<<"Var1: "<<var1<<std::endl; //contains 1 (correct)
    std::cout<<"Var2: "<<var2<<std::endl; //contains 2 (correct)
    std::cout<<"Var3: "<<var3<<std::endl; //contains 3 (correct)
    std::cout<<"Var4: "<<var4<<std::endl; //contains 4 (correct)
    @

    It is working fine, but i think it costs performance! Because it first writes all bytes back to b!!!

    I hope everyone got the problem!?

    Thanks in advance



  • Doing seek(0) you explicitely tell you want to write at the beginning of the underlying QIODevice, aka the very beginning of the QByteArray. You have a construction problem. If I understand correctly you are trying to add some sort of header in front of your serialized data. If you want to do that, and that the header contents depends on the serialized data, I see no other solution but to serialize the data in memory using a QByteArray, computing your header, writing it to the output stream and then writing the QByteArray to the stream.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.