Buffering

In general, writing efficiently to files is best done in chunks, such as by building a line of data in a String and writing it in one go.

Sming offers a simple write-through buffering mechanism which can be used where necessary. The ArduinoJson can benefit greatly from this.

template<size_t size>
class StaticPrintBuffer : public BasePrintBuffer

Write-through buffer using stack only.

Example usage:

 FileStream stream("file.txt", File::ReadWrite);
    {
     StaticPrintBuffer<256> buffer(stream);
        writeSomeData(buffer);
    } // Buffer flushed and destroyed when it goes out of scope

Template Parameters:

size – Size of buffer

Public Functions

inline StaticPrintBuffer(Print &output)

Construct a stack-based buffer.

Parameters:

output – Print destination

class HeapPrintBuffer : public BasePrintBuffer

Write-through buffer using heap storage.

Example usage:

 FileStream stream("file.txt", File::ReadWrite);
    {
     HeapPrintBuffer buffer(stream, 512);
        writeSomeData(buffer);
    } // Buffer flushed and destroyed when it goes out of scope

Public Functions

inline HeapPrintBuffer(Print &output, size_t size)

Construct a stack-based buffer.

Parameters:
  • output – Print destination

  • size – Buffer size

class DynamicPrintBuffer : public BasePrintBuffer

Write-through buffer using heap storage and owned stream pointer.

Example usage:

 auto stream = std::make_unique<FileStream>("file.txt", File::ReadWrite);
 auto bufferedStream = new DynamicPrintBuffer(std::move(stream), 512);

 // write to bufferedStream as required via callbacks, etc.
 ...

 // This destroys both buffer *and* the file stream
 delete bufferedStream;

Public Functions

inline DynamicPrintBuffer(std::unique_ptr<Print> &&output, size_t size)

Construct a stack-based buffer.

Parameters:
  • output – Print destination, will take ownership of this

  • size – Buffer size