2014-08-12 12:10:43 -04:00
|
|
|
package ioutils
|
|
|
|
|
|
|
|
import "io"
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NopWriter represents a type which write operation is nop.
|
2014-08-12 12:10:43 -04:00
|
|
|
type NopWriter struct{}
|
|
|
|
|
|
|
|
func (*NopWriter) Write(buf []byte) (int, error) {
|
|
|
|
return len(buf), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type nopWriteCloser struct {
|
|
|
|
io.Writer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *nopWriteCloser) Close() error { return nil }
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NopWriteCloser returns a nopWriteCloser.
|
2014-08-12 12:10:43 -04:00
|
|
|
func NopWriteCloser(w io.Writer) io.WriteCloser {
|
|
|
|
return &nopWriteCloser{w}
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NopFlusher represents a type which flush opetatin is nop.
|
2014-08-12 12:10:43 -04:00
|
|
|
type NopFlusher struct{}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// Flush is a nop operation.
|
2014-08-12 12:10:43 -04:00
|
|
|
func (f *NopFlusher) Flush() {}
|
2014-08-12 12:20:59 -04:00
|
|
|
|
|
|
|
type writeCloserWrapper struct {
|
|
|
|
io.Writer
|
|
|
|
closer func() error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *writeCloserWrapper) Close() error {
|
|
|
|
return r.closer()
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewWriteCloserWrapper returns a new io.WriteCloser.
|
2014-08-12 12:20:59 -04:00
|
|
|
func NewWriteCloserWrapper(r io.Writer, closer func() error) io.WriteCloser {
|
|
|
|
return &writeCloserWrapper{
|
|
|
|
Writer: r,
|
|
|
|
closer: closer,
|
|
|
|
}
|
|
|
|
}
|
2015-03-29 17:17:23 -04:00
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// WriteCounter wraps a concrete io.Writer and hold a count of the number
|
2015-03-29 17:17:23 -04:00
|
|
|
// of bytes written to the writer during a "session".
|
|
|
|
// This can be convenient when write return is masked
|
|
|
|
// (e.g., json.Encoder.Encode())
|
|
|
|
type WriteCounter struct {
|
|
|
|
Count int64
|
|
|
|
Writer io.Writer
|
|
|
|
}
|
|
|
|
|
2015-08-02 21:45:05 -04:00
|
|
|
// NewWriteCounter returns a new WriteCounter.
|
2015-03-29 17:17:23 -04:00
|
|
|
func NewWriteCounter(w io.Writer) *WriteCounter {
|
|
|
|
return &WriteCounter{
|
|
|
|
Writer: w,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wc *WriteCounter) Write(p []byte) (count int, err error) {
|
|
|
|
count, err = wc.Writer.Write(p)
|
|
|
|
wc.Count += int64(count)
|
|
|
|
return
|
|
|
|
}
|