doutan5844 2018-09-06 13:45
浏览 63
已采纳

GoLang链接io.Reader

I'm trying to implement a proxy pattern to chain transformations on io.Reader, in order to handle chunk of bytes efficiently.

  1. We cannot use pointers on receivers, so my solution seem not very efficient

  2. The code below say "process take too long"

Complete example at: https://play.golang.org/p/KhM0VXLq4CO

b := bytes.NewBufferString(text)
t := transformReaderHandler(*b)
readByChunk(t)

type transformReaderHandler bytes.Buffer

func (t transformReaderHandler) Read(p []byte) (n int, err error) {
    n, err = (*bytes.Buffer)(&t).Read(p)
    //if n > 0 {
    //  Do Something on the chunk
    //}
    return
}

Do you have any more efficient (memory efficient, computationally efficient) solution ?

Why do this code is not working ?

EDIT: The implementation of @svsd solution : https://play.golang.org/p/VUpJcyKLB6D

package main

import (
    "io"
    "fmt"
    "bytes"
)

const text = "Reaaaaally long and complex text to read in chunk"

func main() {
    b := bytes.NewBufferString(text)

    t := (*transformReaderHandler)(b)

    readByChunk(t)
}

type transformReaderHandler bytes.Buffer

func (t *transformReaderHandler) Read(p []byte) (n int, err error) {
    n, err = (*bytes.Buffer)(t).Read(p)
    if n > 0 {
        p[0] = 'X'
    }
    return
}

func readByChunk(r io.Reader) {
    var p = make([]byte, 4)

    for {
        n, err := r.Read(p)
        if err == io.EOF {
            break
        }
        fmt.Println(string(p[:n]))
    }
}
  • 写回答

2条回答 默认 最新

  • dongqin1819 2018-09-06 14:06
    关注

    The code below say "process take too long"

    Why do this code is not working ?

    In the transformReaderHandler.Read() method, you have a value receiver. That means each time Read() is called, it gets a copy of the instance on which it was called. Then when you then call (*bytes.Buffer)(&t).Read(p), it modifies the internal state of that instance so that next time when you read, it reads from after the point it read earlier.

    Now because the instance is a copy, it is discarded after the method exits and the original instance remains unchanged. Hence, each time you call Read(), bytes.Buffer.Read() reads only the first few bytes. To prove this, add a statement fmt.Println("n=", n, "err=", err) inside readByChunk() after calling Read().

    To quickly check that this is indeed due to the value receiver, you can define transformReaderHandler.Read() with a pointer receiver and store t as t = (*transformReaderHandler)(b). I'll let you examine what it does. (edit: the correct solution involving embedding is in the comments)

    Do you have any more efficient (memory efficient, computationally efficient) solution ?

    If you're only looking for buffered IO for more efficient reads, look at the bufio.NewReader(). If that's not sufficient, you can take inspiration from it and wrap around an io.Reader interface instead of wrapping over a bytes.Buffer instance.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?