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