COMMON: Rewrite writeStream to work with any ReadStream

This let it work with stream without a known size and avoids allocating
big buffers when file is big.
This commit is contained in:
Le Philousophe 2023-08-15 09:25:48 +02:00 committed by Eugene Sandulenko
parent 0db796bd98
commit ead4171bbd
2 changed files with 20 additions and 3 deletions

View File

@ -27,6 +27,10 @@
namespace Common { namespace Common {
enum {
kTempBufSize = 65536
};
uint32 WriteStream::writeStream(ReadStream *stream, uint32 dataSize) { uint32 WriteStream::writeStream(ReadStream *stream, uint32 dataSize) {
void *buf = malloc(dataSize); void *buf = malloc(dataSize);
dataSize = stream->read(buf, dataSize); dataSize = stream->read(buf, dataSize);
@ -36,8 +40,21 @@ uint32 WriteStream::writeStream(ReadStream *stream, uint32 dataSize) {
return dataSize; return dataSize;
} }
uint32 WriteStream::writeStream(SeekableReadStream *stream) { uint32 WriteStream::writeStream(ReadStream *stream) {
return writeStream(stream, stream->size()); uint32 ret = 0;
void *buf = malloc(kTempBufSize);
assert(buf);
uint32 readSize, writeSize;
do {
readSize = stream->read(buf, kTempBufSize);
writeSize = write(buf, readSize);
ret += writeSize;
} while(readSize == kTempBufSize && writeSize == readSize);
free(buf);
return ret;
} }
void WriteStream::writeString(const String &str) { void WriteStream::writeString(const String &str) {

View File

@ -288,7 +288,7 @@ public:
* *
* @return The number of bytes written into the stream. * @return The number of bytes written into the stream.
*/ */
uint32 writeStream(SeekableReadStream *stream); uint32 writeStream(ReadStream *stream);
/** /**
* Write the given string to the stream. * Write the given string to the stream.