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