| 1 |
// OutByte.cpp |
| 2 |
|
| 3 |
#include "StdAfx.h" |
| 4 |
|
| 5 |
#include "OutBuffer.h" |
| 6 |
|
| 7 |
#include "../../Common/Alloc.h" |
| 8 |
|
| 9 |
bool COutBuffer::Create(UInt32 bufferSize) |
| 10 |
{ |
| 11 |
const UInt32 kMinBlockSize = 1; |
| 12 |
if (bufferSize < kMinBlockSize) |
| 13 |
bufferSize = kMinBlockSize; |
| 14 |
if (_buffer != 0 && _bufferSize == bufferSize) |
| 15 |
return true; |
| 16 |
Free(); |
| 17 |
_bufferSize = bufferSize; |
| 18 |
_buffer = (Byte *)::BigAlloc(bufferSize); |
| 19 |
return (_buffer != 0); |
| 20 |
} |
| 21 |
|
| 22 |
void COutBuffer::Free() |
| 23 |
{ |
| 24 |
BigFree(_buffer); |
| 25 |
_buffer = 0; |
| 26 |
} |
| 27 |
|
| 28 |
void COutBuffer::SetStream(ISequentialOutStream *stream) |
| 29 |
{ |
| 30 |
_stream = stream; |
| 31 |
} |
| 32 |
|
| 33 |
void COutBuffer::Init() |
| 34 |
{ |
| 35 |
_processedSize = 0; |
| 36 |
_pos = 0; |
| 37 |
#ifdef _NO_EXCEPTIONS |
| 38 |
ErrorCode = S_OK; |
| 39 |
#endif |
| 40 |
} |
| 41 |
|
| 42 |
HRESULT COutBuffer::Flush() |
| 43 |
{ |
| 44 |
if (_pos == 0) |
| 45 |
return S_OK; |
| 46 |
UInt32 processedSize; |
| 47 |
HRESULT result = _stream->Write(_buffer, _pos, &processedSize); |
| 48 |
if (result != S_OK) |
| 49 |
return result; |
| 50 |
if (_pos != processedSize) |
| 51 |
return E_FAIL; |
| 52 |
_processedSize += processedSize; |
| 53 |
_pos = 0; |
| 54 |
return S_OK; |
| 55 |
} |
| 56 |
|
| 57 |
void COutBuffer::WriteBlock() |
| 58 |
{ |
| 59 |
#ifdef _NO_EXCEPTIONS |
| 60 |
if (ErrorCode != S_OK) |
| 61 |
return; |
| 62 |
#endif |
| 63 |
HRESULT result = Flush(); |
| 64 |
#ifdef _NO_EXCEPTIONS |
| 65 |
ErrorCode = result; |
| 66 |
#else |
| 67 |
if (result != S_OK) |
| 68 |
throw COutBufferException(result); |
| 69 |
#endif |
| 70 |
} |