| 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 |
UInt64 _processedSize; |
| 21 |
Byte *_bufferBase; |
| 22 |
UInt32 _bufferSize; |
| 23 |
Byte *_buffer; |
| 24 |
Byte *_bufferLimit; |
| 25 |
CMyComPtr<ISequentialInStream> _stream; |
| 26 |
bool _wasFinished; |
| 27 |
|
| 28 |
bool ReadBlock(); |
| 29 |
|
| 30 |
public: |
| 31 |
#ifdef _NO_EXCEPTIONS |
| 32 |
HRESULT ErrorCode; |
| 33 |
#endif |
| 34 |
|
| 35 |
CInBuffer(); |
| 36 |
~CInBuffer() { Free(); } |
| 37 |
|
| 38 |
bool Create(UInt32 bufferSize); |
| 39 |
void Free(); |
| 40 |
|
| 41 |
void SetStream(ISequentialInStream *stream); |
| 42 |
void Init(); |
| 43 |
void ReleaseStream() { _stream.Release(); } |
| 44 |
|
| 45 |
bool ReadByte(Byte &b) |
| 46 |
{ |
| 47 |
if(_buffer >= _bufferLimit) |
| 48 |
if(!ReadBlock()) |
| 49 |
return false; |
| 50 |
b = *_buffer++; |
| 51 |
return true; |
| 52 |
} |
| 53 |
Byte ReadByte() |
| 54 |
{ |
| 55 |
if(_buffer >= _bufferLimit) |
| 56 |
if(!ReadBlock()) |
| 57 |
return 0xFF; |
| 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 |