1 // StreamObjects.h 2 3 #ifndef ZIP7_INC_STREAM_OBJECTS_H 4 #define ZIP7_INC_STREAM_OBJECTS_H 5 6 #include "../../Common/MyBuffer.h" 7 #include "../../Common/MyCom.h" 8 #include "../../Common/MyVector.h" 9 10 #include "../IStream.h" 11 12 Z7_CLASS_IMP_IInStream( 13 CBufferInStream 14 ) 15 UInt64 _pos; 16 public: 17 CByteBuffer Buf; Init()18 void Init() { _pos = 0; } 19 }; 20 21 22 Z7_CLASS_IMP_COM_0( 23 CReferenceBuf 24 ) 25 public: 26 CByteBuffer Buf; 27 }; 28 29 30 Z7_CLASS_IMP_IInStream( 31 CBufInStream 32 ) 33 const Byte *_data; 34 UInt64 _pos; 35 size_t _size; 36 CMyComPtr<IUnknown> _ref; 37 public: 38 void Init(const Byte *data, size_t size, IUnknown *ref = NULL) 39 { 40 _data = data; 41 _size = size; 42 _pos = 0; 43 _ref = ref; 44 } 45 void Init(CReferenceBuf *ref) { Init(ref->Buf, ref->Buf.Size(), ref); } 46 47 // Seek() is allowed here. So reading order could be changed 48 bool WasFinished() const { return _pos == _size; } 49 }; 50 51 52 void Create_BufInStream_WithReference(const void *data, size_t size, IUnknown *ref, ISequentialInStream **stream); 53 void Create_BufInStream_WithNewBuffer(const void *data, size_t size, ISequentialInStream **stream); 54 inline void Create_BufInStream_WithNewBuffer(const CByteBuffer &buf, ISequentialInStream **stream) 55 { Create_BufInStream_WithNewBuffer(buf, buf.Size(), stream); } 56 57 58 class CByteDynBuffer Z7_final 59 { 60 size_t _capacity; 61 Byte *_buf; 62 Z7_CLASS_NO_COPY(CByteDynBuffer) 63 public: 64 CByteDynBuffer(): _capacity(0), _buf(NULL) {} 65 // there is no copy constructor. So don't copy this object. 66 ~CByteDynBuffer() { Free(); } 67 void Free() throw(); 68 size_t GetCapacity() const { return _capacity; } 69 operator Byte*() const { return _buf; } 70 operator const Byte*() const { return _buf; } 71 bool EnsureCapacity(size_t capacity) throw(); 72 }; 73 74 75 Z7_CLASS_IMP_COM_1( 76 CDynBufSeqOutStream 77 , ISequentialOutStream 78 ) 79 CByteDynBuffer _buffer; 80 size_t _size; 81 public: 82 CDynBufSeqOutStream(): _size(0) {} 83 void Init() { _size = 0; } 84 size_t GetSize() const { return _size; } 85 const Byte *GetBuffer() const { return _buffer; } 86 void CopyToBuffer(CByteBuffer &dest) const; 87 Byte *GetBufPtrForWriting(size_t addSize); 88 void UpdateSize(size_t addSize) { _size += addSize; } 89 }; 90 91 92 Z7_CLASS_IMP_COM_1( 93 CBufPtrSeqOutStream 94 , ISequentialOutStream 95 ) 96 Byte *_buffer; 97 size_t _size; 98 size_t _pos; 99 public: 100 void Init(Byte *buffer, size_t size) 101 { 102 _buffer = buffer; 103 _pos = 0; 104 _size = size; 105 } 106 size_t GetPos() const { return _pos; } 107 }; 108 109 110 Z7_CLASS_IMP_COM_1( 111 CSequentialOutStreamSizeCount 112 , ISequentialOutStream 113 ) 114 CMyComPtr<ISequentialOutStream> _stream; 115 UInt64 _size; 116 public: 117 void SetStream(ISequentialOutStream *stream) { _stream = stream; } 118 void Init() { _size = 0; } 119 UInt64 GetSize() const { return _size; } 120 }; 121 122 123 class CCachedInStream: 124 public IInStream, 125 public CMyUnknownImp 126 { 127 Z7_IFACES_IMP_UNK_2(ISequentialInStream, IInStream) 128 129 UInt64 *_tags; 130 Byte *_data; 131 size_t _dataSize; 132 unsigned _blockSizeLog; 133 unsigned _numBlocksLog; 134 UInt64 _size; 135 UInt64 _pos; 136 protected: 137 virtual HRESULT ReadBlock(UInt64 blockIndex, Byte *dest, size_t blockSize) = 0; 138 public: 139 CCachedInStream(): _tags(NULL), _data(NULL) {} 140 virtual ~CCachedInStream() { Free(); } // the destructor must be virtual (Release() calls it) !!! 141 void Free() throw(); 142 bool Alloc(unsigned blockSizeLog, unsigned numBlocksLog) throw(); 143 void Init(UInt64 size) throw(); 144 }; 145 146 #endif 147