Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion w.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion w_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down