Documentation ¶
Overview ¶
Package bufferedfilewriter provides a writer that buffers data in memory until a threshold is exceeded at which point it switches to writing to a temporary file.
Index ¶
- type BufferedFileWriter
- func (w *BufferedFileWriter) CloseForWriting() error
- func (w *BufferedFileWriter) Len() int
- func (w *BufferedFileWriter) ReadCloser() (io.ReadCloser, error)
- func (w *BufferedFileWriter) ReadFrom(reader io.Reader) (int64, error)
- func (w *BufferedFileWriter) String() (string, error)
- func (w *BufferedFileWriter) Write(data []byte) (int, error)
- type Option
- type PoolSize
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BufferedFileWriter ¶
type BufferedFileWriter struct {
// contains filtered or unexported fields
}
BufferedFileWriter manages a buffer for writing data, flushing to a file when a threshold is exceeded. It supports either write-only or read-only mode, indicated by its state.
func New ¶
func New(opts ...Option) *BufferedFileWriter
New creates a new BufferedFileWriter with the given options.
func (*BufferedFileWriter) CloseForWriting ¶
func (w *BufferedFileWriter) CloseForWriting() error
CloseForWriting flushes any remaining data in the buffer to the file, closes the file if created, and transitions the BufferedFileWriter to read-only mode.
func (*BufferedFileWriter) Len ¶
func (w *BufferedFileWriter) Len() int
Len returns the number of bytes written to the buffer or file.
func (*BufferedFileWriter) ReadCloser ¶
func (w *BufferedFileWriter) ReadCloser() (io.ReadCloser, error)
ReadCloser returns an io.ReadCloser to read the written content. If the content is stored in a file, it opens the file and returns a file reader. If the content is stored in memory, it returns a custom reader that handles returning the buffer to the pool. The caller should call Close() on the returned io.Reader when done to ensure resources are properly released. This method can only be used when the BufferedFileWriter is in read-only mode.
func (*BufferedFileWriter) ReadFrom ¶ added in v3.75.0
func (w *BufferedFileWriter) ReadFrom(reader io.Reader) (int64, error)
ReadFrom reads data from the provided reader and writes it to the buffer or file, depending on the size. This method satisfies the io.ReaderFrom interface, allowing it to be used with standard library functions like io.Copy and io.CopyBuffer.
By implementing this method, BufferedFileWriter can leverage optimized data transfer mechanisms provided by the standard library. For example, when using io.Copy with a BufferedFileWriter, the copy operation will be delegated to the ReadFrom method, avoiding the potentially non-optimized default approach.
This is particularly useful when creating a new BufferedFileWriter from an io.Reader using the NewFromReader function. By leveraging the ReadFrom method, data can be efficiently transferred from the reader to the BufferedFileWriter.
func (*BufferedFileWriter) String ¶
func (w *BufferedFileWriter) String() (string, error)
String returns all the data written to the buffer or file as a string or an empty string if there is an error.
type Option ¶
type Option func(*BufferedFileWriter)
Option is a function that modifies a BufferedFileWriter.
func WithBufferSize ¶ added in v3.76.3
WithBufferSize sets the buffer size for the BufferedFileWriter.
func WithThreshold ¶
WithThreshold sets the threshold for switching to file writing.