memio

package module
v0.0.0-...-c69e2d1 Latest Latest
Warning

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

Go to latest
Published: Feb 2, 2024 License: MIT Imports: 10 Imported by: 5

README

Intro

Package memio implements common types and functions used for in-memory I/O.

Install

go get github.com/amitybell/memio

Usage

    package main

    import (
        "github.com/amitybell/memio"
        "io"
        "fmt"
    )

    func main() {
        // since File is purely in-memory, as long as all inputs are valid (e.g. Seek offsets/whence),
        // all method calls will succeed.
        f := &memio.File{}
        f.WriteString("hello world")
        // reset back to the begining of the file to begin reading
        f.Seek(0, 0)
        s, _ := io.ReadAll(f)
        fmt.Printf("%s\n", s)
    }

Documentation

Overview

Package memio implements common types and functions used for in-memory I/O

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	// contains filtered or unexported fields
}

File implements file-like methods on an in-memory buffer

func NewFile

func NewFile(s []byte) *File

NewFile returns a new File instance with the internal buffer set to s

func (*File) Bytes

func (f *File) Bytes() []byte

Bytes returns the internal buffer

func (*File) Close

func (f *File) Close() error

Close implements the fs.File.Close interface

It always returns nil

func (*File) Expand

func (f *File) Expand(n int) []byte

Expand grows the internal buffer to fill n bytes and sets pos to the end

It returns a slice that should be filled with n bytes of content

func (*File) Grow

func (f *File) Grow(n int) *File

Grow increases the capacity of the internal buffer to guarantee space for another n byte without reallocation

func (*File) IsDir

func (f *File) IsDir() bool

IsDir implements the fs.FileInfo.IsDir interface

It always returns false.

func (*File) Len

func (f *File) Len() int

Len returns the length of the internal buffer

func (*File) ModTime

func (f *File) ModTime() time.Time

ModTime implements the fs.FileInfo.ModTime interface

It always returns time.Time{}

func (*File) Mode

func (f *File) Mode() fs.FileMode

Mode implements the fs.FileInfo.Mode interface

It always returns fs.ModeIrregular

func (*File) Name

func (f *File) Name() string

Name implements the fs.FileInfo.Name interface

It always returns ""

func (*File) Offset

func (f *File) Offset() int64

Offset returns the current read/write position of the internal buffer

func (*File) Read

func (f *File) Read(p []byte) (int, error)

Read implements io.Reader

func (*File) ReadByte

func (f *File) ReadByte() (byte, error)

ReadByte implements io.ByteReader

func (*File) ReadBytes

func (f *File) ReadBytes(delim byte) ([]byte, error)

ReadBytes reads bytes up to and excluding delim An error (wrapping io.ErrUnexpectedEOF) is returned iff delim is not found

func (*File) ReadFloat32

func (f *File) ReadFloat32(o binary.ByteOrder) (float32, error)

ReadFloat32 reads a 32-bit floating point number in the byte order specified by o

func (*File) ReadFloat64

func (f *File) ReadFloat64(o binary.ByteOrder) (float64, error)

ReadFloat64 reads a 64-bit floating point number in the byte order specified by o

func (*File) ReadFrom

func (f *File) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom implements io.ReaderFrom

func (*File) ReadFull

func (f *File) ReadFull(p []byte) (int, error)

ReadFull fills buffer p, or returns the number of bytes read and error io.ErrUnexpectedEOF

func (*File) ReadInt16

func (f *File) ReadInt16(o binary.ByteOrder) (int16, error)

ReadInt16 is a wrapper around int16(ReadUint16)

func (*File) ReadInt32

func (f *File) ReadInt32(o binary.ByteOrder) (int32, error)

ReadInt32 is a wrapper around int32(ReadUint32)

func (*File) ReadInt64

func (f *File) ReadInt64(o binary.ByteOrder) (int64, error)

ReadInt64 is a wrapper around int64(ReadUint64)

func (*File) ReadString

func (f *File) ReadString(delim byte) (string, error)

ReadString reads bytes up to and excluding delim An error (wrapping io.ErrUnexpectedEOF) is returned iff delim is not found

func (*File) ReadUint16

func (f *File) ReadUint16(o binary.ByteOrder) (uint16, error)

ReadUint16 reads a 16-bit number in the byte order specified by o

func (*File) ReadUint32

func (f *File) ReadUint32(o binary.ByteOrder) (uint32, error)

ReadUint32 reads a 32-bit number in the byte order specified by o

func (*File) ReadUint64

func (f *File) ReadUint64(o binary.ByteOrder) (uint64, error)

ReadUint64 reads a 64-bit number in the byte order specified by o

func (*File) Reset

func (f *File) Reset(p []byte)

Reset sets the internal buffer to p and the internal offset to 0

func (*File) Rewind

func (f *File) Rewind() *File

Rewind sets the Read position of the internal buffer back to the start

It's equivalent to Seek(0, io.SeekStart) or Seek(0, 0)

func (*File) Seek

func (f *File) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker

If the final offset is greater than Len(), the internal buffer is expanded accordingly

func (*File) Size

func (f *File) Size() int64

Size implements the fs.FileInfo.Size interface

It returns the length of the internal buffer

func (*File) Stat

func (f *File) Stat() (fs.FileInfo, error)

Stat implements the fs.File.Stat interface

It always returns itself

func (*File) String

func (f *File) String() string

String returns a copy of the internal buffer as a string

func (*File) StringRef

func (f *File) StringRef() string

StringRef returns a reference to the internal buffer as a string

func (*File) Sys

func (f *File) Sys() any

Sys implements the fs.FileInfo.Sys interface

It always returns nil

func (*File) Write

func (f *File) Write(p []byte) (int, error)

Seek implements io.Writer

func (*File) WriteByte

func (f *File) WriteByte(p byte) error

WriteByte implements io.ByteWriter

func (*File) WriteFloat32

func (f *File) WriteFloat32(o binary.ByteOrder, n float32)

WriteFloat32 is a wrapper around f.WriteUint32(o, math.Float32bits(n))

func (*File) WriteFloat64

func (f *File) WriteFloat64(o binary.ByteOrder, n float64)

WriteFloat64 is a wrapper around f.WriteUint64(o, math.Float64bits(n))

func (*File) WriteInt16

func (f *File) WriteInt16(o binary.ByteOrder, n int16)

WriteInt16 is a wrapper around f.WriteUint16(o, uint16(n))

func (*File) WriteInt32

func (f *File) WriteInt32(o binary.ByteOrder, n int32)

WriteInt32 is a wrapper around f.WriteUint32(o, uint32(n))

func (*File) WriteInt64

func (f *File) WriteInt64(o binary.ByteOrder, n int64)

WriteInt64 is a wrapper around f.WriteUint64(o, uint64(n))

func (*File) WriteString

func (f *File) WriteString(p string) (int, error)

WriteString implements io.StringWriter

func (*File) WriteTo

func (f *File) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo

func (*File) WriteUint16

func (f *File) WriteUint16(o binary.ByteOrder, n uint16)

WriteUint16 writes n in the byte order specified by o

func (*File) WriteUint32

func (f *File) WriteUint32(o binary.ByteOrder, n uint32)

WriteUint32 writes n in the byte order specified by o

func (*File) WriteUint64

func (f *File) WriteUint64(o binary.ByteOrder, n uint64)

WriteUint64 writes n in the byte order specified by o

Jump to

Keyboard shortcuts

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