How can I push bytes into a reader in Go?

huangapple go评论82阅读模式
英文:

How can I push bytes into a reader in Go?

问题

我需要将接收到的字节逐步追加到一个缓冲区中,以便以后可以再次读取。类似这样的代码:

type TwoPassReader struct {
  someDataSource SomeDataReader
  secondPassReader io.Reader
}

func (r *TwoPassReader) Read(b []byte) (n int, err error) {
  n, err := r.someDataSource.Read(b)
  if err == nil {
    //XXX: 这个实际上不存在,对吗?
    bytesAppended, writeErr := r.secondPassReader.Write(b)
  }
  return n, err
}

func (r *) ReadSecondPass(b []byte) (n int, err error) {
  return r.secondPassReader.Read(b)
}

虽然最简单的解决方案是使用bufio.Reader.Peek,但我有一个无法修改的SomeDataReader对象,而不是一个Reader。将数据放入[]byte并在调用ReadSecondPass时返回,是最好的解决方案吗?

英文:

I need to incrementally append bytes as I receive them to a buffer where I can read them again later. Something like this:

type TwoPassReader struct {
  someDataSource SomeDataReader
  secondPassReader io.Reader
}

func (r *TwoPassReader) Read(b []byte) (n int, err error) {
  n, err := r.someDataSource.Read(b)
  if err == nil {
    //XXX: this doesn't actually exist, right?
    bytesAppended, writeErr := r.secondPassReader.Write(b)
  }
  return n, err
}

func (r *) ReadSecondPass(b []byte) (n int, err error) {
  return r.secondPassReader.Read(b)
}

While the trivial answer for this would be to use bufio.Reader.Peek, I have a SomeDataReader object that I can't modify instead of a Reader. Is the best solution to just put the data into a []byte and return that when ReadSecondPass is called?

答案1

得分: 2

像这样吗?

package main

import (
	"bytes"
	"io"
)

type TwoPassReader struct {
	firstPassReader  io.Reader
	secondPassBuffer io.ReadWriter
}

func NewTwoPassReader(firstPass io.Reader) *TwoPassReader {
	return &TwoPassReader{
		firstPassReader:  firstPass,
		secondPassBuffer: bytes.NewBuffer(nil),
	}
}

func (r *TwoPassReader) Read(b []byte) (n int, err error) {
	n1, err1 := r.firstPassReader.Read(b)
	n2, err2 := r.secondPassBuffer.Write(b[:n1])
	if err2 != nil {
		return n2, err2
	}
	return n1, err1
}

func (r *TwoPassReader) ReadSecondPass(b []byte) (n int, err error) {
	return r.secondPassBuffer.Read(b)
}

func main() {
}

这是一个示例的Go语言代码。它定义了一个名为TwoPassReader的结构体,该结构体具有两个字段:firstPassReadersecondPassBufferTwoPassReader结构体还有两个方法:ReadReadSecondPass,用于读取数据。

NewTwoPassReader函数用于创建一个TwoPassReader对象,并初始化其字段。

main函数中,你可以添加你的代码逻辑。

英文:

Something like this?

package main

import (
	"bytes"
	"io"
)

type TwoPassReader struct {
	firstPassReader  io.Reader
	secondPassBuffer io.ReadWriter
}

func NewTwoPassReader(firstPass io.Reader) *TwoPassReader {
	return &TwoPassReader{
		firstPassReader:  firstPass,
		secondPassBuffer: bytes.NewBuffer(nil),
	}
}

func (r *TwoPassReader) Read(b []byte) (n int, err error) {
	n1, err1 := r.firstPassReader.Read(b)
	n2, err2 := r.secondPassBuffer.Write(b[:n1])
	if err2 != nil {
		return n2, err2
	}
	return n1, err1
}

func (r *TwoPassReader) ReadSecondPass(b []byte) (n int, err error) {
	return r.secondPassBuffer.Read(b)
}

func main() {
}

答案2

得分: 1

io.Reader只是一个接口。我们无法看到你将什么赋值给该结构字段(如果有的话),但是在这里使用bytes.Buffer可能是正确的类型。

英文:

io.Reader is just an interface. We can't see what you're assigning to that struct field (if anything), but a bytes.Buffer would probably be the correct type to use here.

huangapple
  • 本文由 发表于 2013年12月19日 03:17:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/20666594.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定