diff --git a/w.go b/w.go index 005a39a..94ae564 100644 --- a/w.go +++ b/w.go @@ -16,7 +16,12 @@ type Writer struct { // Write implements io.Writer. func (w *Writer) Write(p []byte) (n int, err error) { if w.stream != nil { - return 0, errStreaming + if len(w.Buf) > 0 { + if w.Flush() { + return 0, w.stream.writeErr + } + } + return w.stream.writer.Write(p) } w.Buf = append(w.Buf, p...) return len(p), nil @@ -63,6 +68,17 @@ func (w *Writer) Grow(n int) { w.Buf = buf.Bytes() } +// Flush flushes the stream. It does nothing if not in streaming mode +func (w *Writer) Flush() (fail bool) { + if w.stream != nil { + w.Buf, fail = w.stream.flush(w.Buf) + if fail { + return true + } + } + return false +} + // byte writes a single byte. func (w *Writer) byte(c byte) (fail bool) { if w.stream == nil { diff --git a/w_stream.go b/w_stream.go index 0ad90b0..9a6c536 100644 --- a/w_stream.go +++ b/w_stream.go @@ -94,7 +94,6 @@ func writeStreamByteseqSlow[S byteseq.Byteseq](w *Writer, s S) bool { if fail { return true } - n := copy(w.Buf[len(w.Buf):cap(w.Buf)], s) s = s[n:] w.Buf = w.Buf[:len(w.Buf)+n]