PrevNextUpHome SophiaFramework UNIVERSE 5.3

17.3. Binary Stream

The binary stream is for reading and writing a binary sequence(data of the arbitrary type). There are 2 kinds of the binary stream corresponding to the read / write type as in the table below:

Table 17.5. Binary Stream

Data Type For Reading For Writing
Arbitrary Type SFXBinaryStreamReader SFXBinaryStreamWriter

17.3.1. Reading and Writing the Binary Sequence Using the Binary Stream

17.3.1.1. Reading the Binary Sequence from the Binary Stream

Example 17.10. The binary sequence in hexadecimal to be read

12 34 56 78 11 22 33 44 55 // HEX format

Example 17.11. Reading the Binary Sequence from the Binary Stream

// *** in the code below, the error handling is omitted.
SFXFile file;                  // file
SFXBinaryStreamReader reader;  // binary stream for reading from the file

// open the file in the read only mode
file.OpenReadOnly(SFXPath("/dir/data.txt"));

// get the binary stream for reading from the file
// * the stream buffer is variable since the size argument is not specified
file.GetStreamReader(&reader);

// read data from the file into the stream buffer
// * the stream buffer will be expanded automatically depending on the size of data to be read into
//    the read operation will finish by calling the Fetch function once
reader.Fetch();

// interpret the binary sequence as Little-Endian
reader >> little;

UInt32 v;

// read data from the stream buffer into the various variables
reader >> v; // interpret 4 byte from head as UInt32
             // since it is Little-Endian, 0x78563412 is assigned to v

// the following binary sequence are interpreted as Big-Endian
reader >> big;

reader.ReadUInt32(&v); // interpret next 4 byte as UInt32
                       // since it is Big-Endian, v = 0x11223344
                       // same as reader >> v

UInt08 c;

reader >> c; // interpret 1 bytes from head as UInt08: c = 0x55

reader.ReadUInt08(&c); // c is not changed because it has come to end despite that it is tried to get 1 byte from head
                       // this function returns error

// release the binary stream for reading from the file
reader.Release();

// close the file
file.Close();
[Note] Note
The Read?????? function for the input stream differs from the extractor (>> operator) in that the former returns the error value.

17.3.1.2. Writing the Binary Sequence into the Binary Stream

Example 17.12. Writing the Binary Sequence into the Binary Stream

// *** in the code below, the error handling is omitted.
SFXFile file;                  // file
SFXBinaryStreamWriter writer;  // binary stream for writing into the file

// open the file in the read-write mode
file.OpenReadWrite(SFXPath("/dir/data.txt"));

// get the binary stream for writing into the file
// * the stream buffer is variable since the size argument is not specified
file.GetStreamWriter(&writer);

// interpret binary sequence as Little-Endian.
writer << little;

// write the various data into the stream buffer
// * the stream buffer will be expanded automatically depending on the size of data to be written
writer.WriteUInt08(0x22);  // write as UInt08
writer.WriteUInt16(0x22);  // write as UInt16
writer.WriteUInt32(0x22);  // write as UInt32

// writer << 0x22; causes error since it is ambigous
// writer << static_cast<UInt08>(0x22); is acceptable

// the following binary sequence is interpreted as Big-Endian
writer << big;

// write data into the stream buffer
// * the stream buffer will be expanded automatically depending on the size of data to be written
writer.WriteUInt32(0x22); // write as UInt32

// write data from the stream buffer into the file
// * the write operation will finish by calling the Flush function once
writer.Flush();

// release the binary stream for writing into the file
writer.Release();

// close the file
file.Close();

Example 17.13. The written the binary sequence in hexadecimal

22 22 00 22 00 00 00 00 00 00 22 // HEX format
[Note] Note
The Write?????? function for the output stream differs from the inserter (<< operator) in that the former returns the error value.

17.3.2. Reading and Writing the String Class Using the Binary Stream

In the binary stream, by specifying the SFXAnsiString / SFXWideString instance in the argument of the ReadSFXAnsiString / ReadSFXWideString function or the WriteSFXAnsiString / WriteSFXWideString function or in the operand of the inserter(<< operator) or extractor(>> operator), data of the string class can be read from or written into the storage.

