Buffered File Writer
Asked at Datadog· Screen
Reported by the problem's creator, not verified
A file accepts writes, and every write to it is slow. You implement a writer that sits in front of the file and collects characters in an in-memory buffer, so the file receives fewer, larger writes. The buffer holds at most capacity characters, and every character counts as one byte. When the buffer holds exactly capacity characters, its contents go to the file as a single write and the buffer is emptied. Characters reach the file in the order they were written; none are lost or sent twice. Each test creates one writer and checks the return value of every operation.
Examples
Example 1
Input: init =
[8], operations =[["write",["abc"]],["write",["defgh"]],["write",["ij"]],["flush",[]],["flush",[]]]Output:
[[],["abcdefgh"],[],["ij"],[]]Explanation:
abcleaves 5 free slots, so the next write fills the buffer exactly andabcdefghgoes out as one write.ijwaits in the buffer untilflushsends it; the secondflushfinds the buffer empty and sends nothing.