1370b324cSopenharmony_ci// OutBuffer.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_OUT_BUFFER_H
4370b324cSopenharmony_ci#define ZIP7_INC_OUT_BUFFER_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include "../IStream.h"
7370b324cSopenharmony_ci#include "../../Common/MyCom.h"
8370b324cSopenharmony_ci#include "../../Common/MyException.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#ifndef Z7_NO_EXCEPTIONS
11370b324cSopenharmony_cistruct COutBufferException: public CSystemException
12370b324cSopenharmony_ci{
13370b324cSopenharmony_ci  COutBufferException(HRESULT errorCode): CSystemException(errorCode) {}
14370b324cSopenharmony_ci};
15370b324cSopenharmony_ci#endif
16370b324cSopenharmony_ci
17370b324cSopenharmony_ciclass COutBuffer
18370b324cSopenharmony_ci{
19370b324cSopenharmony_ciprotected:
20370b324cSopenharmony_ci  Byte *_buf;
21370b324cSopenharmony_ci  UInt32 _pos;
22370b324cSopenharmony_ci  UInt32 _limitPos;
23370b324cSopenharmony_ci  UInt32 _streamPos;
24370b324cSopenharmony_ci  UInt32 _bufSize;
25370b324cSopenharmony_ci  ISequentialOutStream *_stream;
26370b324cSopenharmony_ci  UInt64 _processedSize;
27370b324cSopenharmony_ci  Byte  *_buf2;
28370b324cSopenharmony_ci  bool _overDict;
29370b324cSopenharmony_ci
30370b324cSopenharmony_ci  HRESULT FlushPart() throw();
31370b324cSopenharmony_cipublic:
32370b324cSopenharmony_ci  #ifdef Z7_NO_EXCEPTIONS
33370b324cSopenharmony_ci  HRESULT ErrorCode;
34370b324cSopenharmony_ci  #endif
35370b324cSopenharmony_ci
36370b324cSopenharmony_ci  COutBuffer(): _buf(NULL), _pos(0), _stream(NULL), _buf2(NULL) {}
37370b324cSopenharmony_ci  ~COutBuffer() { Free(); }
38370b324cSopenharmony_ci
39370b324cSopenharmony_ci  bool Create(UInt32 bufSize) throw();
40370b324cSopenharmony_ci  void Free() throw();
41370b324cSopenharmony_ci
42370b324cSopenharmony_ci  void SetMemStream(Byte *buf) { _buf2 = buf; }
43370b324cSopenharmony_ci  void SetStream(ISequentialOutStream *stream) { _stream = stream; }
44370b324cSopenharmony_ci  void Init() throw();
45370b324cSopenharmony_ci  HRESULT Flush() throw();
46370b324cSopenharmony_ci  void FlushWithCheck();
47370b324cSopenharmony_ci
48370b324cSopenharmony_ci  void WriteByte(Byte b)
49370b324cSopenharmony_ci  {
50370b324cSopenharmony_ci    UInt32 pos = _pos;
51370b324cSopenharmony_ci    _buf[pos] = b;
52370b324cSopenharmony_ci    pos++;
53370b324cSopenharmony_ci    _pos = pos;
54370b324cSopenharmony_ci    if (pos == _limitPos)
55370b324cSopenharmony_ci      FlushWithCheck();
56370b324cSopenharmony_ci  }
57370b324cSopenharmony_ci  void WriteBytes(const void *data, size_t size)
58370b324cSopenharmony_ci  {
59370b324cSopenharmony_ci    for (size_t i = 0; i < size; i++)
60370b324cSopenharmony_ci      WriteByte(((const Byte *)data)[i]);
61370b324cSopenharmony_ci  }
62370b324cSopenharmony_ci
63370b324cSopenharmony_ci  UInt64 GetProcessedSize() const throw();
64370b324cSopenharmony_ci};
65370b324cSopenharmony_ci
66370b324cSopenharmony_ci#endif
67