buffered_writer.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package io
  2. import (
  3. "io"
  4. "sync"
  5. "github.com/v2ray/v2ray-core/common/alloc"
  6. )
  7. type BufferedWriter struct {
  8. sync.Mutex
  9. writer io.Writer
  10. buffer *alloc.Buffer
  11. cached bool
  12. }
  13. func NewBufferedWriter(rawWriter io.Writer) *BufferedWriter {
  14. return &BufferedWriter{
  15. writer: rawWriter,
  16. buffer: alloc.NewBuffer().Clear(),
  17. cached: true,
  18. }
  19. }
  20. func (this *BufferedWriter) ReadFrom(reader io.Reader) (int64, error) {
  21. this.Lock()
  22. defer this.Unlock()
  23. if this.writer == nil {
  24. return 0, io.EOF
  25. }
  26. totalBytes := int64(0)
  27. for {
  28. nBytes, err := this.buffer.FillFrom(reader)
  29. if err != nil {
  30. if err == io.EOF {
  31. return totalBytes, nil
  32. }
  33. return totalBytes, err
  34. }
  35. totalBytes += int64(nBytes)
  36. this.FlushWithoutLock()
  37. }
  38. }
  39. func (this *BufferedWriter) Write(b []byte) (int, error) {
  40. this.Lock()
  41. defer this.Unlock()
  42. if this.writer == nil {
  43. return 0, io.EOF
  44. }
  45. if !this.cached {
  46. return this.writer.Write(b)
  47. }
  48. nBytes, _ := this.buffer.Write(b)
  49. if this.buffer.IsFull() {
  50. this.FlushWithoutLock()
  51. }
  52. return nBytes, nil
  53. }
  54. func (this *BufferedWriter) Flush() error {
  55. this.Lock()
  56. defer this.Unlock()
  57. if this.writer == nil {
  58. return io.EOF
  59. }
  60. return this.FlushWithoutLock()
  61. }
  62. func (this *BufferedWriter) FlushWithoutLock() error {
  63. defer this.buffer.Clear()
  64. for !this.buffer.IsEmpty() {
  65. nBytes, err := this.writer.Write(this.buffer.Value)
  66. if err != nil {
  67. return err
  68. }
  69. this.buffer.SliceFrom(nBytes)
  70. }
  71. return nil
  72. }
  73. func (this *BufferedWriter) Cached() bool {
  74. return this.cached
  75. }
  76. func (this *BufferedWriter) SetCached(cached bool) {
  77. this.cached = cached
  78. if !cached && !this.buffer.IsEmpty() {
  79. this.Flush()
  80. }
  81. }
  82. func (this *BufferedWriter) Release() {
  83. this.Flush()
  84. this.Lock()
  85. defer this.Unlock()
  86. this.buffer.Release()
  87. this.buffer = nil
  88. this.writer = nil
  89. }