17.3.2.1. Reading from the Binary Stream into the String Class

In case of reading data of the string class from the binary stream, the data until the next '\0' will be read into the variable of the SFXAnsiString / SFXWideString type.

In this case, there is no need to set the size of the variable in advance. The memory area of the variable will be allocated automatically depending on the length of the strings to be read.

[Note] In Case of Reading into the Buffer Class

In case of reading data into the variable of the SFXBuffer class, it is necessary to set the size of the variable in advance. The memory area of the variable will not be allocated automatically.

[Caution] In Case the Next '\0' is not Found until the Tail

If the next '\0' is not found until the tail, an error will occur and nothing will be stored into the variable(the content of the variable will be the same as before reading).

At this time, the ReadSFXAnsiString / ReadSFXWideString function of the binary stream will return SFERR_INVALID_STATE, but the extractor(>> operator) will return no error.

[Note] In Case of Reading from the String Stream

In case of reading from the string stream, data until the tail will be read as a string even if the tail is not '\0'.

[Tip] '\0' Separator
The '\0', separator to separate the strings in the stream is 1 byte in case of the AChar string(SFXAnsiString), or 2 bytes in case of the WChar string(SFXWideString).

Example 17.14. The Character string in hexadecimal to be read

61 62 63 64 00 65 66 67 68 00 69 6A 6B 6C

Example 17.15. Reading from the Binary Stream into the String Class

// *** in the code below, the error handling is omitted.
SFXFile file;                  // file
SFXBinaryStreamReader reader;  // binary stream for reading from the file
SFXAnsiString string;          // variable to read into

// open the file in the read only mode
file.OpenReadOnly(SFXPath("/dir/data.txt"));

// get the binary stream for reading from the file
// * the stream buffer is variable since the size argument is not specified
file.GetStreamReader(&reader);

// read data from the file into the stream buffer
// * the stream buffer will be expanded automatically depending on the size of data to be read into
//    the read operation will finish by calling the Fetch function once
reader.Fetch();

reader >> string; // get data string until '\0' is met           string = "abcd"
reader >> string; // get string up to next '\0'                  string = "efgh"

// at this point, no '\0' remains until the tail
reader >> string; // error occurs since there is no '\0' at end, string is not changed(i.e., string = "efgh")
                    // in case of the Read function, SFERR_INVALID_STATE will be returned

// release the binary stream for reading from the file
reader.Release();

// close the file
file.Close();

Read data up to the separator of '\0' character and interpret it as the SFXAnsiString string. If the '\0' character is not found, an error will occur and the string will not be stored.

17.3.2.2. Writing from the String Class into the Binary Stream

In the binary stream, if a SFXAnsiString / SFXWideString string is written into the stream by using the WriteSFXAnsiString / WriteSFXWideString function or the inserter(<< operator), '\0' will be automatically appended at the tail of the string.

[Caution] In Case of the String Stream

In case of writing into the string stream, '\0' will not be appended at the tail of the string.

[Tip] '\0' Separator
The '\0', separator to separate the strings in the stream is 1 byte in case of the AChar string(SFXAnsiString), or 2 bytes in case of the WChar string(SFXWideString).

Example 17.16. Writinging from the String Class into the Binary Stream

// *** in the code below, the error handling is omitted.
SFXFile file;                  // file
SFXBinaryStreamWriter writer;  // binary stream for writing into the file
SFXAnsiString string("abcd");  // string to write

// open the file in the read-write mode
file.OpenReadWrite(SFXPath("/dir/data.txt"));

// get the binary stream for writing into the file
// * the stream buffer is variable since the size argument is not specified
file.GetStreamWriter(&writer);

// write data from the string variable into the stream buffer
// * the stream buffer will be expanded automatically depending on the size of data to be written
writer << string; // write "abcd" ended with '\0'
writer << string; // write "abcd" ended with '\0'

