Flutter Engine
byte_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_INCLUDE_FLUTTER_BYTE_STREAMS_H_
6 #define FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BYTE_STREAMS_H_
7 
8 // Interfaces for interacting with a stream of bytes, for use in codecs.
9 
10 namespace flutter {
11 
12 // An interface for a class that reads from a byte stream.
14  public:
15  explicit ByteStreamReader() = default;
16  virtual ~ByteStreamReader() = default;
17 
18  // Reads and returns the next byte from the stream.
19  virtual uint8_t ReadByte() = 0;
20 
21  // Reads the next |length| bytes from the stream into |buffer|. The caller
22  // is responsible for ensuring that |buffer| is large enough.
23  virtual void ReadBytes(uint8_t* buffer, size_t length) = 0;
24 
25  // Advances the read cursor to the next multiple of |alignment| relative to
26  // the start of the stream, unless it is already aligned.
27  virtual void ReadAlignment(uint8_t alignment) = 0;
28 
29  // Reads and returns the next 32-bit integer from the stream.
30  int32_t ReadInt32() {
31  int32_t value = 0;
32  ReadBytes(reinterpret_cast<uint8_t*>(&value), 4);
33  return value;
34  }
35 
36  // Reads and returns the next 64-bit integer from the stream.
37  int64_t ReadInt64() {
38  int64_t value = 0;
39  ReadBytes(reinterpret_cast<uint8_t*>(&value), 8);
40  return value;
41  }
42 
43  // Reads and returns the next 64-bit floating point number from the stream.
44  double ReadDouble() {
45  double value = 0;
46  ReadBytes(reinterpret_cast<uint8_t*>(&value), 8);
47  return value;
48  }
49 };
50 
51 // An interface for a class that writes to a byte stream.
53  public:
54  explicit ByteStreamWriter() = default;
55  virtual ~ByteStreamWriter() = default;
56 
57  // Writes |byte| to the stream.
58  virtual void WriteByte(uint8_t byte) = 0;
59 
60  // Writes the next |length| bytes from |bytes| to the stream
61  virtual void WriteBytes(const uint8_t* bytes, size_t length) = 0;
62 
63  // Writes 0s until the next multiple of |alignment| relative to the start
64  // of the stream, unless the write positition is already aligned.
65  virtual void WriteAlignment(uint8_t alignment) = 0;
66 
67  // Writes the given 32-bit int to the stream.
68  void WriteInt32(int32_t value) {
69  WriteBytes(reinterpret_cast<const uint8_t*>(&value), 4);
70  }
71 
72  // Writes the given 64-bit int to the stream.
73  void WriteInt64(int64_t value) {
74  WriteBytes(reinterpret_cast<const uint8_t*>(&value), 8);
75  }
76 
77  // Writes the given 36-bit double to the stream.
78  void WriteDouble(double value) {
79  WriteBytes(reinterpret_cast<const uint8_t*>(&value), 8);
80  }
81 };
82 
83 } // namespace flutter
84 
85 #endif // FLUTTER_SHELL_PLATFORM_COMMON_CPP_CLIENT_WRAPPER_INCLUDE_FLUTTER_BYTE_STREAMS_H_
virtual void ReadAlignment(uint8_t alignment)=0
virtual void ReadBytes(uint8_t *buffer, size_t length)=0
void WriteDouble(double value)
Definition: byte_streams.h:78
virtual ~ByteStreamReader()=default
uint8_t value
size_t length
void WriteInt32(int32_t value)
Definition: byte_streams.h:68
void WriteInt64(int64_t value)
Definition: byte_streams.h:73
virtual uint8_t ReadByte()=0