将一个uint32数组写入一个字节切片,并将其取回。

huangapple go评论89阅读模式
英文:

Write an array of uint32 to a slice of bytes and obtain it back

问题

我正在尝试创建一个os.File的模拟对象,或者更具体地说是io.Reader。我想要模拟读取128位具体数据的操作,而不进行实际读取。

对于单个的uint32,没有问题。

模拟代码:

func (f *FileMock) Read(buf []byte) (n int, err error) {
    binary.BigEndian.PutUint32(buf, uint32(2052))
    return len(buf), nil
}

被测试的方法(简化版):

b := make([]byte, 128)
meta_data := make([]uint32, 4)
_, err = s.Read(b)
if err != nil {
    // 处理错误
}
binary.Read(bytes.NewBuffer(b), binary.BigEndian, &meta_data)
log.Print(meta_data) // 输出 [2052 0 0 0]

但是,当我需要模拟读取一个uint32切片时,PutUint32方法就不再适用,因为它会从切片的开头开始写入(覆盖之前写入的数据)。我尝试了许多bytesbinary工具的组合,但每次都无法从字节中获取数据。以下是我最后一次尝试(不是唯一的尝试):

func (f *FileMock) Read(b []byte) (n int, err error) {
    buf := bytes.NewBuffer(make([]byte, len(b)))
    err = binary.Write(buf, binary.BigEndian, [4]uint32{2051, 123, 28, 28})
    buf.Read(b)
    return len(b), nil
}

使用上述相同的被测试方法,我得到了一个空的切片[0, 0, 0, 0]。请注意,这是os.File.Read方法的模拟,所以我不能创建一个新的字节切片,而是需要将数据写入现有的切片。

首先,我想知道如何解决这个问题。另外,我想知道为什么结果只是[0, 0, 0, 0]

感谢您的回答!

英文:

I'm trying to create a mock for a os.File or to be more specific io.Reader. I want to emulate reading of 128 bits of concrete data without actual reading.

With a single uint32 there is no problems.

Mock:

func (f *FileMock) Read(buf []byte) (n int, err error) {
    binary.BigEndian.PutUint32(buf, uint32(2052))
    return len(buf), nil
}       

Tested method (simplified):

b := make([]byte, 128)                                                      
meta_data := make([]uint32, 4)                                                                                                                                 
_, err = s.Read(b)                                                                          
if err != nil {                                                                             
    // Handle error         
}                                                                                                                                                                                       
binary.Read(bytes.NewBuffer(b), binary.BigEndian, &meta_data)                                                                                                                               
log.Print(meta_data) // Output [2052 0 0 0]

But when I need to mock reading of a uint32 slice PutUint32 is not helpful because it writes to a slice from beginning (overrites previously wrtten data). I tried a bunch of combinations of bytesand binary tools but had no luck every time I can't get the data back from bytes. There is my last attempt (it is not the only):

func (f *FileMock) Read(b []byte) (n int, err error) {
    buf := bytes.NewBuffer(make([]byte, len(b)))
    err = binary.Write(buf, binary.BigEndian, [4]uint32{2051, 123, 28, 28})
    buf.Read(b)
    return len(b), nil
}

With the same tested method as described above I'm obtaining an empty slice [0, 0, 0, 0]. Please note, this is a mock for os.File.Read method so I can't create a new slice of bytes instead of it I need to write my data to an existing slice.

At first I'm wondering how to solve the issue. Also I want to know why there is just [0, 0, 0, 0]?

Grateful for answers!

答案1

得分: 0

切片共享一个底层数组,你可以将 (PutUint32) 写入 buf[4:]、buf[8:] 等位置。

英文:

Slices share an underlying array, you can write (PutUint32) to buf[4:], buf[8:] etc.

huangapple
  • 本文由 发表于 2017年1月25日 00:09:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/41832913.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定