Sponge
CS144's user-space TCP library
Public Member Functions | Private Attributes | List of all members
ByteStream Class Reference

An in-order byte stream. More...

#include <byte_stream.hh>

Public Member Functions

 ByteStream (const size_t capacity)
 Construct a stream with room for capacity bytes. More...
 
"Input" interface for the writer
size_t write (const std::string &data)
 
size_t remaining_capacity () const
 
void end_input ()
 Signal that the byte stream has reached its ending. More...
 
void set_error ()
 Indicate that the stream suffered an error. More...
 
"Output" interface for the reader
std::string peek_output (const size_t len) const
 
void pop_output (const size_t len)
 Remove bytes from the buffer. More...
 
std::string read (const size_t len)
 
bool input_ended () const
 
bool error () const
 
size_t buffer_size () const
 
bool buffer_empty () const
 
bool eof () const
 
General accounting
size_t bytes_written () const
 Total number of bytes written. More...
 
size_t bytes_read () const
 Total number of bytes popped. More...
 

Private Attributes

bool _error {}
 Flag indicating that the stream suffered an error. More...
 

Detailed Description

An in-order byte stream.

Bytes are written on the "input" side and read from the "output" side. The byte stream is finite: the writer can end the input, and then no more bytes can be written.

Definition at line 11 of file byte_stream.hh.

Constructor & Destructor Documentation

◆ ByteStream()

ByteStream::ByteStream ( const size_t  capacity)

Construct a stream with room for capacity bytes.

Definition at line 15 of file byte_stream.cc.

Member Function Documentation

◆ buffer_empty()

bool ByteStream::buffer_empty ( ) const
Returns
true if the buffer is empty

Definition at line 45 of file byte_stream.cc.

◆ buffer_size()

size_t ByteStream::buffer_size ( ) const
Returns
the maximum amount that can currently be read from the stream

Definition at line 43 of file byte_stream.cc.

◆ bytes_read()

size_t ByteStream::bytes_read ( ) const

Total number of bytes popped.

Definition at line 51 of file byte_stream.cc.

◆ bytes_written()

size_t ByteStream::bytes_written ( ) const

Total number of bytes written.

Definition at line 49 of file byte_stream.cc.

◆ end_input()

void ByteStream::end_input ( )

Signal that the byte stream has reached its ending.

Definition at line 39 of file byte_stream.cc.

◆ eof()

bool ByteStream::eof ( ) const
Returns
true if the output has reached the ending

Definition at line 47 of file byte_stream.cc.

◆ error()

bool ByteStream::error ( ) const
inline
Returns
true if the stream has suffered an error

Definition at line 62 of file byte_stream.hh.

◆ input_ended()

bool ByteStream::input_ended ( ) const
Returns
true if the stream input has ended

Definition at line 41 of file byte_stream.cc.

◆ peek_output()

string ByteStream::peek_output ( const size_t  len) const

Peek at next "len" bytes of the stream

Returns
a string
Parameters
[in]lenbytes will be copied from the output side of the buffer

Definition at line 23 of file byte_stream.cc.

◆ pop_output()

void ByteStream::pop_output ( const size_t  len)

Remove bytes from the buffer.

Parameters
[in]lenbytes will be removed from the output side of the buffer

Definition at line 29 of file byte_stream.cc.

◆ read()

std::string ByteStream::read ( const size_t  len)

Read (i.e., copy and then pop) the next "len" bytes of the stream

Returns
a string

Read (i.e., copy and then pop) the next "len" bytes of the stream

Parameters
[in]lenbytes will be popped and returned
Returns
a string

Definition at line 34 of file byte_stream.cc.

◆ remaining_capacity()

size_t ByteStream::remaining_capacity ( ) const
Returns
the number of additional bytes that the stream has space for

Definition at line 53 of file byte_stream.cc.

◆ set_error()

void ByteStream::set_error ( )
inline

Indicate that the stream suffered an error.

Definition at line 41 of file byte_stream.hh.

◆ write()

size_t ByteStream::write ( const std::string data)

Write a string of bytes into the stream. Write as many as will fit, and return how many were written.

Returns
the number of bytes accepted into the stream

Definition at line 17 of file byte_stream.cc.

Member Data Documentation

◆ _error

bool ByteStream::_error {}
private

Flag indicating that the stream suffered an error.

Definition at line 20 of file byte_stream.hh.


The documentation for this class was generated from the following files: