reader.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2016 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package storage
  15. import (
  16. "io"
  17. )
  18. // Reader reads a Cloud Storage object.
  19. type Reader struct {
  20. body io.ReadCloser
  21. remain, size int64
  22. contentType string
  23. }
  24. func (r *Reader) Close() error {
  25. return r.body.Close()
  26. }
  27. func (r *Reader) Read(p []byte) (int, error) {
  28. n, err := r.body.Read(p)
  29. r.remain -= int64(n)
  30. return n, err
  31. }
  32. // Size returns the size of the object in bytes.
  33. // The returned value is always the same and is not affected by
  34. // calls to Read or Close.
  35. func (r *Reader) Size() int64 {
  36. return r.size
  37. }
  38. // Remain returns the number of bytes left to read.
  39. func (r *Reader) Remain() int64 {
  40. return r.remain
  41. }
  42. // ContentType returns the content type of the object.
  43. func (r *Reader) ContentType() string {
  44. return r.contentType
  45. }