package jx
import (
"errors"
"io"
"github.com/go-faster/jx/internal/byteseq"
)
func (w *Writer ) Close () error {
if w .stream == nil {
return nil
}
_ , fail := w .stream .flush (w .Buf )
if fail {
return w .stream .writeErr
}
return nil
}
var errStreaming = errors .New ("unexpected call in streaming mode" )
type streamState struct {
writer io .Writer
writeErr error
}
func newStreamState (w io .Writer ) *streamState {
return &streamState {
writer : w ,
}
}
func (s *streamState ) mustNotBeStreaming () {
if s != nil {
panic (errStreaming )
}
}
func (s *streamState ) Reset (w io .Writer ) {
s .writer = w
s .writeErr = nil
}
func (s *streamState ) setError (err error ) {
s .writeErr = err
}
func (s *streamState ) fail () bool {
return s .writeErr != nil
}
func (s *streamState ) flush (buf []byte ) ([]byte , bool ) {
if s .fail () {
return nil , true
}
n , err := s .writer .Write (buf )
switch {
case err != nil :
s .setError (err )
return nil , true
case n != len (buf ):
s .setError (io .ErrShortWrite )
return nil , true
default :
buf = buf [:0 ]
return buf , false
}
}
func writeStreamBytes (w *Writer , s ...byte ) bool {
return writeStreamByteseq (w , s )
}
func writeStreamByteseq [S byteseq .Byteseq ](w *Writer , s S ) bool {
if w .stream == nil {
w .Buf = append (w .Buf , s ...)
return false
}
return writeStreamByteseqSlow (w , s )
}
func writeStreamByteseqSlow [S byteseq .Byteseq ](w *Writer , s S ) bool {
if w .stream .fail () {
return true
}
for len (w .Buf )+len (s ) > cap (w .Buf ) {
var fail bool
w .Buf , fail = w .stream .flush (w .Buf )
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 ]
}
w .Buf = append (w .Buf , s ...)
return false
}
The pages are generated with Golds v0.6.7 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds .