github.com/aavshr/aws-sdk-go@v1.41.3/service/s3/s3manager/read_seeker_write_to.go (about)

     1  package s3manager
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  )
     7  
     8  // ReadSeekerWriteTo defines an interface implementing io.WriteTo and io.ReadSeeker
     9  type ReadSeekerWriteTo interface {
    10  	io.ReadSeeker
    11  	io.WriterTo
    12  }
    13  
    14  // BufferedReadSeekerWriteTo wraps a BufferedReadSeeker with an io.WriteAt
    15  // implementation.
    16  type BufferedReadSeekerWriteTo struct {
    17  	*BufferedReadSeeker
    18  }
    19  
    20  // WriteTo writes to the given io.Writer from BufferedReadSeeker until there's no more data to write or
    21  // an error occurs. Returns the number of bytes written and any error encountered during the write.
    22  func (b *BufferedReadSeekerWriteTo) WriteTo(writer io.Writer) (int64, error) {
    23  	return io.Copy(writer, b.BufferedReadSeeker)
    24  }
    25  
    26  // ReadSeekerWriteToProvider provides an implementation of io.WriteTo for an io.ReadSeeker
    27  type ReadSeekerWriteToProvider interface {
    28  	GetWriteTo(seeker io.ReadSeeker) (r ReadSeekerWriteTo, cleanup func())
    29  }
    30  
    31  // BufferedReadSeekerWriteToPool uses a sync.Pool to create and reuse
    32  // []byte slices for buffering parts in memory
    33  type BufferedReadSeekerWriteToPool struct {
    34  	pool sync.Pool
    35  }
    36  
    37  // NewBufferedReadSeekerWriteToPool will return a new BufferedReadSeekerWriteToPool that will create
    38  // a pool of reusable buffers . If size is less then < 64 KiB then the buffer
    39  // will default to 64 KiB. Reason: io.Copy from writers or readers that don't support io.WriteTo or io.ReadFrom
    40  // respectively will default to copying 32 KiB.
    41  func NewBufferedReadSeekerWriteToPool(size int) *BufferedReadSeekerWriteToPool {
    42  	if size < 65536 {
    43  		size = 65536
    44  	}
    45  
    46  	return &BufferedReadSeekerWriteToPool{
    47  		pool: sync.Pool{New: func() interface{} {
    48  			return make([]byte, size)
    49  		}},
    50  	}
    51  }
    52  
    53  // GetWriteTo will wrap the provided io.ReadSeeker with a BufferedReadSeekerWriteTo.
    54  // The provided cleanup must be called after operations have been completed on the
    55  // returned io.ReadSeekerWriteTo in order to signal the return of resources to the pool.
    56  func (p *BufferedReadSeekerWriteToPool) GetWriteTo(seeker io.ReadSeeker) (r ReadSeekerWriteTo, cleanup func()) {
    57  	buffer := p.pool.Get().([]byte)
    58  
    59  	r = &BufferedReadSeekerWriteTo{BufferedReadSeeker: NewBufferedReadSeeker(seeker, buffer)}
    60  	cleanup = func() {
    61  		p.pool.Put(buffer)
    62  	}
    63  
    64  	return r, cleanup
    65  }