BinaryReader.cpp 1.4 KB

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