Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost iostreams for binary data
From: Robert Dailey (rcdailey.lists_at_[hidden])
Date: 2017-02-06 17:03:52


On Sun, Feb 5, 2017 at 5:55 AM, Bjorn Reese <breese_at_[hidden]> wrote:
> On 01/31/2017 09:25 PM, Robert Dailey wrote:
>>
>> I have my own BinaryInputStream and BinaryOutputStream classes that
>> simply take a reference to some container that meets the requirements
>> of random access and contiguous internal memory. It doesn't own this
>> container, but rather serves as an adapter to the container to allow
>> stream operators to be used for streaming out data. For example, you
>> can stream a `std::uint32_t` to the binary stream, and it will insert
>> 4 elements into an internal byte vector which is just
>> `std::vector<std::uint8_t>`.
>
>
> Do they work with arrays?

Yes, albeit a bit awkwardly. The input stream is as follows:

using ByteVector = std::vector<std::uint8_t>;

ByteVector data{/* some real data */};
BinaryInputStream stream{data};

ByteVector read_bytes = stream.ReadBytes(100); // read 100 bytes into
a ByteVector

// Output stream:

ByteVector data;
BinaryOutputStream stream{data};

ByteVector stuff{0x10, 0x20, 0x30}; // bytes to write out
stream << stuff;

I can't use stream operators for input stream since there is no
delimiter in the data to indicate how many bytes to read.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net