7 "github.com/aws/aws-sdk-go/internal/sdkio"
10 // ReadSeekCloser wraps a io.Reader returning a ReaderSeekerCloser. Allows the
11 // SDK to accept an io.Reader that is not also an io.Seeker for unsigned
12 // streaming payload API operations.
14 // A ReadSeekCloser wrapping an nonseekable io.Reader used in an API
15 // operation's input will prevent that operation being retried in the case of
16 // network errors, and cause operation requests to fail if the operation
17 // requires payload signing.
19 // Note: If using With S3 PutObject to stream an object upload The SDK's S3
20 // Upload manager (s3manager.Uploader) provides support for streaming with the
21 // ability to retry network errors.
22 func ReadSeekCloser(r io.Reader) ReaderSeekerCloser {
23 return ReaderSeekerCloser{r}
26 // ReaderSeekerCloser represents a reader that can also delegate io.Seeker and
27 // io.Closer interfaces to the underlying object if they are available.
28 type ReaderSeekerCloser struct {
32 // IsReaderSeekable returns if the underlying reader type can be seeked. A
33 // io.Reader might not actually be seekable if it is the ReaderSeekerCloser
35 func IsReaderSeekable(r io.Reader) bool {
36 switch v := r.(type) {
37 case ReaderSeekerCloser:
39 case *ReaderSeekerCloser:
48 // Read reads from the reader up to size of p. The number of bytes read, and
49 // error if it occurred will be returned.
51 // If the reader is not an io.Reader zero bytes read, and nil error will be
54 // Performs the same functionality as io.Reader Read
55 func (r ReaderSeekerCloser) Read(p []byte) (int, error) {
56 switch t := r.r.(type) {
63 // Seek sets the offset for the next Read to offset, interpreted according to
64 // whence: 0 means relative to the origin of the file, 1 means relative to the
65 // current offset, and 2 means relative to the end. Seek returns the new offset
66 // and an error, if any.
68 // If the ReaderSeekerCloser is not an io.Seeker nothing will be done.
69 func (r ReaderSeekerCloser) Seek(offset int64, whence int) (int64, error) {
70 switch t := r.r.(type) {
72 return t.Seek(offset, whence)
77 // IsSeeker returns if the underlying reader is also a seeker.
78 func (r ReaderSeekerCloser) IsSeeker() bool {
79 _, ok := r.r.(io.Seeker)
83 // HasLen returns the length of the underlying reader if the value implements
84 // the Len() int method.
85 func (r ReaderSeekerCloser) HasLen() (int, bool) {
86 type lenner interface {
90 if lr, ok := r.r.(lenner); ok {
97 // GetLen returns the length of the bytes remaining in the underlying reader.
98 // Checks first for Len(), then io.Seeker to determine the size of the
101 // Will return -1 if the length cannot be determined.
102 func (r ReaderSeekerCloser) GetLen() (int64, error) {
103 if l, ok := r.HasLen(); ok {
107 if s, ok := r.r.(io.Seeker); ok {
114 // SeekerLen attempts to get the number of bytes remaining at the seeker's
115 // current position. Returns the number of bytes remaining or error.
116 func SeekerLen(s io.Seeker) (int64, error) {
117 // Determine if the seeker is actually seekable. ReaderSeekerCloser
118 // hides the fact that a io.Readers might not actually be seekable.
119 switch v := s.(type) {
120 case ReaderSeekerCloser:
122 case *ReaderSeekerCloser:
129 func seekerLen(s io.Seeker) (int64, error) {
130 curOffset, err := s.Seek(0, sdkio.SeekCurrent)
135 endOffset, err := s.Seek(0, sdkio.SeekEnd)
140 _, err = s.Seek(curOffset, sdkio.SeekStart)
145 return endOffset - curOffset, nil
148 // Close closes the ReaderSeekerCloser.
150 // If the ReaderSeekerCloser is not an io.Closer nothing will be done.
151 func (r ReaderSeekerCloser) Close() error {
152 switch t := r.r.(type) {
159 // A WriteAtBuffer provides a in memory buffer supporting the io.WriterAt interface
160 // Can be used with the s3manager.Downloader to download content to a buffer
161 // in memory. Safe to use concurrently.
162 type WriteAtBuffer struct {
166 // GrowthCoeff defines the growth rate of the internal buffer. By
167 // default, the growth rate is 1, where expanding the internal
168 // buffer will allocate only enough capacity to fit the new expected
173 // NewWriteAtBuffer creates a WriteAtBuffer with an internal buffer
175 func NewWriteAtBuffer(buf []byte) *WriteAtBuffer {
176 return &WriteAtBuffer{buf: buf}
179 // WriteAt writes a slice of bytes to a buffer starting at the position provided
180 // The number of bytes written will be returned, or error. Can overwrite previous
181 // written slices if the write ats overlap.
182 func (b *WriteAtBuffer) WriteAt(p []byte, pos int64) (n int, err error) {
184 expLen := pos + int64(pLen)
187 if int64(len(b.buf)) < expLen {
188 if int64(cap(b.buf)) < expLen {
189 if b.GrowthCoeff < 1 {
192 newBuf := make([]byte, expLen, int64(b.GrowthCoeff*float64(expLen)))
196 b.buf = b.buf[:expLen]
202 // Bytes returns a slice of bytes written to the buffer.
203 func (b *WriteAtBuffer) Bytes() []byte {