// write data from the stream buffer into the file
// * the write operation will finish by calling the Flush function once
writer.Flush();

// release the binary stream for writing into the file
writer.Release();

// close the file
file.Close();

Example 17.17. The Character string in hexadecimal to be written

61 62 63 64 00 61 62 63 64 00

17.3.3. Reading and Writing the Buffer Class Using the Binary Stream

By specifying the SFXBuffer instance in the argument of the Read or Write function or in the operand of the inserter(<< operator) or extractor(>> operator), data of the buffer class can be read from or written into the storage.

[Caution] Caution
In the binary stream, the SFXBuffer instance can be specified as the operand of the inserter(<< operator) or extractor(>> operator). However, in case of the string stream, it cannot be specified.

17.3.3.1. Reading from the Binary Stream into the Buffer Class

In case of reading data into the variable of the SFXBuffer type, it is necessary to set the size of the variable in advance. The memory region of the variable will not be allocated automatically.

[Note] In case of reading the string class

In case of reading into the variable of the SFXAnsiString / SFXWideString type, there is no need to set the size of the variable in advance.

The memory region of the variable will be allocated automatically depending on the length of the string to be read.

[Caution] Caution
If only data less than the specified size can be read, an error will occur.

Example 17.18. The binary sequence in hexadecimal to be read

11 22 33 44 55 11 22 33 44 55 22

Example 17.19. Reading from the Binary Stream into the Buffer Class

// *** in the code below, the error handling is omitted.
SFXFile file;                  // file
SFXBinaryStreamReader reader;  // binary stream for reading from the file
SFXBuffer buffer;  // buffer to be read
buffer.SetSize(4);  // it is necessary to set buffer size in advance.

// open the file in the read only mode
file.OpenReadOnly(SFXPath("/dir/data.txt"));

// get the binary stream for reading from the file
// * the stream buffer is variable since the size argument is not specified
file.GetStreamReader(&reader);

// read data from the file into the stream buffer
// * the stream buffer will be expanded automatically depending on the size of data to be read into
//    the read operation will finish by calling the Fetch function once
reader.Fetch();

// read data from the stream buffer into the buffer variable
reader >> buffer;   // get first 4 byte: 11 22 33 44 (hexadecimal number), store in buffer
reader >> buffer;   // get next 4 byte: 55 11 22 33 (hexadecimal number), store in buffer

// here, data of 3 bytes remain in the stream buffer
// since the size of buffer 4 byte, data to be read is bigger than data which remain in the stream buffer.
reader >> buffer;  // after this statement, even if the stream try to be read, 
                   // the content of buffer will remain to be "55 11 22 33" (HEX format).
                   // In case of calling the Read function, the SFERR_FAILED error will be returned.

// release the binary stream for reading from the file
reader.Release();

// close the file
file.Close();

It is neccesary to specify the buffer size. The buffer cannot be automatically expanded.

Moreover, if the specified size is bigger than the remaining data to be read, an error will occur.

17.3.3.2. Writing from the Buffer Class into the Binary Stream

Example 17.20. Writing from the Buffer Class into the Binary Stream

// *** in the code below, the error handling is omitted.
SFXFile file;                  // file
SFXBinaryStreamWriter writer;  // binary stream for writing into the file
SFXBuffer buffer;              // buffer
AChar data[] = {0x11, 0x22, 0x33, 0x44, 0x55};  // data to set to the buffer

buffer.Set(data, 5);  // set the buffer to 5 bytes of data

// open the file in the read-write mode
file.OpenReadWrite(SFXPath("/dir/data.txt"));

// get the string stream for writing into the file
// * the stream buffer is variable since the size argument is not specified
file.GetStreamWriter(&writer);

writer << buffer; // write 5 bytes of data
writer << buffer; // write 5 bytes of data

// write data from the stream buffer into the file
// * the write operation will finish by calling the Flush function once
writer.Flush();

// release the binary stream for writing into the file
writer.Release();

// close the file
file.Close();

Example 17.21. The binary sequence in hexadecimal to be written

11 22 33 44 55 11 22 33 44 55  // HEX format