github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/libkb/buf.go (about)

     1  // Copyright 2015 Keybase, Inc. All rights reserved. Use of
     2  // this source code is governed by the included BSD license.
     3  
     4  package libkb
     5  
     6  import (
     7  	"bufio"
     8  	"bytes"
     9  	"io"
    10  )
    11  
    12  // BufferCloser is a Buffer that satisfies the io.Closer
    13  // interface.
    14  type BufferCloser struct {
    15  	*bytes.Buffer
    16  }
    17  
    18  func NewBufferCloser() *BufferCloser {
    19  	return &BufferCloser{Buffer: new(bytes.Buffer)}
    20  }
    21  
    22  func (b *BufferCloser) Close() error { return nil }
    23  
    24  // BufferWriter has a bufio Writer that will Flush before Close.
    25  type BufferWriter struct {
    26  	wc io.WriteCloser
    27  	w  *bufio.Writer
    28  }
    29  
    30  func NewBufferWriter(wc io.WriteCloser) *BufferWriter {
    31  	return &BufferWriter{
    32  		wc: wc,
    33  		w:  bufio.NewWriter(wc),
    34  	}
    35  }
    36  
    37  func (b *BufferWriter) Write(p []byte) (n int, err error) {
    38  	return b.w.Write(p)
    39  }
    40  
    41  func (b *BufferWriter) Close() error {
    42  	b.w.Flush()
    43  	return b.wc.Close()
    44  }