boundary_buffered_writer.go 514 B

123456789101112131415161718192021222324252627282930
  1. package writers
  2. import (
  3. "bufio"
  4. "io"
  5. )
  6. type BoundaryBufferedWriter struct {
  7. bw *bufio.Writer
  8. }
  9. func NewBoundaryBufferedWriter(w io.Writer, size int) *BoundaryBufferedWriter {
  10. return &BoundaryBufferedWriter{
  11. bw: bufio.NewWriterSize(w, size),
  12. }
  13. }
  14. func (b *BoundaryBufferedWriter) Write(p []byte) (int, error) {
  15. if len(p) > b.bw.Available() {
  16. err := b.bw.Flush()
  17. if err != nil {
  18. return 0, err
  19. }
  20. }
  21. return b.bw.Write(p)
  22. }
  23. func (b *BoundaryBufferedWriter) Flush() error {
  24. return b.bw.Flush()
  25. }