michael@0: // OutByte.cpp michael@0: michael@0: #include "StdAfx.h" michael@0: michael@0: #include "OutBuffer.h" michael@0: michael@0: #include "../../Common/Alloc.h" michael@0: michael@0: bool COutBuffer::Create(UInt32 bufferSize) michael@0: { michael@0: const UInt32 kMinBlockSize = 1; michael@0: if (bufferSize < kMinBlockSize) michael@0: bufferSize = kMinBlockSize; michael@0: if (_buffer != 0 && _bufferSize == bufferSize) michael@0: return true; michael@0: Free(); michael@0: _bufferSize = bufferSize; michael@0: _buffer = (Byte *)::MidAlloc(bufferSize); michael@0: return (_buffer != 0); michael@0: } michael@0: michael@0: void COutBuffer::Free() michael@0: { michael@0: ::MidFree(_buffer); michael@0: _buffer = 0; michael@0: } michael@0: michael@0: void COutBuffer::SetStream(ISequentialOutStream *stream) michael@0: { michael@0: _stream = stream; michael@0: } michael@0: michael@0: void COutBuffer::Init() michael@0: { michael@0: _streamPos = 0; michael@0: _limitPos = _bufferSize; michael@0: _pos = 0; michael@0: _processedSize = 0; michael@0: _overDict = false; michael@0: #ifdef _NO_EXCEPTIONS michael@0: ErrorCode = S_OK; michael@0: #endif michael@0: } michael@0: michael@0: UInt64 COutBuffer::GetProcessedSize() const michael@0: { michael@0: UInt64 res = _processedSize + _pos - _streamPos; michael@0: if (_streamPos > _pos) michael@0: res += _bufferSize; michael@0: return res; michael@0: } michael@0: michael@0: michael@0: HRESULT COutBuffer::FlushPart() michael@0: { michael@0: // _streamPos < _bufferSize michael@0: UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos); michael@0: HRESULT result = S_OK; michael@0: #ifdef _NO_EXCEPTIONS michael@0: result = ErrorCode; michael@0: #endif michael@0: if (_buffer2 != 0) michael@0: { michael@0: memmove(_buffer2, _buffer + _streamPos, size); michael@0: _buffer2 += size; michael@0: } michael@0: michael@0: if (_stream != 0 michael@0: #ifdef _NO_EXCEPTIONS michael@0: && (ErrorCode == S_OK) michael@0: #endif michael@0: ) michael@0: { michael@0: UInt32 processedSize = 0; michael@0: result = _stream->Write(_buffer + _streamPos, size, &processedSize); michael@0: size = processedSize; michael@0: } michael@0: _streamPos += size; michael@0: if (_streamPos == _bufferSize) michael@0: _streamPos = 0; michael@0: if (_pos == _bufferSize) michael@0: { michael@0: _overDict = true; michael@0: _pos = 0; michael@0: } michael@0: _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize; michael@0: _processedSize += size; michael@0: return result; michael@0: } michael@0: michael@0: HRESULT COutBuffer::Flush() michael@0: { michael@0: #ifdef _NO_EXCEPTIONS michael@0: if (ErrorCode != S_OK) michael@0: return ErrorCode; michael@0: #endif michael@0: michael@0: while(_streamPos != _pos) michael@0: { michael@0: HRESULT result = FlushPart(); michael@0: if (result != S_OK) michael@0: return result; michael@0: } michael@0: return S_OK; michael@0: } michael@0: michael@0: void COutBuffer::FlushWithCheck() michael@0: { michael@0: HRESULT result = FlushPart(); michael@0: #ifdef _NO_EXCEPTIONS michael@0: ErrorCode = result; michael@0: #else michael@0: if (result != S_OK) michael@0: throw COutBufferException(result); michael@0: #endif michael@0: }