func (z *Writer) writeUint32(x uint32) error {
	buf := z.buf[:4]
	binary.LittleEndian.PutUint32(buf, x)
	_, err := z.dst.Write(buf)
	return err
}
writeUint32 writes a uint32 to the underlying writer.
package lz4
import (
	"encoding/binary"
	"fmt"
	"github.com/pierrec/lz4/internal/xxh32"
	"io"
	"runtime"
)
type zResult struct {
	size     uint32  
	data     []byte  
	checksum uint32  
}
type Writer struct {
	Header
	OnBlockDone func(size int)
	buf       [19]byte       
	dst       io.Writer      
	checksum  xxh32.XXHZero  
	data      []byte         
	idx       int            
	hashtable [winSize]int   
	c   chan chan zResult  
	err error              
}
func NewWriter(dst io.Writer) *Writer {
	z := new(Writer)
	z.Reset(dst)
	return z
}
func (z *Writer) WithConcurrency(n int) *Writer {
	switch {
	case n == 0 || n == 1:
		z.c = nil
		return z
	case n < 0:
		n = runtime.GOMAXPROCS(0)
	}
	z.c = make(chan chan zResult, n)
	go func() {
		for c := range z.c {
			res := <-c
			n := len(res.data)
			if n == 0 {
				close(c)
				return
			}
			if err := z.writeUint32(res.size); err != nil && z.err == nil {
				z.err = err
			}
			if _, err := z.dst.Write(res.data); err != nil && z.err == nil {
				z.err = err
			}
			if z.BlockChecksum {
				if err := z.writeUint32(res.checksum); err != nil && z.err == nil {
					z.err = err
				}
			}
			if isCompressed := res.size&compressedBlockFlag == 0; isCompressed {
				putBuffer(cap(res.data), res.data)
			}
			if h := z.OnBlockDone; h != nil {
				h(n)
			}
			close(c)
		}
	}()
	return z
}
func (z *Writer) newBuffers() {
	bSize := z.Header.BlockMaxSize
	buf := getBuffer(bSize)
	z.data = buf[:bSize]  
}
func (z *Writer) freeBuffers() {
	putBuffer(z.Header.BlockMaxSize, z.data)
	z.data = nil
}
func (z *Writer) writeHeader() error {
	if z.Header.BlockMaxSize == 0 {
		z.Header.BlockMaxSize = blockSize4M
	}
	bSize := z.Header.BlockMaxSize
	if !isValidBlockSize(z.Header.BlockMaxSize) {
		return fmt.Errorf("lz4: invalid block max size: %d", bSize)
	}
	z.newBuffers()
	z.idx = 0
	buf := z.buf[:]
	binary.LittleEndian.PutUint32(buf[0:], frameMagic)
	flg := byte(Version << 6)
	flg |= 1 << 5  
	if z.Header.BlockChecksum {
		flg |= 1 << 4
	}
	if z.Header.Size > 0 {
		flg |= 1 << 3
	}
	if !z.Header.NoChecksum {
		flg |= 1 << 2
	}
	buf[4] = flg
	buf[5] = blockSizeValueToIndex(z.Header.BlockMaxSize) << 4
	n := 6
	if z.Header.Size > 0 {
		binary.LittleEndian.PutUint64(buf[n:], z.Header.Size)
		n += 8
	}
	buf[n] = byte(xxh32.ChecksumZero(buf[4:n]) >> 8 & 0xFF)
	z.checksum.Reset()
	if _, err := z.dst.Write(buf[0 : n+1]); err != nil {
		return err
	}
	z.Header.done = true
	if debugFlag {
		debug("wrote header %v", z.Header)
	}
	return nil
}
func (z *Writer) Write(buf []byte) (int, error) {
	if !z.Header.done {
		if err := z.writeHeader(); err != nil {
			return 0, err
		}
	}
	if debugFlag {
		debug("input buffer len=%d index=%d", len(buf), z.idx)
	}
	zn := len(z.data)
	var n int
	for len(buf) > 0 {
		if z.idx == 0 && len(buf) >= zn {
			if err := z.compressBlock(buf[:zn]); err != nil {
				return n, err
			}
			n += zn
			buf = buf[zn:]
			continue
		}
		m := copy(z.data[z.idx:], buf)
		n += m
		z.idx += m
		buf = buf[m:]
		if debugFlag {
			debug("%d bytes copied to buf, current index %d", n, z.idx)
		}
		if z.idx < len(z.data) {
			if debugFlag {
				debug("need more data for compression")
			}
			return n, nil
		}
		if err := z.compressBlock(z.data); err != nil {
			return n, err
		}
		z.idx = 0
	}
	return n, nil
}
func (z *Writer) compressBlock(data []byte) error {
	if !z.NoChecksum {
		_, _ = z.checksum.Write(data)
	}
	if z.c != nil {
		c := make(chan zResult)
		z.c <- c  
		go writerCompressBlock(c, z.Header, data)
		return nil
	}
	zdata := z.data[z.Header.BlockMaxSize:cap(z.data)]
	var zn int
	if level := z.Header.CompressionLevel; level != 0 {
		zn, _ = CompressBlockHC(data, zdata, level)
	} else {
		zn, _ = CompressBlock(data, zdata, z.hashtable[:])
	}
	var bLen uint32
	if debugFlag {
		debug("block compression %d => %d", len(data), zn)
	}
	if zn > 0 && zn < len(data) {
		bLen = uint32(zn)
		zdata = zdata[:zn]
	} else {
		bLen = uint32(len(data)) | compressedBlockFlag
		zdata = data
	}
	if debugFlag {
		debug("block compression to be written len=%d data len=%d", bLen, len(zdata))
	}
	if err := z.writeUint32(bLen); err != nil {
		return err
	}
	written, err := z.dst.Write(zdata)
	if err != nil {
		return err
	}
	if h := z.OnBlockDone; h != nil {
		h(written)
	}
	if !z.BlockChecksum {
		if debugFlag {
			debug("current frame checksum %x", z.checksum.Sum32())
		}
		return nil
	}
	checksum := xxh32.ChecksumZero(zdata)
	if debugFlag {
		debug("block checksum %x", checksum)
		defer func() { debug("current frame checksum %x", z.checksum.Sum32()) }()
	}
	return z.writeUint32(checksum)
}
func (z *Writer) Flush() error {
	if debugFlag {
		debug("flush with index %d", z.idx)
	}
	if z.idx == 0 {
		return nil
	}
	data := z.data[:z.idx]
	z.idx = 0
	if z.c == nil {
		return z.compressBlock(data)
	}
	if !z.NoChecksum {
		_, _ = z.checksum.Write(data)
	}
	c := make(chan zResult)
	z.c <- c
	writerCompressBlock(c, z.Header, data)
	return nil
}
func (z *Writer) close() error {
	if z.c == nil {
		return nil
	}
	c := make(chan zResult)
	z.c <- c
	c <- zResult{}
	<-c
	z.c = nil
	return z.err
}
func (z *Writer) Close() error {
	if !z.Header.done {
		if err := z.writeHeader(); err != nil {
			return err
		}
	}
	if err := z.Flush(); err != nil {
		return err
	}
	if err := z.close(); err != nil {
		return err
	}
	z.freeBuffers()
	if debugFlag {
		debug("writing last empty block")
	}
	if err := z.writeUint32(0); err != nil {
		return err
	}
	if z.NoChecksum {
		return nil
	}
	checksum := z.checksum.Sum32()
	if debugFlag {
		debug("stream checksum %x", checksum)
	}
	return z.writeUint32(checksum)
}
func (z *Writer) Reset(w io.Writer) {
	n := cap(z.c)
	_ = z.close()
	z.freeBuffers()
	z.Header.Reset()
	z.dst = w
	z.checksum.Reset()
	z.idx = 0
	z.err = nil
	z.WithConcurrency(n)
}