Documentation
¶
Index ¶
- Variables
- func WithFileAppend(fil *File)
- func WithFileFlag(flag int) func(*File)
- func WithFileName(name string) func(*File)
- func WithFileOffset(off int) func(*File)
- type File
- func (fil *File) Cap() int
- func (fil *File) Close() error
- func (fil *File) Grow(n int)
- func (fil *File) Len() int
- func (fil *File) Offset() int
- func (fil *File) Read(p []byte) (int, error)
- func (fil *File) ReadAt(p []byte, off int64) (int, error)
- func (fil *File) ReadByte() (byte, error)
- func (fil *File) ReadFrom(r io.Reader) (int64, error)
- func (fil *File) Release() []byte
- func (fil *File) Seek(offset int64, whence int) (int64, error)
- func (fil *File) SeekEnd() int64
- func (fil *File) SeekStart() int64
- func (fil *File) Stat() (fs.FileInfo, error)
- func (fil *File) String() string
- func (fil *File) Truncate(size int64) error
- func (fil *File) Write(p []byte) (n int, err error)
- func (fil *File) WriteAt(p []byte, off int64) (n int, err error)
- func (fil *File) WriteByte(c byte) error
- func (fil *File) WriteString(s string) (int, error)
- func (fil *File) WriteTo(w io.Writer) (int64, error)
- type FileInfo
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrOutOfBounds = errors.New("offset out of bounds")
ErrOutOfBounds is returned for invalid offsets.
Functions ¶
func WithFileAppend ¶
func WithFileAppend(fil *File)
WithFileAppend is a File constructor function option setting the offset to the end of the file. This option must be the last option on the option list.
When File.Truncate is used, the offset will be set to the end of the File.
func WithFileFlag ¶
WithFileFlag is a File constructor function option setting flags. Flags are the same as for os.OpenFile.
Currently only os.O_APPEND flag is supported.
func WithFileName ¶
WithFileName is a File constructor function option setting file name.
func WithFileOffset ¶
WithFileOffset is a File constructor function option setting the offset.
Types ¶
type File ¶
type File struct {
// contains filtered or unexported fields
}
A File is a variable-sized buffer of bytes. Its zero value is an empty buffer ready to use.
Example ¶
package main
import (
"fmt"
"io"
"github.com/ctx42/testing/pkg/kit/memfs"
)
func main() {
buf := &memfs.File{}
_, _ = buf.Write([]byte{0, 1, 2, 3})
_, _ = buf.Seek(-2, io.SeekEnd)
_, _ = buf.Write([]byte{4, 5})
_, _ = buf.Seek(0, io.SeekStart)
data, _ := io.ReadAll(buf)
fmt.Println(data)
}
Output: [0 1 4 5]
func FileWith ¶
FileWith creates a new instance of File initialized with content. The created instance takes ownership of the content slice, and the caller must not use it after passing it to this function. FileWith is intended to prepare the File instance to read existing data. It can also be used to set the initial size of the internal buffer for writing. To do that, the content slice should have the desired capacity but a length of zero. It will panic with ErrOutOfBounds if the WithFileOffset option sets the offset to a negative number or beyond sata slice length.
func NewFile ¶
NewFile returns a new instance of File. The difference between using this function and using the zero value is that this function will initialize the buffer with capacity of bytes.MinRead. It will panic with ErrOutOfBounds if the WithFileOffset option sets offset to a negative number or greater than bytes.MinRead.
func (*File) Cap ¶
Cap returns the buffer capacity, that is, the total space allocated for the buffer's data.
func (*File) Close ¶
Close sets offset to zero and zero put the buffer. It always returns nil error.
func (*File) Grow ¶
Grow grows the buffer's capacity, if necessary, to guarantee space for another n bytes. After Grow(n), at least n bytes can be written to the buffer without another allocation. If n is negative, Grow will panic. If the buffer can't grow, it will panic with bytes.ErrTooLarge.
func (*File) Read ¶
Read reads the next len(p) bytes from the buffer at the current offset or until the buffer is drained. The return value is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil.
func (*File) ReadAt ¶
ReadAt reads len(p) bytes from the buffer at the current offset. It returns the number of bytes read and the error, if any. ReadAt always returns a non-nil error when n < len(p). It does not change the offset.
func (*File) ReadByte ¶
ReadByte reads and returns the next byte from the buffer at the current offset or returns an error. If ReadByte returns an error, no input byte was consumed, and the returned byte value is undefined.
func (*File) ReadFrom ¶
ReadFrom reads data from r until EOF and appends it to the buffer at the current offset, growing the buffer as needed. The return value is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with bytes.ErrTooLarge.
func (*File) Release ¶
Release releases ownership of the underlying buffer, the caller should not use this instance after this call.
func (*File) Seek ¶
Seek sets the offset for the next Read or Write on the buffer to the offset, interpreted according to whence: 0 means relative to the origin of the file, 1 means relative to the current offset, and 2 means relative to the end. It returns the new offset and an error (only if calculated offset < 0). Returns non-nil error of type fs.PathError where fs.PathError.Path field is an empty string unless WithFileName option was used during instance creation.
func (*File) SeekEnd ¶
SeekEnd is a convenience method setting the buffer's offset to the buffer length and returning the value it had before the method was called.
func (*File) SeekStart ¶
SeekStart is a convenience method setting the buffer's offset to zero and returning the value it had before the method was called.
func (*File) Stat ¶
Stat returns information about the in-memory file where the name is empty (unless, WithFileName option was used), the size is the length of the underlying buffer, mode is always 0444, modification time is always zero value time and fs.FileInfo.Sys always returns nil.
func (*File) String ¶
String returns string representation of the buffer starting at the current offset. Calling this method is considered as reading the buffer and advances offset to the end of the buffer.
func (*File) Truncate ¶
Truncate changes the size of the buffer discarding bytes at the offsets greater than size. It does not change the offset unless a WithFileAppend option was used, then it sets the offset to the end of the buffer. Returns an error only when size is negative. The error is of type fs.PathError where fs.PathError.Path field is an empty string unless WithFileName option was used during instance creation.
func (*File) Write ¶
Write writes the contents of p to the underlying buffer at the current offset, growing the buffer as needed. The return value n is the length of p; err is always nil.
func (*File) WriteAt ¶
WriteAt writes len(p) bytes to the underlying buffer starting at the current offset. It returns the number of bytes written; err is always nil. It does not change the offset.
func (*File) WriteString ¶
WriteString writes string s to the buffer at the current offset.
type FileInfo ¶
type FileInfo struct {
// contains filtered or unexported fields
}
FileInfo implements fs.FileInfo interface.