...

Source file src/github.com/aws/smithy-go/testing/reader.go

Documentation: github.com/aws/smithy-go/testing

     1  package testing
     2  
     3  import (
     4  	"io"
     5  	"sync"
     6  )
     7  
     8  // ByteLoop provides an io.ReadCloser that always fills the read byte slice
     9  // with repeating value, until closed.
    10  type ByteLoop struct {
    11  	value  byte
    12  	closed bool
    13  	mu     sync.RWMutex
    14  }
    15  
    16  // Read populates the passed in byte slice with the value the ByteLoop was
    17  // created with. Returns the size of bytes written. If the reader is closed,
    18  // io.EOF will be returned.
    19  func (l *ByteLoop) Read(p []byte) (size int, err error) {
    20  	l.mu.RLock()
    21  	defer l.mu.RUnlock()
    22  	if l.closed {
    23  		return 0, io.EOF
    24  	}
    25  
    26  	for i := 0; i < len(p); i++ {
    27  		p[i] = l.value
    28  	}
    29  	return len(p), nil
    30  }
    31  
    32  // Close closes the ByteLoop, and prevents any further reading.
    33  func (l *ByteLoop) Close() error {
    34  	l.mu.Lock()
    35  	defer l.mu.Unlock()
    36  	l.closed = true
    37  
    38  	return nil
    39  }
    40  

View as plain text