multi_readcloser.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package wal
  14. import "io"
  15. type multiReadCloser struct {
  16. closers []io.Closer
  17. reader io.Reader
  18. }
  19. func (mc *multiReadCloser) Close() error {
  20. var err error
  21. for i := range mc.closers {
  22. err = mc.closers[i].Close()
  23. }
  24. return err
  25. }
  26. func (mc *multiReadCloser) Read(p []byte) (int, error) {
  27. return mc.reader.Read(p)
  28. }
  29. func MultiReadCloser(readClosers ...io.ReadCloser) io.ReadCloser {
  30. cs := make([]io.Closer, len(readClosers))
  31. rs := make([]io.Reader, len(readClosers))
  32. for i := range readClosers {
  33. cs[i] = readClosers[i]
  34. rs[i] = readClosers[i]
  35. }
  36. r := io.MultiReader(rs...)
  37. return &multiReadCloser{cs, r}
  38. }