Documentation
¶
Overview ¶
Package bufwrite provides buffered io.Writer with sync and close methods.
Index ¶
- type BufIOWriter
- type LineWriter
- func (b *LineWriter) Available() int
- func (b *LineWriter) Buffered() int
- func (b *LineWriter) Close() error
- func (b *LineWriter) Flush() error
- func (b *LineWriter) Reset(w io.Writer)
- func (b *LineWriter) Size() int
- func (b *LineWriter) Sync() error
- func (b *LineWriter) Write(p []byte) (nn int, err error)
- func (b *LineWriter) WriteString(s string) (int, error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufIOWriter ¶
BufIOWriter wrap the bufio.Writer, implements the Sync() Close() methods
func NewBufIOWriterSize ¶
func NewBufIOWriterSize(w io.Writer, size int) *BufIOWriter
NewBufIOWriterSize instance with size
type LineWriter ¶
type LineWriter struct {
// contains filtered or unexported fields
}
LineWriter is a buffered io.Writer that keeps every single Write intact.
Adapted from bufio.Writer. The key difference: when the incoming data does not fit in the remaining buffer, bufio.Writer fills the buffer, flushes, and continues with the rest — so one Write call can be split across two flushes. For logging that means an external collector reading the underlying writer could observe a half-written line (e.g. a truncated JSON record).
LineWriter instead guarantees each Write(p) is delivered to the underlying writer as a whole: p is either fully buffered, or (if it doesn't fit) the buffer is flushed first and then p is written in a single underlying Write. So a complete log line/record always reaches the destination unsplit.
NOTE:
- "Line" means "one logical line/record per Write call". It does NOT flush on the '\n' byte and is not line-buffered in the C-stdio sense.
- It is NOT safe for concurrent use; guard Write/Flush with an external lock.
- On a write error, no more data is accepted and all subsequent Write and Flush calls return that error. Call Flush (or Close) when done to ensure buffered data is forwarded to the underlying io.Writer.
func NewLineWriter ¶
func NewLineWriter(w io.Writer) *LineWriter
NewLineWriter returns a new LineWriter whose buffer has the default size.
func NewLineWriterSize ¶
func NewLineWriterSize(w io.Writer, size int) *LineWriter
NewLineWriterSize returns a new LineWriter whose buffer has at least the specified size. If the argument io.Writer is already a LineWriter with large enough size, it returns the underlying LineWriter.
func (*LineWriter) Available ¶
func (b *LineWriter) Available() int
Available returns how many bytes are unused in the buffer.
func (*LineWriter) Buffered ¶
func (b *LineWriter) Buffered() int
Buffered returns the number of bytes that have been written into the current buffer.
func (*LineWriter) Flush ¶
func (b *LineWriter) Flush() error
Flush writes any buffered data to the underlying io.Writer.
TIP: please add lock before calling the method.
func (*LineWriter) Reset ¶
func (b *LineWriter) Reset(w io.Writer)
Reset discards any unflushed buffered data, clears any error, and resets b to write its output to w.
func (*LineWriter) Size ¶
func (b *LineWriter) Size() int
Size returns the size of the underlying buffer in bytes.
func (*LineWriter) Write ¶
func (b *LineWriter) Write(p []byte) (nn int, err error)
Write writes the contents of p into the buffer. It returns the number of bytes written. If nn < len(p), it also returns an error explaining why the writing is short.
func (*LineWriter) WriteString ¶
func (b *LineWriter) WriteString(s string) (int, error)
WriteString to the writer