DASH Media Packaging SDK
 All Classes Namespaces Functions Variables Typedefs Enumerator
buffer_reader.h
1 // Copyright 2014 Google Inc. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #ifndef MEDIA_BASE_BUFFER_READER_H_
8 #define MEDIA_BASE_BUFFER_READER_H_
9 
10 #include <stdint.h>
11 
12 #include <vector>
13 
14 #include "packager/base/compiler_specific.h"
15 #include "packager/base/macros.h"
16 
17 namespace edash_packager {
18 namespace media {
19 
22 class BufferReader {
23  public:
25  BufferReader(const uint8_t* buf, size_t size)
26  : buf_(buf), size_(size), pos_(0) {}
27  ~BufferReader() {}
28 
31  bool HasBytes(size_t count) { return pos() + count <= size(); }
32 
37  bool Read1(uint8_t* v) WARN_UNUSED_RESULT;
38  bool Read2(uint16_t* v) WARN_UNUSED_RESULT;
39  bool Read2s(int16_t* v) WARN_UNUSED_RESULT;
40  bool Read4(uint32_t* v) WARN_UNUSED_RESULT;
41  bool Read4s(int32_t* v) WARN_UNUSED_RESULT;
42  bool Read8(uint64_t* v) WARN_UNUSED_RESULT;
43  bool Read8s(int64_t* v) WARN_UNUSED_RESULT;
45 
51  bool ReadNBytesInto8(uint64_t* v, size_t num_bytes) WARN_UNUSED_RESULT;
52  bool ReadNBytesInto8s(int64_t* v, size_t num_bytes) WARN_UNUSED_RESULT;
54 
55  bool ReadToVector(std::vector<uint8_t>* t, size_t count) WARN_UNUSED_RESULT;
56 
59  bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT;
60 
61  const uint8_t* data() const { return buf_; }
62  size_t size() const { return size_; }
63  void set_size(size_t size) { size_ = size; }
64  size_t pos() const { return pos_; }
65 
66  private:
67  // Internal implementation of multi-byte reads.
68  template <typename T>
69  bool Read(T* t) WARN_UNUSED_RESULT;
70  template <typename T>
71  bool ReadNBytes(T* t, size_t num_bytes) WARN_UNUSED_RESULT;
72 
73  const uint8_t* buf_;
74  size_t size_;
75  size_t pos_;
76 
77  DISALLOW_COPY_AND_ASSIGN(BufferReader);
78 };
79 
80 } // namespace media
81 } // namespace edash_packager
82 
83 #endif // MEDIA_BASE_BUFFER_READER_H_
bool Read1(uint8_t *v) WARN_UNUSED_RESULT
bool SkipBytes(size_t num_bytes) WARN_UNUSED_RESULT
bool ReadNBytesInto8(uint64_t *v, size_t num_bytes) WARN_UNUSED_RESULT
BufferReader(const uint8_t *buf, size_t size)
Create a BufferReader from a raw buffer.
Definition: buffer_reader.h:25