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
;
60 if (ErrorCode
!= S_OK
)
65 memmove(_buffer2
, _buffer
+ _streamPos
, size
);
71 && (ErrorCode
!= S_OK
)
75 UInt32 processedSize
= 0;
76 result
= _stream
->Write(_buffer
+ _streamPos
, size
, &processedSize
);
80 if (_streamPos
== _bufferSize
)
82 if (_pos
== _bufferSize
)
87 _limitPos
= (_streamPos
> _pos
) ? _streamPos
: _bufferSize
;
88 _processedSize
+= size
;
92 HRESULT
COutBuffer::Flush()
95 if (ErrorCode
!= S_OK
)
99 while(_streamPos
!= _pos
)
101 HRESULT result
= FlushPart();
108 void COutBuffer::FlushWithCheck()
110 HRESULT result
= FlushPart();
111 #ifdef _NO_EXCEPTIONS
115 throw COutBufferException(result
);