github.com/torfuzx/docker@v1.8.1/pkg/ioutils/writers.go (about) 1 package ioutils 2 3 import "io" 4 5 type NopWriter struct{} 6 7 func (*NopWriter) Write(buf []byte) (int, error) { 8 return len(buf), nil 9 } 10 11 type nopWriteCloser struct { 12 io.Writer 13 } 14 15 func (w *nopWriteCloser) Close() error { return nil } 16 17 func NopWriteCloser(w io.Writer) io.WriteCloser { 18 return &nopWriteCloser{w} 19 } 20 21 type NopFlusher struct{} 22 23 func (f *NopFlusher) Flush() {} 24 25 type writeCloserWrapper struct { 26 io.Writer 27 closer func() error 28 } 29 30 func (r *writeCloserWrapper) Close() error { 31 return r.closer() 32 } 33 34 func NewWriteCloserWrapper(r io.Writer, closer func() error) io.WriteCloser { 35 return &writeCloserWrapper{ 36 Writer: r, 37 closer: closer, 38 } 39 } 40 41 // Wrap a concrete io.Writer and hold a count of the number 42 // of bytes written to the writer during a "session". 43 // This can be convenient when write return is masked 44 // (e.g., json.Encoder.Encode()) 45 type WriteCounter struct { 46 Count int64 47 Writer io.Writer 48 } 49 50 func NewWriteCounter(w io.Writer) *WriteCounter { 51 return &WriteCounter{ 52 Writer: w, 53 } 54 } 55 56 func (wc *WriteCounter) Write(p []byte) (count int, err error) { 57 count, err = wc.Writer.Write(p) 58 wc.Count += int64(count) 59 return 60 }