Skip to content

Commit a480a92

Browse files
committed
URLStreamDefault lazy memory alloc
1 parent 2a1deb5 commit a480a92

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/AudioHttp/URLStream.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,28 +40,24 @@ class URLStreamDefault : public AbstractURLStream {
4040

4141
URLStreamDefault(int readBufferSize=DEFAULT_BUFFER_SIZE){
4242
TRACEI();
43-
read_buffer = new uint8_t[readBufferSize];
43+
read_buffer_size = readBufferSize;
4444
}
4545

4646
URLStreamDefault(Client &clientPar, int readBufferSize=DEFAULT_BUFFER_SIZE){
4747
TRACEI();
48-
read_buffer = new uint8_t[readBufferSize];
48+
read_buffer_size = readBufferSize;
4949
client = &clientPar;
5050
}
5151

5252
URLStreamDefault(const char* network, const char *password, int readBufferSize=DEFAULT_BUFFER_SIZE) {
5353
TRACEI();
54-
read_buffer = new uint8_t[readBufferSize];
54+
read_buffer_size = readBufferSize;
5555
this->network = (char*)network;
5656
this->password = (char*)password;
5757
}
5858

5959
~URLStreamDefault(){
6060
TRACEI();
61-
if (read_buffer!=nullptr){
62-
delete[] read_buffer;
63-
read_buffer = nullptr;
64-
}
6561
if (clientSecure!=nullptr){
6662
delete clientSecure;
6763
clientSecure = nullptr;
@@ -73,7 +69,6 @@ class URLStreamDefault : public AbstractURLStream {
7369
end();
7470
}
7571

76-
7772
virtual bool begin(const char* urlStr, const char* acceptMime=nullptr, MethodID action=GET, const char* reqMime="", const char*reqData="") override{
7873
LOGI( "%s: %s",LOG_METHOD, urlStr);
7974

@@ -112,13 +107,15 @@ class URLStreamDefault : public AbstractURLStream {
112107
virtual size_t readBytes(uint8_t *buffer, size_t length) override {
113108
if (!active || !request) return 0;
114109

115-
size_t read = request.read((uint8_t*)buffer, length);
110+
size_t read = request.read((uint8_t*)&buffer[0], length);
116111
total_read+=read;
117112
return read;
118113
}
119114

120115
virtual int read() override {
121116
if (!active) return -1;
117+
// lazy allocation since this is rarely used
118+
read_buffer.resize(read_buffer_size);
122119

123120
fillBuffer();
124121
total_read++;
@@ -127,6 +124,8 @@ class URLStreamDefault : public AbstractURLStream {
127124

128125
virtual int peek() override {
129126
if (!active) return -1;
127+
// lazy allocation since this is rarely used
128+
read_buffer.resize(read_buffer_size);
130129

131130
fillBuffer();
132131
return isEOS() ? -1 : read_buffer[read_pos];
@@ -171,7 +170,7 @@ class URLStreamDefault : public AbstractURLStream {
171170
long size;
172171
long total_read;
173172
// buffered read
174-
uint8_t *read_buffer=nullptr;
173+
Vector<uint8_t> read_buffer{0};
175174
uint16_t read_buffer_size;
176175
uint16_t read_pos;
177176
uint16_t read_size;
@@ -258,7 +257,7 @@ class URLStreamDefault : public AbstractURLStream {
258257
inline void fillBuffer() {
259258
if (isEOS()){
260259
// if we consumed all bytes we refill the buffer
261-
read_size = readBytes(read_buffer,read_buffer_size);
260+
read_size = readBytes(&read_buffer[0],read_buffer_size);
262261
read_pos = 0;
263262
}
264263
}

0 commit comments

Comments
 (0)