9 #include "../../../C/Alloc.h"
12 bool COutBuffer::Create(UInt32 bufferSize)
14 const UInt32 kMinBlockSize = 1;
15 if (bufferSize < kMinBlockSize)
16 bufferSize = kMinBlockSize;
17 if (_buffer != 0 && _bufferSize == bufferSize)
20 _bufferSize = bufferSize;
21 _buffer = (Byte *)::MidAlloc(bufferSize);
22 return (_buffer != 0);
25 void COutBuffer::Free()
31 void COutBuffer::SetStream(ISequentialOutStream *stream)
36 void COutBuffer::Init()
39 _limitPos = _bufferSize;
48 UInt64 COutBuffer::GetProcessedSize() const
50 UInt64 res = _processedSize + _pos - _streamPos;
51 if (_streamPos > _pos)
57 HRESULT COutBuffer::FlushPart()
59 // _streamPos < _bufferSize
60 UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos);
61 HRESULT result = S_OK;
67 memmove(_buffer2, _buffer + _streamPos, size);
73 && (ErrorCode == S_OK)
77 UInt32 processedSize = 0;
78 result = _stream->Write(_buffer + _streamPos, size, &processedSize);
82 if (_streamPos == _bufferSize)
84 if (_pos == _bufferSize)
89 _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize;
90 _processedSize += size;
94 HRESULT COutBuffer::Flush()
97 if (ErrorCode != S_OK)
101 while(_streamPos != _pos)
103 HRESULT result = FlushPart();
110 void COutBuffer::FlushWithCheck()
112 HRESULT result = Flush();
113 #ifdef _NO_EXCEPTIONS
117 throw COutBufferException(result);