Io.WriteSeeker and io.ReadSeeker from [] bytes or file

I have a method called DoSomething. DoSomething will operate on it with binary source data and write out binary data. DoSomething must be general enough to handle either an array of bytes [] or a file descriptor for the source and destination. To do this, I tried to declare a method as follows:

func DoSomething(source *io.ReadSeeker, destination *io.WriteSeeker)

I implemented ReadSeeker and WriteSeeker to work with buffers using my own necessary methods (if there is a way to do this automatically, I would also like to hear about it). Unfortunately, I cannot figure out how to create either the io.ReadSeeker or io.WriteSeeker file from the file descriptor. I am quite sure that a pre-prepared way should be prepared to deal with this without the need for them manually. Is it possible?

+4
source share
2 answers

The file already implements both of these files. You can do something like this:

package main

import (
   "fmt"
   "io"
   "os"
)

func main() {
   f, err := os.Open("test.txt")
   if err != nil {
     fmt.Println(err)
   }
   defer f.Close()
   f2, err := os.Create("test2.txt")
   if err != nil {
      fmt.Println(err)
   }
   defer f2.Close()
   DoSomething(f, f2) 
}

func DoSomething(source io.ReadSeeker, destination io.WriteSeeker) {
   io.Copy(destination, source)
}

In addition, you do not need to pass pointers to interfaces, which facilitates their elimination.

+8
source

-, - , . , , :

package filebuffer

import (
    "bytes"
    "errors"
)

type FileBuffer struct {
    Buffer bytes.Buffer
    Index  int64
}

func NewFileBuffer() FileBuffer {
    return FileBuffer{}
}

func (fbuffer *FileBuffer) Bytes() []byte {
    return fbuffer.Buffer.Bytes()
}

func (fbuffer *FileBuffer) Read(p []byte) (int, error) {
    n, err := bytes.NewBuffer(fbuffer.Buffer.Bytes()[fbuffer.Index:]).Read(p)

    if err == nil {
        if fbuffer.Index+int64(len(p)) < int64(fbuffer.Buffer.Len()) {
            fbuffer.Index += int64(len(p))
        } else {
            fbuffer.Index = int64(fbuffer.Buffer.Len())
        }
    }

    return n, err
}

func (fbuffer *FileBuffer) Write(p []byte) (int, error) {
    n, err := fbuffer.Buffer.Write(p)

    if err == nil {
        fbuffer.Index = int64(fbuffer.Buffer.Len())
    }

    return n, err
}

func (fbuffer *FileBuffer) Seek(offset int64, whence int) (int64, error) {
    var err error
    var Index int64 = 0

    switch whence {
    case 0:
        if offset >= int64(fbuffer.Buffer.Len()) || offset < 0 {
            err = errors.New("Invalid Offset.")
        } else {
            fbuffer.Index = offset
            Index = offset
        }
    default:
        err = errors.New("Unsupported Seek Method.")
    }

    return Index, err
}

:

destination := filebuffer.NewFileBuffer()

source, err := os.Open(pathString)
if err != nil {
    return nil, err
}
defer source.Close()

if _, err := encrypter.Decrypt(source, &destination, password); err != nil {
    return nil, err
}
+2

All Articles