From 1e2988b7f75adc471e3dc39aa42eb99e33727023 Mon Sep 17 00:00:00 2001 From: Max Horn Date: Wed, 30 Jul 2008 07:39:41 +0000 Subject: Changed BufferedReadStream to not permanently decrease its buffer size at the end of a stream (this would fail when using BufferedSeekableReadStream and then seeking back from the end); this also fixes a bug which let you seek back beyond the start of a stream (not that we currently support that in other streams) svn-id: r33436 --- common/stream.cpp | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) (limited to 'common/stream.cpp') diff --git a/common/stream.cpp b/common/stream.cpp index d068e2fc1e..e06cc28415 100644 --- a/common/stream.cpp +++ b/common/stream.cpp @@ -244,14 +244,16 @@ void SeekableSubReadStream::seek(int32 offset, int whence) { BufferedReadStream::BufferedReadStream(ReadStream *parentStream, uint32 bufSize, bool disposeParentStream) : _parentStream(parentStream), - _bufSize(bufSize), - _disposeParentStream(disposeParentStream) { + _disposeParentStream(disposeParentStream), + _pos(0), + _bufSize(0), + _realBufSize(bufSize) { assert(parentStream); - _buf = new byte[_bufSize]; + _buf = new byte[bufSize]; assert(_buf); - _pos = _bufSize = bufSize; } + BufferedReadStream::~BufferedReadStream() { if (_disposeParentStream) delete _parentStream; @@ -281,18 +283,14 @@ uint32 BufferedReadStream::read(void *dataPtr, uint32 dataSize) { return alreadyRead + _parentStream->read(dataPtr, dataSize); // Refill the buffer. - uint32 bytesRead = _parentStream->read(_buf, _bufSize); - _pos = 0; - // If we didn't read as many bytes as requested, the reason // is EOF or an error. In that case we truncate the buffer // size, as well as the number of bytes we are going to // return to the caller. - if (_bufSize > bytesRead) { - _bufSize = bytesRead; - if (dataSize > bytesRead) - dataSize = bytesRead; - } + _bufSize = _parentStream->read(_buf, _realBufSize); + _pos = 0; + if (dataSize > _bufSize) + dataSize = _bufSize; } // Satisfy the request from the buffer -- cgit v1.2.3