Discussion:
Boost iostreams for binary data
(too old to reply)
Robert Dailey
2017-01-31 20:25:05 UTC
Permalink
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>`.

I did not implement these in terms of IO streams, because those seemed
a bit overkill and make a lot of assumptions on the data being
text-only. Also I can't stream binary data into vectors, lists, and
other containers because the data simply can't be parsed. Each stream
out of a binary vector must be done so with a finite count of bytes.

Would it make sense to implement these objects in terms of the
IOstream facilities provided by boost? I know these were designed to
make defining stream classes easier, but honestly I don't have a lot
of experience with them.

Any feedback on this? Thanks in advance. I can provide some example
code if needed.
Robert Dailey
2017-02-02 21:07:51 UTC
Permalink
Post by Robert Dailey
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>`.
I did not implement these in terms of IO streams, because those seemed
a bit overkill and make a lot of assumptions on the data being
text-only. Also I can't stream binary data into vectors, lists, and
other containers because the data simply can't be parsed. Each stream
out of a binary vector must be done so with a finite count of bytes.
Would it make sense to implement these objects in terms of the
IOstream facilities provided by boost? I know these were designed to
make defining stream classes easier, but honestly I don't have a lot
of experience with them.
Any feedback on this? Thanks in advance. I can provide some example
code if needed.
Could I get any advice on this? If I was too vague, please let me know.
Jeff Garland
2017-02-03 16:24:53 UTC
Permalink
I guess the request is somewhat confusing -- are you asking about
boost.iostreams, boost.serialization, something else? Note, if you want to
do binary i/o in C++ you can always use the streambuf interfaces directly
-- which would be similar to the vector approach if I'm understanding...

http://en.cppreference.com/w/cpp/io/basic_streambuf

Jeff
Post by Robert Dailey
Post by Robert Dailey
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>`.
I did not implement these in terms of IO streams, because those seemed
a bit overkill and make a lot of assumptions on the data being
text-only. Also I can't stream binary data into vectors, lists, and
other containers because the data simply can't be parsed. Each stream
out of a binary vector must be done so with a finite count of bytes.
Would it make sense to implement these objects in terms of the
IOstream facilities provided by boost? I know these were designed to
make defining stream classes easier, but honestly I don't have a lot
of experience with them.
Any feedback on this? Thanks in advance. I can provide some example
code if needed.
Could I get any advice on this? If I was too vague, please let me know.
_______________________________________________
Boost-users mailing list
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Bjorn Reese
2017-02-05 11:55:14 UTC
Permalink
Post by Robert Dailey
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?
Robert Dailey
2017-02-06 17:03:52 UTC
Permalink
Post by Bjorn Reese
Post by Robert Dailey
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.
Bjorn Reese
2017-02-06 18:06:44 UTC
Permalink
Post by Robert Dailey
using ByteVector = std::vector<std::uint8_t>;
I meant, can BinaryInputStream read data from an array, and can
BinaryOutputStream write data into an array?
Robert Dailey
2017-02-06 19:17:05 UTC
Permalink
Post by Bjorn Reese
Post by Robert Dailey
using ByteVector = std::vector<std::uint8_t>;
I meant, can BinaryInputStream read data from an array, and can
BinaryOutputStream write data into an array?
If by array you mean:

std::uint8_t bytes[100];

Not in my current implementation. I do not use arrays in my code, so I
haven't needed to support it. But my stream classes use iterators and
algorithms for writing/reading data, so shouldn't be hard to add.
What, if I may ask, are you trying to get at?

Loading...