MCPcopy Create free account
hub / github.com/DNAProject/DNA / byteXReader

Function byteXReader

common/serialization/serialize.go:262–284  ·  view source on GitHub ↗

************************************************************************** ** internal func *** ************************************************************************** ** 2.byteXReader: read x byte and return []byte. ** 3.byteToUint8: change b

(reader io.Reader, x uint64)

Source from the content-addressed store, hash-verified

260//**************************************************************************
261
262func byteXReader(reader io.Reader, x uint64) ([]byte, error) {
263 if x == 0 {
264 return nil, nil
265 }
266 //fast path to avoid buffer reallocation
267 if x < 2*1024*1024 {
268 p := make([]byte, x)
269 _, err := io.ReadFull(reader, p)
270 if err != nil {
271 return nil, err
272 }
273 return p, nil
274 }
275
276 // normal path to avoid attack
277 limited := io.LimitReader(reader, int64(x))
278 buf := &bytes.Buffer{}
279 n, _ := buf.ReadFrom(limited)
280 if n == int64(x) {
281 return buf.Bytes(), nil
282 }
283 return nil, ErrEof
284}
285
286func WriteBool(writer io.Writer, val bool) error {
287 err := binary.Write(writer, binary.LittleEndian, val)

Callers 6

ReadVarBytesFunction · 0.85
ReadBytesFunction · 0.85
ReadByteFunction · 0.85
TestReadVarBytesReadFunction · 0.85
BenchmarkBytesXReaderFunction · 0.85

Calls 1

BytesMethod · 0.45

Tested by 3

TestReadVarBytesReadFunction · 0.68
BenchmarkBytesXReaderFunction · 0.68