bufferedfilewriter

package
v3.78.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 4, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

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

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 NewFromReader added in v3.75.0

func NewFromReader(r io.Reader, opts ...Option) (*BufferedFileWriter, error)

NewFromReader creates a new instance of BufferedFileWriter and writes the content from the provided reader to the writer.

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) ReadSeekCloser added in v3.75.0

func (w *BufferedFileWriter) ReadSeekCloser() (io.ReadSeekCloser, error)

ReadSeekCloser returns an io.ReadSeekCloser 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 allows seeking and handles returning the buffer to the pool. This method can only be used when the BufferedFileWriter is in read-only mode.

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.

func (*BufferedFileWriter) Write

func (w *BufferedFileWriter) Write(data []byte) (int, error)

Write writes data to the buffer or a file, depending on the size.

type Option

type Option func(*BufferedFileWriter)

Option is a function that modifies a BufferedFileWriter.

func WithBufferSize added in v3.76.3

func WithBufferSize(size PoolSize) Option

WithBufferSize sets the buffer size for the BufferedFileWriter.

func WithThreshold

func WithThreshold(threshold uint64) Option

WithThreshold sets the threshold for switching to file writing.

type PoolSize added in v3.76.3

type PoolSize int
const (
	Default PoolSize = iota
	Large
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL