Flutter Linux Embedder
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_CLIENT_WRAPPER_BYTE_BUFFER_STREAMS_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_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_CLIENT_WRAPPER_BYTE_BUFFER_STREAMS_H_
flutter::ByteBufferStreamReader::ReadByte
uint8_t ReadByte() override
Definition: byte_buffer_streams.h:29
i
int i
Definition: fl_socket_accessible.cc:18
flutter::ByteBufferStreamWriter::WriteByte
void WriteByte(uint8_t byte)
Definition: byte_buffer_streams.h:77
flutter::ByteStreamReader
Definition: byte_streams.h:13
flutter::ByteBufferStreamReader
Definition: byte_buffer_streams.h:19
flutter::ByteBufferStreamWriter::ByteBufferStreamWriter
ByteBufferStreamWriter(std::vector< uint8_t > *buffer)
Definition: byte_buffer_streams.h:69
flutter::ByteBufferStreamWriter::WriteAlignment
void WriteAlignment(uint8_t alignment)
Definition: byte_buffer_streams.h:86
flutter::ByteBufferStreamReader::ReadBytes
void ReadBytes(uint8_t *buffer, size_t length) override
Definition: byte_buffer_streams.h:38
flutter::ByteBufferStreamWriter::~ByteBufferStreamWriter
virtual ~ByteBufferStreamWriter()=default
flutter
Definition: accessibility_bridge.cc:14
flutter::ByteBufferStreamReader::ByteBufferStreamReader
ByteBufferStreamReader(const uint8_t *bytes, size_t size)
Definition: byte_buffer_streams.h:23
byte_streams.h
flutter::ByteBufferStreamReader::~ByteBufferStreamReader
virtual ~ByteBufferStreamReader()=default
buffer
static const uint8_t buffer[]
Definition: fl_pixel_buffer_texture_test.cc:44
flutter::ByteBufferStreamWriter::WriteBytes
void WriteBytes(const uint8_t *bytes, size_t length)
Definition: byte_buffer_streams.h:80
flutter::ByteBufferStreamReader::ReadAlignment
void ReadAlignment(uint8_t alignment) override
Definition: byte_buffer_streams.h:48
length
size_t length
Definition: fl_standard_message_codec_test.cc:1113
flutter::ByteStreamWriter
Definition: byte_streams.h:52
flutter::ByteBufferStreamWriter
Definition: byte_buffer_streams.h:65