You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+51-6Lines changed: 51 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,6 +27,7 @@ For example, with this library, you can:
27
27
* debug your program more easily by logging what it sends to a Web service
28
28
* send large data with the [Wire library](https://www.arduino.cc/en/reference/wire)
29
29
* use a `String`, EEPROM, or `PROGMEM` with a stream interface
30
+
* decode HTTP chunks
30
31
31
32
Read on to see how StreamUtils can help you!
32
33
@@ -37,11 +38,11 @@ How to add buffering to a Stream?
37
38
### Buffering read operations
38
39
39
40
Sometimes, you can significantly improve performance by reading many bytes at once.
40
-
For example, [according to SPIFFS's wiki](https://github.com/pellepl/spiffs/wiki/Performance-and-Optimizing#reading-files), reading read files in chunks of 64 bytes is much faster than reading them one byte at a time.
41
+
For example, [according to SPIFFS's wiki](https://github.com/pellepl/spiffs/wiki/Performance-and-Optimizing#reading-files), reading files in chunks of 64 bytes is much faster than reading them one byte at a time.
To buffer the input, decorate the original `Stream` with `ReadBufferingStream`. For example, suppose your program reads a JSON document from SPIFFS like that:
45
+
To buffer the input, decorate the original `Stream` with `ReadBufferingStream`. For example, suppose your program reads a JSON document from SPIFFS like this:
45
46
46
47
```c++
47
48
File file = SPIFFS.open("example.json", "r");
@@ -61,7 +62,7 @@ Unfortunately, this optimization is only possible if:
61
62
1.`Stream.readBytes()` is declared `virtual` in your Arduino Code (as it's the case for ESP8266), and
62
63
2. the derived class has an optimized implementation of `readBytes()` (as it's the case for SPIFFS' `File`).
63
64
64
-
When possible, prefer `ReadBufferingClient` to `ReadBufferingStream` because `Client` defines a `read()` method similar to `readBytes()`, except that this one is `virtual` on all platforms.
65
+
When possible, prefer `ReadBufferingClient` to `ReadBufferingStream` because `Client` defines a `read()` method similar to `readBytes()`, except this one is `virtual` on all platforms.
65
66
66
67
If memory allocation fails, `ReadBufferingStream` behaves as if no buffer was used: it forwards all calls to the upstream `Stream`.
67
68
@@ -74,7 +75,7 @@ For example, writing to `WiFiClient` one byte at a time is very slow; it's much
To add a buffer, decorate the original `Stream` with `WriteBufferingStream`. For example, if your program sends a JSON document via `WiFiClient`, like that:
78
+
To add a buffer, decorate the original `Stream` with `WriteBufferingStream`. For example, if your program sends a JSON document via `WiFiClient` like this:
78
79
79
80
```c++
80
81
serializeJson(doc, wifiClient);
@@ -166,7 +167,7 @@ char response[256];
166
167
client.readBytes(response, 256);
167
168
```
168
169
169
-
Then decorate `client` and replace the calls:
170
+
Then, decorate `client` and replace the calls:
170
171
171
172
```c++
172
173
LoggingStream loggingClient(client, Serial);
@@ -187,7 +188,7 @@ These extra bits increase the amount of traffic but allow correcting any one-bit
187
188
If you use this encoding on an 8-bit channel, it effectively doubles the amount of traffic. However, if you use an [`HardwareSerial`](https://www.arduino.cc/reference/en/language/functions/communication/serial/) instance (like `Serial`, `Serial1`...), you can slightly reduce the overhead by configuring the ports as a 7-bit channel, like so:
188
189
189
190
```c++
190
-
// Initialize serial port with 9600 bauds, 7-bits of data, no parity, and one stop bit
191
+
// Initialize serial port with 9600 bauds, 7bits of data, no parity, and one stop bit
`ProgmemStream`'s constructor also supports `const __FlashStringHelper*` (the type returned by the `F()` macro) and an optional second argument to specify the size of the buffer.
348
349
350
+
How to decode HTTP chunks?
351
+
--------------------------
352
+
353
+
HTTP servers can send their response in multiple parts using [Chunked Transfer Encoding](https://en.wikipedia.org/wiki/Chunked_transfer_encoding). Clients using HTTP 1.1 must support this encoding as it's not optional and is dictated by the server.
354
+
355
+
`ChunkDecodingStream` and `ChunkDecodingClient` are decorators that decode the chunks and make the response available as a regular stream.
Note that `HTTPClient` already performs chunk decoding **if** you use `getString()`, but you might want to use `getStream()` to avoid buffering the entire response in memory.
391
+
392
+
Also, you can avoid chunked transfer encoding by downgrading the HTTP version to 1.0. `HTTPClient` allows you to do that by calling `useHTTP10(true)` before sending the request.
349
393
350
394
Summary
351
395
-------
@@ -367,6 +411,7 @@ See the equivalence table below.
Prefer `XxxClient` to `XxxStream` because, unlike `Stream::readBytes()`, `Client::read()` is virtual on all cores and therefore allows optimized implementations.
0 commit comments