-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbyte_stream.cc
More file actions
82 lines (63 loc) · 1.75 KB
/
byte_stream.cc
File metadata and controls
82 lines (63 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "byte_stream.hh"
#include "debug.hh"
using namespace std;
ByteStream::ByteStream( uint64_t capacity ) : capacity_( capacity ) { buffer_.resize(capacity_); }
void Writer::push( string data )
{
if ( closed_ && not data.empty() ) {
throw runtime_error( "Writer:: push() called on closed stream" );
}
const uint64_t can_push = available_capacity();
const uint64_t to_push = min( can_push, static_cast<uint64_t>( data.size() ) );
for ( uint64_t i = 0; i < to_push; ++i ) {
buffer_[( bytes_pushed_ + i ) % capacity_] = data[i];
}
bytes_pushed_ += to_push;
}
void Writer::close()
{
closed_ = true;
}
bool Writer::is_closed() const
{
return closed_;
}
uint64_t Writer::available_capacity() const
{
return capacity_ - ( bytes_pushed_ - bytes_popped_ );
}
uint64_t Writer::bytes_pushed() const
{
return bytes_pushed_;
}
string_view Reader::peek() const
{
// Only return contiguous data from current position to end
const uint64_t start_pos = bytes_popped_ % capacity_;
const uint64_t bytes_in_buffer = bytes_pushed_ - bytes_popped_;
if ( bytes_in_buffer == 0 ) {
return string_view(); // Empty view
}
// Return only the contiguous portion before wraparound
const uint64_t available = min( bytes_in_buffer, capacity_ - start_pos );
return string_view( buffer_.data() + start_pos, available );
}
void Reader::pop( uint64_t len )
{
if ( len > bytes_buffered() ) {
throw runtime_error( "Reader::pop() called with len greater than buffered size" );
}
bytes_popped_ += len;
}
bool Reader::is_finished() const
{
return closed_ && bytes_popped_ == bytes_pushed_;
}
uint64_t Reader::bytes_buffered() const
{
return bytes_pushed_ - bytes_popped_;
}
uint64_t Reader:: bytes_popped() const
{
return bytes_popped_;
}