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
10struct CReferenceBuf:
11  public IUnknown,
12  public CMyUnknownImp
13{
14  CByteBuffer Buf;
15  MY_UNKNOWN_IMP
16};
17
18class CBufInStream:
19  public IInStream,
20  public CMyUnknownImp
21{
22  const Byte *_data;
23  UInt64 _pos;
24  size_t _size;
25  CMyComPtr<IUnknown> _ref;
26public:
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  }
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
41class CByteDynBuffer
42{
43  size_t _capacity;
44  Byte *_buf;
45public:
46  CByteDynBuffer(): _capacity(0), _buf(0) {};
47  // there is no copy constructor. So don't copy this object.
48  ~CByteDynBuffer() { Free(); }
49  void Free();
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
56class CDynBufSeqOutStream:
57  public ISequentialOutStream,
58  public CMyUnknownImp
59{
60  CByteDynBuffer _buffer;
61  size_t _size;
62public:
63  CDynBufSeqOutStream(): _size(0) {}
64  void Init() { _size = 0;  }
65  size_t GetSize() const { return _size; }
66  const Byte *GetBuffer() const { return _buffer; }
67  void CopyToBuffer(CByteBuffer &dest) const;
68  Byte *GetBufPtrForWriting(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
75class CBufPtrSeqOutStream:
76  public ISequentialOutStream,
77  public CMyUnknownImp
78{
79  Byte *_buffer;
80  size_t _size;
81  size_t _pos;
82public:
83  void Init(Byte *buffer, size_t size)
84  {
85    _buffer = buffer;
86    _pos = 0;
87    _size = size;
88  }
89  size_t GetPos() const { return _pos; }
90
91  MY_UNKNOWN_IMP
92  STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
93};
94
95class CSequentialOutStreamSizeCount:
96  public ISequentialOutStream,
97  public CMyUnknownImp
98{
99  CMyComPtr<ISequentialOutStream> _stream;
100  UInt64 _size;
101public:
102  void SetStream(ISequentialOutStream *stream) { _stream = stream; }
103  void Init() { _size = 0; }
104  UInt64 GetSize() const { return _size; }
105
106  MY_UNKNOWN_IMP
107  STDMETHOD(Write)(const void *data, UInt32 size, UInt32 *processedSize);
108};
109
110class 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;
121protected:
122  virtual HRESULT ReadBlock(UInt64 blockIndex, Byte *dest, size_t blockSize) = 0;
123public:
124  CCachedInStream(): _tags(0), _data(0) {}
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