7 #include "../../Common/Alloc.h"
9 bool COutBuffer::Create(UInt32 bufferSize)
11 const UInt32 kMinBlockSize = 1;
12 if (bufferSize < kMinBlockSize)
13 bufferSize = kMinBlockSize;
14 if (_buffer != 0 && _bufferSize == bufferSize)
17 _bufferSize = bufferSize;
18 _buffer = (Byte *)::MidAlloc(bufferSize);
19 return (_buffer != 0);
22 void COutBuffer::Free()
28 void COutBuffer::SetStream(ISequentialOutStream *stream)
33 void COutBuffer::Init()
36 _limitPos = _bufferSize;
45 UInt64 COutBuffer::GetProcessedSize() const
47 UInt64 res = _processedSize + _pos - _streamPos;
48 if (_streamPos > _pos)
54 HRESULT COutBuffer::FlushPart()
56 // _streamPos < _bufferSize
57 UInt32 size = (_streamPos >= _pos) ? (_bufferSize - _streamPos) : (_pos - _streamPos);
58 HRESULT result = S_OK;
64 memmove(_buffer2, _buffer + _streamPos, size);
70 && (ErrorCode == S_OK)
74 UInt32 processedSize = 0;
75 result = _stream->Write(_buffer + _streamPos, size, &processedSize);
79 if (_streamPos == _bufferSize)
81 if (_pos == _bufferSize)
86 _limitPos = (_streamPos > _pos) ? _streamPos : _bufferSize;
87 _processedSize += size;
91 HRESULT COutBuffer::Flush()
94 if (ErrorCode != S_OK)
98 while(_streamPos != _pos)
100 HRESULT result = FlushPart();
107 void COutBuffer::FlushWithCheck()
109 HRESULT result = FlushPart();
110 #ifdef _NO_EXCEPTIONS
114 throw COutBufferException(result);