1 // Copyright (c) 2016 The WebM project authors. All Rights Reserved. 2 // 3 // Use of this source code is governed by a BSD-style license 4 // that can be found in the LICENSE file in the root of the source 5 // tree. An additional intellectual property rights grant can be found 6 // in the file PATENTS. All contributing project authors may 7 // be found in the AUTHORS file in the root of the source tree. 8 #ifndef INCLUDE_WEBM_BUFFER_READER_H_ 9 #define INCLUDE_WEBM_BUFFER_READER_H_ 10 11 #include <cstddef> 12 #include <cstdint> 13 #include <initializer_list> 14 #include <vector> 15 16 #include "./reader.h" 17 #include "./status.h" 18 19 /** 20 \file 21 A `Reader` implementation that reads from a `std::vector<std::uint8_t>`. 22 */ 23 24 namespace webm { 25 26 /** 27 \addtogroup PUBLIC_API 28 @{ 29 */ 30 31 /** 32 A simple reader that reads data from a buffer of bytes. 33 */ 34 class BufferReader : public Reader { 35 public: 36 /** 37 Constructs a new, empty reader. 38 */ 39 BufferReader() = default; 40 41 /** 42 Constructs a new reader by copying the provided reader into the new reader. 43 44 \param other The source reader to copy. 45 */ 46 BufferReader(const BufferReader& other) = default; 47 48 /** 49 Copies the provided reader into this reader. 50 51 \param other The source reader to copy. May be equal to `*this`, in which 52 case this is a no-op. 53 \return `*this`. 54 */ 55 BufferReader& operator=(const BufferReader& other) = default; 56 57 /** 58 Constructs a new reader by moving the provided reader into the new reader. 59 60 \param other The source reader to move. After moving, it will be reset to an 61 empty stream. 62 */ 63 BufferReader(BufferReader&&); 64 65 /** 66 Moves the provided reader into this reader. 67 68 \param other The source reader to move. After moving, it will be reset to an 69 empty stream. May be equal to `*this`, in which case this is a no-op. 70 \return `*this`. 71 */ 72 BufferReader& operator=(BufferReader&&); 73 74 /** 75 Creates a new `BufferReader` populated with the provided bytes. 76 77 \param bytes Bytes that are assigned to the internal buffer and used as the 78 source which is read from. 79 */ 80 BufferReader(std::initializer_list<std::uint8_t> bytes); 81 82 /** 83 Creates a new `BufferReader` populated with the provided data. 84 85 \param vector A vector of bytes that is copied to the internal buffer and 86 used as the source which is read from. 87 */ 88 explicit BufferReader(const std::vector<std::uint8_t>& vector); 89 90 /** 91 Creates a new `BufferReader` populated with the provided data. 92 93 \param vector A vector of bytes that is moved to the internal buffer and used 94 as the source which is read from. 95 */ 96 explicit BufferReader(std::vector<std::uint8_t>&& vector); 97 98 /** 99 Resets the reader to read from the given list of bytes, starting at the 100 beginning. 101 102 This makes `reader = {1, 2, 3};` effectively equivalent to `reader = 103 BufferReader({1, 2, 3});`. 104 105 \param bytes Bytes that are assigned to the internal buffer and used as the 106 source which is read from. 107 \return `*this`. 108 */ 109 BufferReader& operator=(std::initializer_list<std::uint8_t> bytes); 110 111 Status Read(std::size_t num_to_read, std::uint8_t* buffer, 112 std::uint64_t* num_actually_read) override; 113 114 Status Skip(std::uint64_t num_to_skip, 115 std::uint64_t* num_actually_skipped) override; 116 117 std::uint64_t Position() const override; 118 119 /** 120 Gets the total size of the buffer. 121 */ size()122 std::size_t size() const { return data_.size(); } 123 124 private: 125 // Stores the byte buffer from which data is read. 126 std::vector<std::uint8_t> data_; 127 128 // The position of the reader in the byte buffer. 129 std::size_t pos_ = 0; 130 }; 131 132 /** 133 @} 134 */ 135 136 } // namespace webm 137 138 #endif // INCLUDE_WEBM_BUFFER_READER_H_ 139