doufen3563 2018-08-10 01:10
浏览 97
已采纳

将无限字节写入bytes.Buffer

When I run the following code

func writeBytes() ([]byte, error) {
    var buf bytes.Buffer
    dstBytes := bufio.NewWriter(&buf)
    writeTonsOfBytes(dstBytes)
    b := buf.Bytes()
    fmt.Println(len(b))
    return b, nil
}

I get the output

32768

Which is signaling to me that there must be a limit on my bytes.Buffer instance, which I can't find documentation for.

How can I write an unlimited number of bytes to a bytes.Buffer?

  • 写回答

1条回答 默认 最新

  • duan97689 2018-08-11 22:06
    关注

    There is no call to

    dstBytes.Flush()
    

    in the code. Any bytes that are currently unwritten to buf will remain unwritten, causing buf to have an incomplete record of the bytes written to dstBytes in writeTonsOfBytes.

    It also explains why the number of written bytes would end up as a power of 2 because new bytes are written to the underlying buffer at such an increment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?