|
1 // InBuffer.h |
|
2 |
|
3 #ifndef __INBUFFER_H |
|
4 #define __INBUFFER_H |
|
5 |
|
6 #include "../IStream.h" |
|
7 #include "../../Common/MyCom.h" |
|
8 |
|
9 #ifndef _NO_EXCEPTIONS |
|
10 class CInBufferException |
|
11 { |
|
12 public: |
|
13 HRESULT ErrorCode; |
|
14 CInBufferException(HRESULT errorCode): ErrorCode(errorCode) {} |
|
15 }; |
|
16 #endif |
|
17 |
|
18 class CInBuffer |
|
19 { |
|
20 Byte *_buffer; |
|
21 Byte *_bufferLimit; |
|
22 Byte *_bufferBase; |
|
23 CMyComPtr<ISequentialInStream> _stream; |
|
24 UInt64 _processedSize; |
|
25 UInt32 _bufferSize; |
|
26 bool _wasFinished; |
|
27 |
|
28 bool ReadBlock(); |
|
29 Byte ReadBlock2(); |
|
30 |
|
31 public: |
|
32 #ifdef _NO_EXCEPTIONS |
|
33 HRESULT ErrorCode; |
|
34 #endif |
|
35 |
|
36 CInBuffer(); |
|
37 ~CInBuffer() { Free(); } |
|
38 |
|
39 bool Create(UInt32 bufferSize); |
|
40 void Free(); |
|
41 |
|
42 void SetStream(ISequentialInStream *stream); |
|
43 void Init(); |
|
44 void ReleaseStream() { _stream.Release(); } |
|
45 |
|
46 bool ReadByte(Byte &b) |
|
47 { |
|
48 if(_buffer >= _bufferLimit) |
|
49 if(!ReadBlock()) |
|
50 return false; |
|
51 b = *_buffer++; |
|
52 return true; |
|
53 } |
|
54 Byte ReadByte() |
|
55 { |
|
56 if(_buffer >= _bufferLimit) |
|
57 return ReadBlock2(); |
|
58 return *_buffer++; |
|
59 } |
|
60 void ReadBytes(void *data, UInt32 size, UInt32 &processedSize) |
|
61 { |
|
62 for(processedSize = 0; processedSize < size; processedSize++) |
|
63 if (!ReadByte(((Byte *)data)[processedSize])) |
|
64 return; |
|
65 } |
|
66 bool ReadBytes(void *data, UInt32 size) |
|
67 { |
|
68 UInt32 processedSize; |
|
69 ReadBytes(data, size, processedSize); |
|
70 return (processedSize == size); |
|
71 } |
|
72 UInt64 GetProcessedSize() const { return _processedSize + (_buffer - _bufferBase); } |
|
73 bool WasFinished() const { return _wasFinished; } |
|
74 }; |
|
75 |
|
76 #endif |