douhuifen9942 2018-03-25 14:51
浏览 787
已采纳

在golang中实现io.ReadWriteSeeker

Is there an implementation of io.ReadWriteSeeker to use in Golang?

Since, bytes.Buffer does not implement Seek method, I need to find such an implementation to use as a buffer written by zipwriter and to be read with seeking.

In addition I wont go with Reader(buff.Bytes()) to covert with memory copy, because I can not afford double memory size for buffered data.

In addition, when using os.File as the option, if I wont call f.Sync, it will never touch file system, right? Thanks.

My simplified codes:

func process() {
  buff := new(bytes.Buffer)
  zipWriter := zip.NewWriter(buff)
  // here to add data into zipWriter in sequence
  zipWriter.Close()
  upload(buff) // upload(io.ReadSeeker)
}
  • 写回答

1条回答 默认 最新

  • 普通网友 2018-03-25 17:46
    关注

    For example, using the same underlying array for (uBuf and zBuf) buffers,

    package main
    
    import (
        "archive/zip"
        "bytes"
        "io"
    )
    
    func upload(io.ReadSeeker) {}
    
    func process() {
        zBuf := new(bytes.Buffer)
        zipWriter := zip.NewWriter(zBuf)
        // add data into zipWriter in sequence
        zipWriter.Close()
        uBuf, zBuf := zBuf.Bytes(), nil
        // upload(io.ReadSeeker)
        upload(bytes.NewReader(uBuf))
    }
    
    func main() {}
    

    Playground: https://play.golang.org/p/8TKmnL_vRY9


    Package bytes

    import "bytes" 
    

    func (*Buffer) Bytes

    func (b *Buffer) Bytes() []byte
    

    Bytes returns a slice of length b.Len() holding the unread portion of the buffer. The slice is valid for use only until the next buffer modification (that is, only until the next call to a method like Read, Write, Reset, or Truncate). The slice aliases the buffer content at least until the next buffer modification, so immediate changes to the slice will affect the result of future reads.

    The tuple assignment statement

        uBuf, zBuf := zBuf.Bytes(), nil
    

    gets the slice descriptor for the zipped bytes (zBuf.Bytes()) and assigns it to the slice descriptor uBuf. A slice descriptor is a struct with a pointer to the underlying array, the slice length, and the slice capacity. For example,

    type slice struct {
        array unsafe.Pointer
        len   int
        cap   int
    }
    

    Then, for safety, we assign nil to zBuf to ensure that no further changes can be made to its underlying array, which is now used by uBuf.

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

报告相同问题?

悬赏问题

  • ¥15 C++数组中找第二小的数字程序纠错
  • ¥50 MATLAB APP 制作出现问题
  • ¥15 wannier复现图像时berry曲率极值点与高对称点严重偏移
  • ¥15 利用决策森林为什么会出现这样·的问题(关键词-情感分析)
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)