aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/google.golang.org/api/gensupport/buffer.go
diff options
context:
space:
mode:
authorNathan Dench <ndenc2@gmail.com>2019-05-24 15:16:44 +1000
committerNathan Dench <ndenc2@gmail.com>2019-05-24 15:16:44 +1000
commit107c1cdb09c575aa2f61d97f48d8587eb6bada4c (patch)
treeca7d008643efc555c388baeaf1d986e0b6b3e28c /vendor/google.golang.org/api/gensupport/buffer.go
parent844b5a68d8af4791755b8f0ad293cc99f5959183 (diff)
downloadterraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.gz
terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.tar.zst
terraform-provider-statuscake-107c1cdb09c575aa2f61d97f48d8587eb6bada4c.zip
Upgrade to 0.12
Diffstat (limited to 'vendor/google.golang.org/api/gensupport/buffer.go')
-rw-r--r--vendor/google.golang.org/api/gensupport/buffer.go79
1 files changed, 79 insertions, 0 deletions
diff --git a/vendor/google.golang.org/api/gensupport/buffer.go b/vendor/google.golang.org/api/gensupport/buffer.go
new file mode 100644
index 0000000..3d0817e
--- /dev/null
+++ b/vendor/google.golang.org/api/gensupport/buffer.go
@@ -0,0 +1,79 @@
1// Copyright 2016 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package gensupport
6
7import (
8 "bytes"
9 "io"
10
11 "google.golang.org/api/googleapi"
12)
13
14// MediaBuffer buffers data from an io.Reader to support uploading media in
15// retryable chunks. It should be created with NewMediaBuffer.
16type MediaBuffer struct {
17 media io.Reader
18
19 chunk []byte // The current chunk which is pending upload. The capacity is the chunk size.
20 err error // Any error generated when populating chunk by reading media.
21
22 // The absolute position of chunk in the underlying media.
23 off int64
24}
25
26// NewMediaBuffer initializes a MediaBuffer.
27func NewMediaBuffer(media io.Reader, chunkSize int) *MediaBuffer {
28 return &MediaBuffer{media: media, chunk: make([]byte, 0, chunkSize)}
29}
30
31// Chunk returns the current buffered chunk, the offset in the underlying media
32// from which the chunk is drawn, and the size of the chunk.
33// Successive calls to Chunk return the same chunk between calls to Next.
34func (mb *MediaBuffer) Chunk() (chunk io.Reader, off int64, size int, err error) {
35 // There may already be data in chunk if Next has not been called since the previous call to Chunk.
36 if mb.err == nil && len(mb.chunk) == 0 {
37 mb.err = mb.loadChunk()
38 }
39 return bytes.NewReader(mb.chunk), mb.off, len(mb.chunk), mb.err
40}
41
42// loadChunk will read from media into chunk, up to the capacity of chunk.
43func (mb *MediaBuffer) loadChunk() error {
44 bufSize := cap(mb.chunk)
45 mb.chunk = mb.chunk[:bufSize]
46
47 read := 0
48 var err error
49 for err == nil && read < bufSize {
50 var n int
51 n, err = mb.media.Read(mb.chunk[read:])
52 read += n
53 }
54 mb.chunk = mb.chunk[:read]
55 return err
56}
57
58// Next advances to the next chunk, which will be returned by the next call to Chunk.
59// Calls to Next without a corresponding prior call to Chunk will have no effect.
60func (mb *MediaBuffer) Next() {
61 mb.off += int64(len(mb.chunk))
62 mb.chunk = mb.chunk[0:0]
63}
64
65type readerTyper struct {
66 io.Reader
67 googleapi.ContentTyper
68}
69
70// ReaderAtToReader adapts a ReaderAt to be used as a Reader.
71// If ra implements googleapi.ContentTyper, then the returned reader
72// will also implement googleapi.ContentTyper, delegating to ra.
73func ReaderAtToReader(ra io.ReaderAt, size int64) io.Reader {
74 r := io.NewSectionReader(ra, 0, size)
75 if typer, ok := ra.(googleapi.ContentTyper); ok {
76 return readerTyper{r, typer}
77 }
78 return r
79}