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
MemoryStream is neither in the header nor in the documentation nor in the examples.
I use this class in the tests, but I didn't want to publish it, because I'm pretty sure users will complain about its fixed capacity.
@skyhisi, do you really use this class in your project?
Hi Benoit,
I found it whilst browsing the source, and it seems to work fine to me.
My use case is to build up a string in memory with print to serialise up some data, before reading it out the buffer in blocks for sending over the network. I know the maximum size of the data I have, so a fixed size is fine for my use. Using MemoryStream was simpler than managing a buffer manually with sprintf and strcat 😃
Did you mean stream.readBytes() instead of buffer.readBytes()?
If so, you probably want to use BufferingPrint, WriteBufferingStream, or WriteBufferingClient, because that's exactly what they do (assuming that client is an instance of Client).
Yes, sorry I did mean stream.readBytes(), I was trying to simplify my code into the example and missed changing that.
In this case, I'm not sure I can use the normal buffering classes, as I need to get the size of the complete buffer (I'm using available) before anything is sent to the client.
My actual code is closer to this:
PubSubClient mqttClient(/*...*/);
voidupload()
{
// ...
MemoryStream influxLine(512);
influxLine.print(F("weather "));
influxLine.print(F("temperature="));
influxLine.print(temperature);
//...if (!mqttClient.beginPublish(TOPIC_PREFIX"/influx", influxLine.available(), true))
{
LOG("Failed to begin publish Influx update");
goto exit;
}
while ((len = influxLine.readBytes(buf, 64)) != 0)
{
mqttClient.write((uint8_t*)buf, len);
}
if (!mqttClient.endPublish())
{
LOG("Failed to end publish Influx update");
goto exit;
}
// ...
I would probably use sprintf_P() in a similar situation, but this is alright too.
I think you're right: this class seems to be useful outside of my unit tests.
As I told you, my main concern is that users will ask for an elastic capacity, so maybe I should rename it to FixedMemoryStream.
What do you think?
FixedMemoryStream is a good name. It allows in the future to have DynamicMemoryStream & StaticMemoryStream versions if required, which could be like the DynamicJsonDocument and StaticJsonDocument classes (variable size / compile time size).
Activity
bblanchon commentedon Jul 18, 2019
Hi @skyhisi,
MemoryStreamis neither in the header nor in the documentation nor in the examples.I use this class in the tests, but I didn't want to publish it, because I'm pretty sure users will complain about its fixed capacity.
@skyhisi, do you really use this class in your project?
Best Regards,
Benoit
skyhisi commentedon Jul 18, 2019
Hi Benoit,
I found it whilst browsing the source, and it seems to work fine to me.
My use case is to build up a string in memory with
printto serialise up some data, before reading it out the buffer in blocks for sending over the network. I know the maximum size of the data I have, so a fixed size is fine for my use. UsingMemoryStreamwas simpler than managing a buffer manually withsprintfandstrcat😃Something like:
If there's a better way of doing this, let me know.
Thanks,
Silas
bblanchon commentedon Jul 18, 2019
Did you mean
stream.readBytes()instead ofbuffer.readBytes()?If so, you probably want to use
BufferingPrint,WriteBufferingStream, orWriteBufferingClient, because that's exactly what they do (assuming thatclientis an instance ofClient).skyhisi commentedon Jul 18, 2019
Yes, sorry I did mean
stream.readBytes(), I was trying to simplify my code into the example and missed changing that.In this case, I'm not sure I can use the normal buffering classes, as I need to get the size of the complete buffer (I'm using
available) before anything is sent to the client.My actual code is closer to this:
Is there a better way to do this?
bblanchon commentedon Jul 19, 2019
I would probably use
sprintf_P()in a similar situation, but this is alright too.I think you're right: this class seems to be useful outside of my unit tests.
As I told you, my main concern is that users will ask for an elastic capacity, so maybe I should rename it to
FixedMemoryStream.What do you think?
skyhisi commentedon Jul 19, 2019
FixedMemoryStreamis a good name. It allows in the future to haveDynamicMemoryStream&StaticMemoryStreamversions if required, which could be like theDynamicJsonDocumentandStaticJsonDocumentclasses (variable size / compile time size).