Sponge
CS144's user-space TCP library
byte_stream.hh
Go to the documentation of this file.
1 #ifndef SPONGE_LIBSPONGE_BYTE_STREAM_HH
2 #define SPONGE_LIBSPONGE_BYTE_STREAM_HH
3 
4 #include <string>
5 
7 
11 class ByteStream {
12  private:
13  // Your code here -- add private members as necessary.
14 
15  // Hint: This doesn't need to be a sophisticated data structure at
16  // all, but if any of your tests are taking longer than a second,
17  // that's a sign that you probably want to keep exploring
18  // different approaches.
19 
20  bool _error{};
21 
22  public:
24  ByteStream(const size_t capacity);
25 
28 
32  size_t write(const std::string &data);
33 
35  size_t remaining_capacity() const;
36 
38  void end_input();
39 
41  void set_error() { _error = true; }
43 
46 
49  std::string peek_output(const size_t len) const;
50 
52  void pop_output(const size_t len);
53 
56  std::string read(const size_t len);
57 
59  bool input_ended() const;
60 
62  bool error() const { return _error; }
63 
65  size_t buffer_size() const;
66 
68  bool buffer_empty() const;
69 
71  bool eof() const;
73 
76 
78  size_t bytes_written() const;
79 
81  size_t bytes_read() const;
83 };
84 
85 #endif // SPONGE_LIBSPONGE_BYTE_STREAM_HH
An in-order byte stream.
Definition: byte_stream.hh:11
size_t bytes_read() const
Total number of bytes popped.
Definition: byte_stream.cc:51
size_t bytes_written() const
Total number of bytes written.
Definition: byte_stream.cc:49
size_t write(const std::string &data)
Definition: byte_stream.cc:17
bool input_ended() const
Definition: byte_stream.cc:41
bool _error
Flag indicating that the stream suffered an error.
Definition: byte_stream.hh:20
std::string read(const size_t len)
Definition: byte_stream.cc:34
ByteStream(const size_t capacity)
Construct a stream with room for capacity bytes.
Definition: byte_stream.cc:15
bool eof() const
Definition: byte_stream.cc:47
void set_error()
Indicate that the stream suffered an error.
Definition: byte_stream.hh:41
size_t remaining_capacity() const
Definition: byte_stream.cc:53
size_t buffer_size() const
Definition: byte_stream.cc:43
bool error() const
Definition: byte_stream.hh:62
void pop_output(const size_t len)
Remove bytes from the buffer.
Definition: byte_stream.cc:29
std::string peek_output(const size_t len) const
Definition: byte_stream.cc:23
void end_input()
Signal that the byte stream has reached its ending.
Definition: byte_stream.cc:39
bool buffer_empty() const
Definition: byte_stream.cc:45