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