mfr

package module
v0.0.0-...-0e553ff Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2024 License: MIT Imports: 3 Imported by: 0

README

MultiFileReader

MultiFileReader is a Go package that provides a way to read the contents of multiple files sequentially as if they were a single file. It manages a set of file paths and allows seeking to specific positions within the combined file.

Thanks to @zhayes for helping me debug this.

Features

Reads multiple files sequentially as a single file Supports seeking to specific positions within the combined file Implements io.Reader, io.Seeker, and io.Closer interfaces Compatible with fs.FS file systems Installation To install the package, use the following command:

go get github.com/your/package/path/mfr

Usage

Here's an example of how to use the MultiFileReader:

package main

import (
    "fmt"
    "io"
    "os"

    "github.com/your/package/path/mfr"
)

func main() {
    // Create a new MultiFileReader
    dir := os.DirFS("path/to/directory")
    files := []string{"file1.txt", "file2.txt", "file3.txt"}
    reader := mfr.NewMFR(dir, files)

    // Read the contents of the files
    buffer := make([]byte, 1024)
    for {
        n, err := reader.Read(buffer)
        if err == io.EOF {
            break
        }
        if err != nil {
            fmt.Println("Error:", err)
            return
        }
        fmt.Print(string(buffer[:n]))
    }

    // Seek to a specific position
    offset := int64(100)
    newOffset, err := reader.Seek(offset, io.SeekStart)
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
    fmt.Println("New offset:", newOffset)

    // Close the reader
    err = reader.Close()
    if err != nil {
        fmt.Println("Error:", err)
        return
    }
}

API

NewMFR(dir fs.FS, files []string) *MultiFileReader

Creates a new MultiFileReader from the given directory and file paths. The dir parameter should be an fs.FS file system, and the files parameter should be a slice of file paths relative to the directory.

The order of files passed in signify the order in which the files will be read.

The MFR implements the io.Reader, io.Seeker, and io.Closer interfaces.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MultiFileReader

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

MultiFileReader is a struct that manages a set of entries (file paths) and provides a way to read the contents of those entries sequentially.

func New

func New(dir fs.FS, files []string) *MultiFileReader

New creates a new MultiFileReader from the given directory and entries. fs.FS Opened files must be seekable.

func (*MultiFileReader) Close

func (mfr *MultiFileReader) Close() error

func (*MultiFileReader) Read

func (mfr *MultiFileReader) Read(p []byte) (int, error)

func (*MultiFileReader) Seek

func (mfr *MultiFileReader) Seek(offset int64, whence int) (int64, error)

Jump to

Keyboard shortcuts

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