importing from binaryStream into BLOB field

Franco Vaccari vaccari at units.it
Tue Jul 23 06:15:47 CDT 2013


I'm trying to store data read from a binaryStream (bis) into a BLOB field, but I'm unable to recover the correct values after that.

The relevant part of the binaryStream is a vector of doubles. When I read them as doubles from the binary stream, I see values (samples) are ok in the debugger:

        dim bis as BinaryStream, m as memoryBlock
        dim samples(-1) as double
        dim m as MemoryBlock
        bis=BinaryStream.Open(inFolder,true)
…
…
        bis.Position=endPosition+376 // x()
        m=new MemoryBlock(nDataBytes)
        m.LittleEndian=true
…
…
        // read as doubles (slow!)
        for j as integer=1 to nData
          m.DoubleValue((j-1)*8)=bis.ReadDouble
          samples.Append m.DoubleValue((j-1)*8)  // <-- values are ok, i.e. I'm pointing to the right bytes of binary stream ("A" statement, see below)
        next
        


What I want is to store the part of the binary stream that contains the samples into a BLOB field in my DB table, to recover the samples at need. This is what I'm doing:

// read values from a binaryInputStream (bis) as a memoryBlock and write them to the blob field

…
…
        // samples
        bis.Position=endPosition+376 // x()
        dim samples(-1) as double
        dim m as MemoryBlock
        m=new MemoryBlock(nDataBytes) <-- the number of bytes (number of data samples)*8
        m.littleEndian=true
        
        // read as string
        m=bis.Read(nDataBytes)

        // write to the BLOB
        tblData.BlobField("dataSamples").WriteRawData(m)
…
…


And then I read the data from the BLOB from the cursor:

…
…
// read values

    m=new MemoryBlock(nDataBytes)
    m.LittleEndian=true
    m=inCursor.BlobField("dataSamples").ReadRawData

    dim samples(-1) as double
    dim istop as integer= m.size/8
    for i as integer=1 to iStop
      samples.append m.DoubleValue(i-1)*8    <-- The size of the obtained vector is correct, but values are garbage. Pointer to bytes same as in "A" statement above.
    next
…
…

I suppose it might be a problem of encodings, as I believe the memoryBlock is read/written as a string, although I guessed that using WriteRawData and ReadRawData should free me from the encodings trouble. I tried playing with convertEncoding when reading/writing the memoryBlock, with no improvements. 

What am I doing wrong?

What is the difference between ReadData and ReadRawData, (and Write as well)? I can't find ReadRawData in the Wiki, but it autocompletes in Xojo for the BlobField…

Is there a better way to clone the sequence of bytes of the file I'm reading, and store it in the BLOB field?

Thanks

Franco


    


More information about the Valentina mailing list