1 // OutBuffer.h 2 3 #ifndef ZIP7_INC_OUT_BUFFER_H 4 #define ZIP7_INC_OUT_BUFFER_H 5 6 #include "../IStream.h" 7 #include "../../Common/MyCom.h" 8 #include "../../Common/MyException.h" 9 10 #ifndef Z7_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 Z7_NO_EXCEPTIONS 33 HRESULT ErrorCode; 34 #endif 35 COutBuffer()36 COutBuffer(): _buf(NULL), _pos(0), _stream(NULL), _buf2(NULL) {} ~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 GetOutBuffer(size_t & avail)63 Byte *GetOutBuffer(size_t &avail) 64 { 65 const UInt32 pos = _pos; 66 avail = (size_t)(_limitPos - pos); 67 return _buf + pos; 68 } 69 SkipWrittenBytes(size_t num)70 void SkipWrittenBytes(size_t num) 71 { 72 const UInt32 pos = _pos; 73 const UInt32 rem = _limitPos - pos; 74 if (rem > num) 75 { 76 _pos = pos + (UInt32)num; 77 return; 78 } 79 // (rem <= num) 80 // the caller must not call it with (rem < num) 81 // so (rem == num) 82 _pos = _limitPos; 83 FlushWithCheck(); 84 } 85 /* 86 void WriteBytesBig(const void *data, size_t size) 87 { 88 while (size) 89 { 90 UInt32 pos = _pos; 91 UInt32 rem = _limitPos - pos; 92 if (rem > size) 93 { 94 _pos = pos + size; 95 memcpy(_buf + pos, data, size); 96 return; 97 } 98 memcpy(_buf + pos, data, rem); 99 _pos = pos + rem; 100 FlushWithCheck(); 101 } 102 } 103 */ 104 105 UInt64 GetProcessedSize() const throw(); 106 }; 107 108 #endif 109