Outputs exactly the same bytes as the input stream, in different chunks. A chunk boundary occurs after each delimiter, or, if maxLength is non-zero, after maxLength bytes, whichever comes first. Your callback can return false to stop consuming the stream at any time.
Declared in <folly/gen/String.h>
template<class Callback>
class StreamSplitter;
The splitter buffers the last incomplete chunk, so you must call flush() to consume the piece of the stream after the final delimiter. This piece may be empty. After a flush(), the splitter can be re-used for a new stream.
operator() and flush() return false iff your callback returns false. The internal buffer is not flushed, so reusing such a splitter will have indeterminate results. Same goes if your callback throws. Feel free to fix these corner cases if needed.
Tips:
Create via streamSplitter() to take advantage of template deduction.
If your callback needs an end-of-stream signal, test for "no trailing delimiter and shorter than maxLength".
You can fine-tune the initial capacity of the internal IOBuf.
| Name | Description |
|---|---|
StreamSplitter [constructor] | Constructs a splitter over a delimiter with a chunk callback. |
flush | Consume any incomplete last line (may be empty). Do this before destroying the StreamSplitter, or you will fail to consume part of the input. |
operator() | Consume another piece of the input stream. |
| Name | Description |
|---|---|
streamSplitter | Creates a StreamSplitter, deducing the callback type. |