github.com/karrick/gorill@v1.10.3/nopClose.go (about) 1 package gorill 2 3 import ( 4 "bytes" 5 "io" 6 ) 7 8 // NewNopCloseBuffer returns a structure that wraps bytes.Buffer with a no-op Close method. It can 9 // be used in tests that need a bytes.Buffer, but need to provide a Close method. 10 // 11 // bb := gorill.NopCloseBuffer() 12 // bb.Write([]byte("example")) 13 // bb.Close() // does nothing 14 func NewNopCloseBuffer() *NopCloseBuffer { 15 return &NopCloseBuffer{Buffer: new(bytes.Buffer), closed: false} 16 } 17 18 // NewNopCloseBufferSize returns a structure that wraps bytes.Buffer with a no-op Close method, 19 // using a specified buffer size. It can be used in tests that need a bytes.Buffer, but need to 20 // provide a Close method. 21 // 22 // bb := gorill.NopCloseBufferSize(8192) 23 // bb.Write([]byte("example")) 24 // bb.Close() // does nothing 25 func NewNopCloseBufferSize(size int) *NopCloseBuffer { 26 return &NopCloseBuffer{Buffer: bytes.NewBuffer(make([]byte, 0, size)), closed: false} 27 } 28 29 // NopCloseBuffer is a structure that wraps a buffer, but also provides a no-op Close method. 30 type NopCloseBuffer struct { 31 *bytes.Buffer 32 closed bool 33 } 34 35 // Close returns nil error. 36 func (m *NopCloseBuffer) Close() error { m.closed = true; return nil } 37 38 // IsClosed returns false, unless NopCloseBuffer's Close method has been invoked 39 func (m *NopCloseBuffer) IsClosed() bool { return m.closed } 40 41 // NopCloseReader returns a structure that implements io.ReadCloser, but provides a no-op Close 42 // method. It is useful when you have an io.Reader that you must pass to a method that requires an 43 // io.ReadCloser. It is the same as ioutil.NopCloser, but for provided here for symmetry with 44 // NopCloseWriter. 45 // 46 // iorc := gorill.NopCloseReader(ior) 47 // iorc.Close() // does nothing 48 func NopCloseReader(ior io.Reader) io.ReadCloser { return nopCloseReader{ior} } 49 50 type nopCloseReader struct{ io.Reader } 51 52 func (nopCloseReader) Close() error { return nil } 53 54 // NopCloseWriter returns a structure that implements io.WriteCloser, but provides a no-op Close 55 // method. It is useful when you have an io.Writer that you must pass to a method that requires an 56 // io.WriteCloser. It is the counter-part to ioutil.NopCloser, but for io.Writer. 57 // 58 // iowc := gorill.NopCloseWriter(iow) 59 // iowc.Close() // does nothing 60 func NopCloseWriter(iow io.Writer) io.WriteCloser { return nopCloseWriter{iow} } 61 62 func (nopCloseWriter) Close() error { return nil } 63 64 type nopCloseWriter struct{ io.Writer }