InBuffer.h 1.5 KB

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