COMMON: Add an optional argument to wrapCompressedReadStream, to simplify using streams that can't tell their size()

This commit is contained in:
Einar Johan Trøan Sømåen 2012-07-18 13:54:15 +02:00
parent b6000da875
commit b398dcabaf
2 changed files with 14 additions and 5 deletions

View File

@ -107,7 +107,7 @@ protected:
public:
GZipReadStream(SeekableReadStream *w) : _wrapped(w), _stream() {
GZipReadStream(SeekableReadStream *w, uint32 knownSize = 0) : _wrapped(w), _stream() {
assert(w != 0);
// Verify file header is correct
@ -122,7 +122,8 @@ public:
_origSize = w->readUint32LE();
} else {
// Original size not available in zlib format
_origSize = 0;
// use an otherwise known size if supplied.
_origSize = knownSize;
}
_pos = 0;
w->seek(0, SEEK_SET);
@ -336,7 +337,7 @@ public:
#endif // USE_ZLIB
SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped) {
SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped, uint32 knownSize) {
#if defined(USE_ZLIB)
if (toBeWrapped) {
uint16 header = toBeWrapped->readUint16BE();
@ -345,7 +346,7 @@ SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped) {
header % 31 == 0));
toBeWrapped->seek(-2, SEEK_CUR);
if (isCompressed)
return new GZipReadStream(toBeWrapped);
return new GZipReadStream(toBeWrapped, knownSize);
}
#endif
return toBeWrapped;

View File

@ -86,10 +86,18 @@ bool inflateZlibHeaderless(byte *dst, uint dstLen, const byte *src, uint srcLen,
* format. In the former case, the original stream is returned unmodified
* (and in particular, not wrapped).
*
* Certain GZip-formats don't supply an easily readable length, if you
* still need the length carried along with the stream, and you know
* the decompressed length at wrap-time, then it can be supplied as knownSize
* here. knownSize will be ignored if the GZip-stream DOES include a length.
*
* It is safe to call this with a NULL parameter (in this case, NULL is
* returned).
*
* @param toBeWrapped the stream to be wrapped (if it is in gzip-format)
* @param knownSize a supplied length of the compressed data (if not available directly)
*/
SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped);
SeekableReadStream *wrapCompressedReadStream(SeekableReadStream *toBeWrapped, uint32 knownSize = 0);
/**
* Take an arbitrary WriteStream and wrap it in a custom stream which provides