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条)

报告相同问题?

悬赏问题

  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码
  • ¥20 SAP HANA SQL Script 。如何判断字段值包含某个字符串
  • ¥85 cmd批处理参数如果含有双引号,该如何传入?
  • ¥15 fx2n系列plc的自控成型机模拟
  • ¥15 时间序列LSTM模型归回预测代码问题
  • ¥50 使用CUDA如何高效的做并行化处理,是否可以多个分段同时进行匹配计算处理?目前数据传输速度有些慢,如何提高速度,使用gdrcopy是否可行?请给出具体意见。
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),如何通过环岛的原理及完整代码
  • ¥20 机器学习或深度学习问题?困扰了我一个世纪,晚来天欲雪,能饮一杯无?
  • ¥15 c语言数据结构高铁订票系统