Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COMMON: Add WriteStream::writeStream() #1998

Merged
merged 1 commit into from Feb 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions common/stream.cpp
Expand Up @@ -28,6 +28,19 @@

namespace Common {

uint32 WriteStream::writeStream(ReadStream *stream, uint32 dataSize) {
void *buf = malloc(dataSize);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typically data copying routines use some kind of buffer of a fixed size as temporary storage so that they can work on arbitrarily sized inputs. See what boost does for example:
https://github.com/boostorg/iostreams/blob/develop/include/boost/iostreams/copy.hpp#L123

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is indeed nice to have, but I believe that it's outside of the scope of this pull request. The code can be adapted later on in-tree

dataSize = stream->read(buf, dataSize);
assert(dataSize > 0);
dataSize = write(buf, dataSize);
free(buf);
return dataSize;
}

uint32 WriteStream::writeStream(SeekableReadStream *stream) {
return writeStream(stream, stream->size());
}

void WriteStream::writeString(const String &str) {
write(str.c_str(), str.size());
}
Expand Down
8 changes: 8 additions & 0 deletions common/stream.h
Expand Up @@ -29,6 +29,7 @@

namespace Common {

class ReadStream;
class SeekableReadStream;

/**
Expand Down Expand Up @@ -228,6 +229,13 @@ class WriteStream : virtual public Stream {
writeUint64BE(n);
}

/**
* Write data from another stream to this one.
*/
uint32 writeStream(ReadStream *stream, uint32 dataSize);

uint32 writeStream(SeekableReadStream *stream);

/**
* Write the given string to the stream.
* This writes str.size() characters, but no terminating zero byte.
Expand Down