Flutter Engine
byte_buffer_streams.h
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_BYTE_BUFFER_STREAMS_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_BYTE_BUFFER_STREAMS_H_
7 
8 #include <cassert>
9 #include <cstdint>
10 #include <cstring>
11 #include <iostream>
12 #include <vector>
13 
15 
16 namespace flutter {
17 
18 // Implementation of ByteStreamReader base on a byte array.
20  public:
21  // Createa a reader reading from |bytes|, which must have a length of |size|.
22  // |bytes| must remain valid for the lifetime of this object.
23  explicit ByteBufferStreamReader(const uint8_t* bytes, size_t size)
24  : bytes_(bytes), size_(size) {}
25 
26  virtual ~ByteBufferStreamReader() = default;
27 
28  // |ByteStreamReader|
29  uint8_t ReadByte() override {
30  if (location_ >= size_) {
31  std::cerr << "Invalid read in StandardCodecByteStreamReader" << std::endl;
32  return 0;
33  }
34  return bytes_[location_++];
35  }
36 
37  // |ByteStreamReader|
38  void ReadBytes(uint8_t* buffer, size_t length) override {
39  if (location_ + length > size_) {
40  std::cerr << "Invalid read in StandardCodecByteStreamReader" << std::endl;
41  return;
42  }
43  std::memcpy(buffer, &bytes_[location_], length);
44  location_ += length;
45  }
46 
47  // |ByteStreamReader|
48  void ReadAlignment(uint8_t alignment) override {
49  uint8_t mod = location_ % alignment;
50  if (mod) {
51  location_ += alignment - mod;
52  }
53  }
54 
55  private:
56  // The buffer to read from.
57  const uint8_t* bytes_;
58  // The total size of the buffer.
59  size_t size_;
60  // The current read location.
61  size_t location_ = 0;
62 };
63 
64 // Implementation of ByteStreamWriter based on a byte array.
66  public:
67  // Creates a writer that writes into |buffer|.
68  // |buffer| must remain valid for the lifetime of this object.
69  explicit ByteBufferStreamWriter(std::vector<uint8_t>* buffer)
70  : bytes_(buffer) {
71  assert(buffer);
72  }
73 
74  virtual ~ByteBufferStreamWriter() = default;
75 
76  // |ByteStreamWriter|
77  void WriteByte(uint8_t byte) { bytes_->push_back(byte); }
78 
79  // |ByteStreamWriter|
80  void WriteBytes(const uint8_t* bytes, size_t length) {
81  assert(length > 0);
82  bytes_->insert(bytes_->end(), bytes, bytes + length);
83  }
84 
85  // |ByteStreamWriter|
86  void WriteAlignment(uint8_t alignment) {
87  uint8_t mod = bytes_->size() % alignment;
88  if (mod) {
89  for (int i = 0; i < alignment - mod; ++i) {
90  WriteByte(0);
91  }
92  }
93  }
94 
95  private:
96  // The buffer to write to.
97  std::vector<uint8_t>* bytes_;
98 };
99 
100 } // namespace flutter
101 
102 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_BYTE_BUFFER_STREAMS_H_
void WriteAlignment(uint8_t alignment)
void ReadBytes(uint8_t *buffer, size_t length) override
void WriteBytes(const uint8_t *bytes, size_t length)
ByteBufferStreamReader(const uint8_t *bytes, size_t size)
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
virtual ~ByteBufferStreamReader()=default
void ReadAlignment(uint8_t alignment) override
size_t length
ByteBufferStreamWriter(std::vector< uint8_t > *buffer)