michael@0: // InBuffer.h michael@0: michael@0: #ifndef __INBUFFER_H michael@0: #define __INBUFFER_H michael@0: michael@0: #include "../IStream.h" michael@0: #include "../../Common/MyCom.h" michael@0: michael@0: #ifndef _NO_EXCEPTIONS michael@0: class CInBufferException michael@0: { michael@0: public: michael@0: HRESULT ErrorCode; michael@0: CInBufferException(HRESULT errorCode): ErrorCode(errorCode) {} michael@0: }; michael@0: #endif michael@0: michael@0: class CInBuffer michael@0: { michael@0: Byte *_buffer; michael@0: Byte *_bufferLimit; michael@0: Byte *_bufferBase; michael@0: CMyComPtr _stream; michael@0: UInt64 _processedSize; michael@0: UInt32 _bufferSize; michael@0: bool _wasFinished; michael@0: michael@0: bool ReadBlock(); michael@0: Byte ReadBlock2(); michael@0: michael@0: public: michael@0: #ifdef _NO_EXCEPTIONS michael@0: HRESULT ErrorCode; michael@0: #endif michael@0: michael@0: CInBuffer(); michael@0: ~CInBuffer() { Free(); } michael@0: michael@0: bool Create(UInt32 bufferSize); michael@0: void Free(); michael@0: michael@0: void SetStream(ISequentialInStream *stream); michael@0: void Init(); michael@0: void ReleaseStream() { _stream.Release(); } michael@0: michael@0: bool ReadByte(Byte &b) michael@0: { michael@0: if(_buffer >= _bufferLimit) michael@0: if(!ReadBlock()) michael@0: return false; michael@0: b = *_buffer++; michael@0: return true; michael@0: } michael@0: Byte ReadByte() michael@0: { michael@0: if(_buffer >= _bufferLimit) michael@0: return ReadBlock2(); michael@0: return *_buffer++; michael@0: } michael@0: void ReadBytes(void *data, UInt32 size, UInt32 &processedSize) michael@0: { michael@0: for(processedSize = 0; processedSize < size; processedSize++) michael@0: if (!ReadByte(((Byte *)data)[processedSize])) michael@0: return; michael@0: } michael@0: bool ReadBytes(void *data, UInt32 size) michael@0: { michael@0: UInt32 processedSize; michael@0: ReadBytes(data, size, processedSize); michael@0: return (processedSize == size); michael@0: } michael@0: UInt64 GetProcessedSize() const { return _processedSize + (_buffer - _bufferBase); } michael@0: bool WasFinished() const { return _wasFinished; } michael@0: }; michael@0: michael@0: #endif