@@ -40,28 +40,24 @@ class URLStreamDefault : public AbstractURLStream {
40
40
41
41
URLStreamDefault (int readBufferSize=DEFAULT_BUFFER_SIZE){
42
42
TRACEI ();
43
- read_buffer = new uint8_t [ readBufferSize] ;
43
+ read_buffer_size = readBufferSize;
44
44
}
45
45
46
46
URLStreamDefault (Client &clientPar, int readBufferSize=DEFAULT_BUFFER_SIZE){
47
47
TRACEI ();
48
- read_buffer = new uint8_t [ readBufferSize] ;
48
+ read_buffer_size = readBufferSize;
49
49
client = &clientPar;
50
50
}
51
51
52
52
URLStreamDefault (const char * network, const char *password, int readBufferSize=DEFAULT_BUFFER_SIZE) {
53
53
TRACEI ();
54
- read_buffer = new uint8_t [ readBufferSize] ;
54
+ read_buffer_size = readBufferSize;
55
55
this ->network = (char *)network;
56
56
this ->password = (char *)password;
57
57
}
58
58
59
59
~URLStreamDefault (){
60
60
TRACEI ();
61
- if (read_buffer!=nullptr ){
62
- delete[] read_buffer;
63
- read_buffer = nullptr ;
64
- }
65
61
if (clientSecure!=nullptr ){
66
62
delete clientSecure;
67
63
clientSecure = nullptr ;
@@ -73,7 +69,6 @@ class URLStreamDefault : public AbstractURLStream {
73
69
end ();
74
70
}
75
71
76
-
77
72
virtual bool begin (const char * urlStr, const char * acceptMime=nullptr , MethodID action=GET, const char * reqMime=" " , const char *reqData=" " ) override {
78
73
LOGI ( " %s: %s" ,LOG_METHOD, urlStr);
79
74
@@ -112,13 +107,15 @@ class URLStreamDefault : public AbstractURLStream {
112
107
virtual size_t readBytes (uint8_t *buffer, size_t length) override {
113
108
if (!active || !request) return 0 ;
114
109
115
- size_t read = request.read ((uint8_t *)buffer, length);
110
+ size_t read = request.read ((uint8_t *)& buffer[ 0 ] , length);
116
111
total_read+=read;
117
112
return read;
118
113
}
119
114
120
115
virtual int read () override {
121
116
if (!active) return -1 ;
117
+ // lazy allocation since this is rarely used
118
+ read_buffer.resize (read_buffer_size);
122
119
123
120
fillBuffer ();
124
121
total_read++;
@@ -127,6 +124,8 @@ class URLStreamDefault : public AbstractURLStream {
127
124
128
125
virtual int peek () override {
129
126
if (!active) return -1 ;
127
+ // lazy allocation since this is rarely used
128
+ read_buffer.resize (read_buffer_size);
130
129
131
130
fillBuffer ();
132
131
return isEOS () ? -1 : read_buffer[read_pos];
@@ -171,7 +170,7 @@ class URLStreamDefault : public AbstractURLStream {
171
170
long size;
172
171
long total_read;
173
172
// buffered read
174
- uint8_t * read_buffer= nullptr ;
173
+ Vector< uint8_t > read_buffer{ 0 } ;
175
174
uint16_t read_buffer_size;
176
175
uint16_t read_pos;
177
176
uint16_t read_size;
@@ -258,7 +257,7 @@ class URLStreamDefault : public AbstractURLStream {
258
257
inline void fillBuffer () {
259
258
if (isEOS ()){
260
259
// 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);
262
261
read_pos = 0 ;
263
262
}
264
263
}
0 commit comments