7 "github.com/aws/aws-sdk-go/internal/sdkio"
10 // ReadSeekCloser wraps a io.Reader returning a ReaderSeekerCloser. Should
11 // only be used with an io.Reader that is also an io.Seeker. Doing so may
12 // cause request signature errors, or request body's not sent for GET, HEAD
13 // and DELETE HTTP methods.
15 // Deprecated: Should only be used with io.ReadSeeker. If using for
16 // S3 PutObject to stream content use s3manager.Uploader instead.
17 func ReadSeekCloser(r io.Reader) ReaderSeekerCloser {
18 return ReaderSeekerCloser{r}
21 // ReaderSeekerCloser represents a reader that can also delegate io.Seeker and
22 // io.Closer interfaces to the underlying object if they are available.
23 type ReaderSeekerCloser struct {
27 // IsReaderSeekable returns if the underlying reader type can be seeked. A
28 // io.Reader might not actually be seekable if it is the ReaderSeekerCloser
30 func IsReaderSeekable(r io.Reader) bool {
31 switch v := r.(type) {
32 case ReaderSeekerCloser:
34 case *ReaderSeekerCloser:
43 // Read reads from the reader up to size of p. The number of bytes read, and
44 // error if it occurred will be returned.
46 // If the reader is not an io.Reader zero bytes read, and nil error will be returned.
48 // Performs the same functionality as io.Reader Read
49 func (r ReaderSeekerCloser) Read(p []byte) (int, error) {
50 switch t := r.r.(type) {
57 // Seek sets the offset for the next Read to offset, interpreted according to
58 // whence: 0 means relative to the origin of the file, 1 means relative to the
59 // current offset, and 2 means relative to the end. Seek returns the new offset
60 // and an error, if any.
62 // If the ReaderSeekerCloser is not an io.Seeker nothing will be done.
63 func (r ReaderSeekerCloser) Seek(offset int64, whence int) (int64, error) {
64 switch t := r.r.(type) {
66 return t.Seek(offset, whence)
71 // IsSeeker returns if the underlying reader is also a seeker.
72 func (r ReaderSeekerCloser) IsSeeker() bool {
73 _, ok := r.r.(io.Seeker)
77 // HasLen returns the length of the underlying reader if the value implements
78 // the Len() int method.
79 func (r ReaderSeekerCloser) HasLen() (int, bool) {
80 type lenner interface {
84 if lr, ok := r.r.(lenner); ok {
91 // GetLen returns the length of the bytes remaining in the underlying reader.
92 // Checks first for Len(), then io.Seeker to determine the size of the
95 // Will return -1 if the length cannot be determined.
96 func (r ReaderSeekerCloser) GetLen() (int64, error) {
97 if l, ok := r.HasLen(); ok {
101 if s, ok := r.r.(io.Seeker); ok {
108 // SeekerLen attempts to get the number of bytes remaining at the seeker's
109 // current position. Returns the number of bytes remaining or error.
110 func SeekerLen(s io.Seeker) (int64, error) {
111 // Determine if the seeker is actually seekable. ReaderSeekerCloser
112 // hides the fact that a io.Readers might not actually be seekable.
113 switch v := s.(type) {
114 case ReaderSeekerCloser:
116 case *ReaderSeekerCloser:
123 func seekerLen(s io.Seeker) (int64, error) {
124 curOffset, err := s.Seek(0, sdkio.SeekCurrent)
129 endOffset, err := s.Seek(0, sdkio.SeekEnd)
134 _, err = s.Seek(curOffset, sdkio.SeekStart)
139 return endOffset - curOffset, nil
142 // Close closes the ReaderSeekerCloser.
144 // If the ReaderSeekerCloser is not an io.Closer nothing will be done.
145 func (r ReaderSeekerCloser) Close() error {
146 switch t := r.r.(type) {
153 // A WriteAtBuffer provides a in memory buffer supporting the io.WriterAt interface
154 // Can be used with the s3manager.Downloader to download content to a buffer
155 // in memory. Safe to use concurrently.
156 type WriteAtBuffer struct {
160 // GrowthCoeff defines the growth rate of the internal buffer. By
161 // default, the growth rate is 1, where expanding the internal
162 // buffer will allocate only enough capacity to fit the new expected
167 // NewWriteAtBuffer creates a WriteAtBuffer with an internal buffer
169 func NewWriteAtBuffer(buf []byte) *WriteAtBuffer {
170 return &WriteAtBuffer{buf: buf}
173 // WriteAt writes a slice of bytes to a buffer starting at the position provided
174 // The number of bytes written will be returned, or error. Can overwrite previous
175 // written slices if the write ats overlap.
176 func (b *WriteAtBuffer) WriteAt(p []byte, pos int64) (n int, err error) {
178 expLen := pos + int64(pLen)
181 if int64(len(b.buf)) < expLen {
182 if int64(cap(b.buf)) < expLen {
183 if b.GrowthCoeff < 1 {
186 newBuf := make([]byte, expLen, int64(b.GrowthCoeff*float64(expLen)))
190 b.buf = b.buf[:expLen]
196 // Bytes returns a slice of bytes written to the buffer.
197 func (b *WriteAtBuffer) Bytes() []byte {