Shouldn't one of these be `self.buf.len()`? I think that a test of two successive buffered writes would have caught this.