"Fossies" - the Fresh Open Source Software Archive

Member "gdrive-2.1.1/vendor/google.golang.org/api/gensupport/buffer.go" (28 May 2021, 2405 Bytes) of package /linux/misc/gdrive-2.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Go source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    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 
    5 package gensupport
    6 
    7 import (
    8     "bytes"
    9     "io"
   10 
   11     "google.golang.org/api/googleapi"
   12 )
   13 
   14 // ResumableBuffer buffers data from an io.Reader to support uploading media in retryable chunks.
   15 type ResumableBuffer struct {
   16     media io.Reader
   17 
   18     chunk []byte // The current chunk which is pending upload.  The capacity is the chunk size.
   19     err   error  // Any error generated when populating chunk by reading media.
   20 
   21     // The absolute position of chunk in the underlying media.
   22     off int64
   23 }
   24 
   25 func NewResumableBuffer(media io.Reader, chunkSize int) *ResumableBuffer {
   26     return &ResumableBuffer{media: media, chunk: make([]byte, 0, chunkSize)}
   27 }
   28 
   29 // Chunk returns the current buffered chunk, the offset in the underlying media
   30 // from which the chunk is drawn, and the size of the chunk.
   31 // Successive calls to Chunk return the same chunk between calls to Next.
   32 func (rb *ResumableBuffer) Chunk() (chunk io.Reader, off int64, size int, err error) {
   33     // There may already be data in chunk if Next has not been called since the previous call to Chunk.
   34     if rb.err == nil && len(rb.chunk) == 0 {
   35         rb.err = rb.loadChunk()
   36     }
   37     return bytes.NewReader(rb.chunk), rb.off, len(rb.chunk), rb.err
   38 }
   39 
   40 // loadChunk will read from media into chunk, up to the capacity of chunk.
   41 func (rb *ResumableBuffer) loadChunk() error {
   42     bufSize := cap(rb.chunk)
   43     rb.chunk = rb.chunk[:bufSize]
   44 
   45     read := 0
   46     var err error
   47     for err == nil && read < bufSize {
   48         var n int
   49         n, err = rb.media.Read(rb.chunk[read:])
   50         read += n
   51     }
   52     rb.chunk = rb.chunk[:read]
   53     return err
   54 }
   55 
   56 // Next advances to the next chunk, which will be returned by the next call to Chunk.
   57 // Calls to Next without a corresponding prior call to Chunk will have no effect.
   58 func (rb *ResumableBuffer) Next() {
   59     rb.off += int64(len(rb.chunk))
   60     rb.chunk = rb.chunk[0:0]
   61 }
   62 
   63 type readerTyper struct {
   64     io.Reader
   65     googleapi.ContentTyper
   66 }
   67 
   68 // ReaderAtToReader adapts a ReaderAt to be used as a Reader.
   69 // If ra implements googleapi.ContentTyper, then the returned reader
   70 // will also implement googleapi.ContentTyper, delegating to ra.
   71 func ReaderAtToReader(ra io.ReaderAt, size int64) io.Reader {
   72     r := io.NewSectionReader(ra, 0, size)
   73     if typer, ok := ra.(googleapi.ContentTyper); ok {
   74         return readerTyper{r, typer}
   75     }
   76     return r
   77 }