1 // OutBuffer.h
2 
3 #ifndef __OUT_BUFFER_H
4 #define __OUT_BUFFER_H
5 
6 #include "../IStream.h"
7 #include "../../Common/MyCom.h"
8 #include "../../Common/MyException.h"
9 
10 #ifndef _NO_EXCEPTIONS
11 struct COutBufferException: public CSystemException
12 {
COutBufferExceptionCOutBufferException13   COutBufferException(HRESULT errorCode): CSystemException(errorCode) {}
14 };
15 #endif
16 
17 class COutBuffer
18 {
19 protected:
20   Byte *_buf;
21   UInt32 _pos;
22   UInt32 _limitPos;
23   UInt32 _streamPos;
24   UInt32 _bufSize;
25   ISequentialOutStream *_stream;
26   UInt64 _processedSize;
27   Byte  *_buf2;
28   bool _overDict;
29 
30   HRESULT FlushPart() throw();
31 public:
32   #ifdef _NO_EXCEPTIONS
33   HRESULT ErrorCode;
34   #endif
35 
COutBuffer()36   COutBuffer(): _buf(0), _pos(0), _stream(0), _buf2(0) {}
~COutBuffer()37   ~COutBuffer() { Free(); }
38 
39   bool Create(UInt32 bufSize) throw();
40   void Free() throw();
41 
SetMemStream(Byte * buf)42   void SetMemStream(Byte *buf) { _buf2 = buf; }
SetStream(ISequentialOutStream * stream)43   void SetStream(ISequentialOutStream *stream) { _stream = stream; }
44   void Init() throw();
45   HRESULT Flush() throw();
46   void FlushWithCheck();
47 
WriteByte(Byte b)48   void WriteByte(Byte b)
49   {
50     UInt32 pos = _pos;
51     _buf[pos] = b;
52     pos++;
53     _pos = pos;
54     if (pos == _limitPos)
55       FlushWithCheck();
56   }
WriteBytes(const void * data,size_t size)57   void WriteBytes(const void *data, size_t size)
58   {
59     for (size_t i = 0; i < size; i++)
60       WriteByte(((const Byte *)data)[i]);
61   }
62 
63   UInt64 GetProcessedSize() const throw();
64 };
65 
66 #endif
67