Problem Using FileOutputStream write(const void*, size_t) function


#1

I’m having a problem using FileOutputStream write(const void*, size_t) function when the first data byte is either 0xE0, or 0xF0. The interesting part is that it depends on the number of bytes to be written. For example, if the second argument (number of bytes to write) is 0x800A, the file is written correctly even if the first byte is 0xF0. However, if the second argument is 0x159A (smaller file size), the file is written with what looks like random data and the file size is 0x1757 instead of 0x159A. In addition, if the first byte is 0xF0 and the file size is one byte less, 0x1599, the file is written correctly with the first byte of 0xF0 and a file size of 0x1599.

Can anybody make sense of this?


#2

What comes to mind is the “Byte Order Mark” for unicode, but that’s actually 0xFEFF: https://en.wikipedia.org/wiki/Byte_order_mark

This has an influence, how your file is parsed in terms of unicode. But the numbers don’t add up for that, and also it would certainly not turn the content into random.

Can you elaborate a bit, what kind of data you are writing and maybe post the code snippet you are using to write?

Also be aware, that data might be still in a buffer, until you call either flush on the stream or the FileOutputStream goes out of scope.


#3

Here’s some code details to help define the problem:

     ptrMidiOutStream = chosenFile.createOutputStream() ;
     if (ptrMidiOutStream != NULL)
     {
        if (ptrMidiOutStream->openedOk())
        {
           ptrMidiOutStream->write(SysExData, ArrayLength) ;
           ptrMidiOutStream->flush() ;
           Success = true ;
        }
        else
        {
           Success = false ;
        }

where the variables are defined as
int ArrayLength ;
ScopedPointer <FileOutputStream ptrMidiOutStream ;
UINT8 SysExData[0x800A] ;

and SysExData[0x800A] is filled with data suitable for a MIDI System Exclusive Message with a start byte (0xF0), end byte (0xF7), an 8 byte header, and data bytes with the most significant nibble set to 0 as follows:
0xF0, 0x00, 0x00, 0x15, 0x64, 0x04, 0x00, 0x40, 0x00, (data bytes with msn = 0), 0xF7

Also, ArrayLength is set to the number of bytes to write, including the 0xF0 and 0xF7.