BinaryReader.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "BinaryReader.h"
  2. #include <stdlib.h> // for size_t
  3. #include <cstdint> // for uint8_t
  4. #include <type_traits> // for remove_extent_t
  5. #include "ByteStream.h" // for ByteStream
  6. bell::BinaryReader::BinaryReader(std::shared_ptr<ByteStream> stream) {
  7. this->stream = stream;
  8. }
  9. size_t bell::BinaryReader::position() {
  10. return stream->position();
  11. }
  12. size_t bell::BinaryReader::size() {
  13. return stream->size();
  14. }
  15. void bell::BinaryReader::close() {
  16. stream->close();
  17. }
  18. void bell::BinaryReader::skip(size_t pos) {
  19. std::vector<uint8_t> b(pos);
  20. stream->read(&b[0], pos);
  21. }
  22. int32_t bell::BinaryReader::readInt() {
  23. uint8_t b[4];
  24. if (stream->read((uint8_t*)b, 4) != 4)
  25. return 0;
  26. return static_cast<int32_t>((b[3]) | (b[2] << 8) | (b[1] << 16) |
  27. (b[0] << 24));
  28. }
  29. int16_t bell::BinaryReader::readShort() {
  30. uint8_t b[2];
  31. if (stream->read((uint8_t*)b, 2) != 2)
  32. return 0;
  33. return static_cast<int16_t>((b[1]) | (b[0] << 8));
  34. }
  35. uint32_t bell::BinaryReader::readUInt() {
  36. return readInt() & 0xffffffffL;
  37. }
  38. uint8_t bell::BinaryReader::readByte() {
  39. uint8_t b[1];
  40. if (stream->read((uint8_t*)b, 1) != 1)
  41. return 0;
  42. return b[0];
  43. }
  44. std::vector<uint8_t> bell::BinaryReader::readBytes(size_t size) {
  45. std::vector<uint8_t> data(size);
  46. stream->read(&data[0], size);
  47. return data;
  48. }
  49. long long bell::BinaryReader::readLong() {
  50. long high = readInt();
  51. long low = readInt();
  52. return static_cast<long long>(((long long)high << 32) | low);
  53. }