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