8 // ReadSeekCloser wraps a io.Reader returning a ReaderSeekerCloser. Should
9 // only be used with an io.Reader that is also an io.Seeker. Doing so may
10 // cause request signature errors, or request body's not sent for GET, HEAD
11 // and DELETE HTTP methods.
13 // Deprecated: Should only be used with io.ReadSeeker. If using for
14 // S3 PutObject to stream content use s3manager.Uploader instead.
15 func ReadSeekCloser(r io.Reader) ReaderSeekerCloser {
16 return ReaderSeekerCloser{r}
19 // ReaderSeekerCloser represents a reader that can also delegate io.Seeker and
20 // io.Closer interfaces to the underlying object if they are available.
21 type ReaderSeekerCloser struct {
25 // Read reads from the reader up to size of p. The number of bytes read, and
26 // error if it occurred will be returned.
28 // If the reader is not an io.Reader zero bytes read, and nil error will be returned.
30 // Performs the same functionality as io.Reader Read
31 func (r ReaderSeekerCloser) Read(p []byte) (int, error) {
32 switch t := r.r.(type) {
39 // Seek sets the offset for the next Read to offset, interpreted according to
40 // whence: 0 means relative to the origin of the file, 1 means relative to the
41 // current offset, and 2 means relative to the end. Seek returns the new offset
42 // and an error, if any.
44 // If the ReaderSeekerCloser is not an io.Seeker nothing will be done.
45 func (r ReaderSeekerCloser) Seek(offset int64, whence int) (int64, error) {
46 switch t := r.r.(type) {
48 return t.Seek(offset, whence)
53 // IsSeeker returns if the underlying reader is also a seeker.
54 func (r ReaderSeekerCloser) IsSeeker() bool {
55 _, ok := r.r.(io.Seeker)
59 // Close closes the ReaderSeekerCloser.
61 // If the ReaderSeekerCloser is not an io.Closer nothing will be done.
62 func (r ReaderSeekerCloser) Close() error {
63 switch t := r.r.(type) {
70 // A WriteAtBuffer provides a in memory buffer supporting the io.WriterAt interface
71 // Can be used with the s3manager.Downloader to download content to a buffer
72 // in memory. Safe to use concurrently.
73 type WriteAtBuffer struct {
77 // GrowthCoeff defines the growth rate of the internal buffer. By
78 // default, the growth rate is 1, where expanding the internal
79 // buffer will allocate only enough capacity to fit the new expected
84 // NewWriteAtBuffer creates a WriteAtBuffer with an internal buffer
86 func NewWriteAtBuffer(buf []byte) *WriteAtBuffer {
87 return &WriteAtBuffer{buf: buf}
90 // WriteAt writes a slice of bytes to a buffer starting at the position provided
91 // The number of bytes written will be returned, or error. Can overwrite previous
92 // written slices if the write ats overlap.
93 func (b *WriteAtBuffer) WriteAt(p []byte, pos int64) (n int, err error) {
95 expLen := pos + int64(pLen)
98 if int64(len(b.buf)) < expLen {
99 if int64(cap(b.buf)) < expLen {
100 if b.GrowthCoeff < 1 {
103 newBuf := make([]byte, expLen, int64(b.GrowthCoeff*float64(expLen)))
107 b.buf = b.buf[:expLen]
113 // Bytes returns a slice of bytes written to the buffer.
114 func (b *WriteAtBuffer) Bytes() []byte {