buffered_writer.go 940 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fileexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter"
  4. import (
  5. "bufio"
  6. "errors"
  7. "io"
  8. )
  9. // bufferedWriteCloser is intended to use more memory
  10. // in order to optimize writing to disk to help improve performance.
  11. type bufferedWriteCloser struct {
  12. wrapped io.Closer
  13. buffered *bufio.Writer
  14. }
  15. var (
  16. _ io.WriteCloser = (*bufferedWriteCloser)(nil)
  17. )
  18. func newBufferedWriteCloser(f io.WriteCloser) io.WriteCloser {
  19. return &bufferedWriteCloser{
  20. wrapped: f,
  21. buffered: bufio.NewWriter(f),
  22. }
  23. }
  24. func (bwc *bufferedWriteCloser) Write(p []byte) (n int, err error) {
  25. return bwc.buffered.Write(p)
  26. }
  27. func (bwc *bufferedWriteCloser) Close() error {
  28. return errors.Join(
  29. bwc.buffered.Flush(),
  30. bwc.wrapped.Close(),
  31. )
  32. }
  33. func (bwc *bufferedWriteCloser) flush() error {
  34. return bwc.buffered.Flush()
  35. }