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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • dongqiu5184 2018-09-06 14:03
    关注

    You're copying the bytes.Buffer value each time Read is called on the transformReaderHandler, so you can never progress through the buffer. You must used a *bytes.Buffer pointer to avoid this copy.

    Embed the buffer (or alternatively add it as a named field) in your transformReaderHandler, so you can call delegate the Read method as needed.

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

    https://play.golang.org/p/npZQ4Tz0hhv

    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#c++#的问题:c++如何使用websocketpp实现websocket接口调用,求示例代码和相关资料
  • ¥15 51单片机的外部中断,按下按键后不能切换到另一个模式
  • ¥15 java连接sqlserver有问题
  • ¥15 yolov8 如何调cfg参数
  • ¥15 这个四人抢答器代码哪儿有问题?仿真程序怎么写?
  • ¥15 burpsuite密码爆破
  • ¥15 关于#ubuntu#的问题,如何解决?(相关搜索:移动硬盘)
  • ¥15 scikit安装之后import不了
  • ¥15 Ros2编译一个使用opencv的c++节点的时候,报了这个错误,请问怎么解决啊
  • ¥15 人脸识别相关算法 YOLO,AI等