remove readNBytes func

This commit is contained in:
Marten Seemann 2016-04-14 17:55:28 +07:00
parent 3d767401d0
commit c0edd24a2e

View file

@ -2,7 +2,6 @@ package utils
import (
"bytes"
"encoding/binary"
"io"
)
@ -22,43 +21,53 @@ func ReadUintN(b io.ByteReader, length uint8) (uint64, error) {
// ReadUint32 reads a uint32
func ReadUint32(b io.ByteReader) (uint32, error) {
slice, err := readNBytes(b, 4)
if err != nil {
var b1, b2, b3, b4 uint8
var err error
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
return binary.LittleEndian.Uint32(slice), nil
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
if b3, err = b.ReadByte(); err != nil {
return 0, err
}
if b4, err = b.ReadByte(); err != nil {
return 0, err
}
return uint32(b1) + uint32(b2)<<8 + uint32(b3)<<16 + uint32(b4)<<24, nil
}
// ReadUint32BigEndian reads a uint32 Big Endian
func ReadUint32BigEndian(b io.ByteReader) (uint32, error) {
slice, err := readNBytes(b, 4)
if err != nil {
var b1, b2, b3, b4 uint8
var err error
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
return binary.BigEndian.Uint32(slice), nil
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
if b3, err = b.ReadByte(); err != nil {
return 0, err
}
if b4, err = b.ReadByte(); err != nil {
return 0, err
}
return uint32(b4) + uint32(b3)<<8 + uint32(b2)<<16 + uint32(b1)<<24, nil
}
// ReadUint16 reads a uint16
func ReadUint16(b io.ByteReader) (uint16, error) {
slice, err := readNBytes(b, 2)
if err != nil {
var b1, b2 uint8
var err error
if b1, err = b.ReadByte(); err != nil {
return 0, err
}
return binary.LittleEndian.Uint16(slice), nil
}
func readNBytes(b io.ByteReader, n int) ([]byte, error) {
slice := make([]byte, n, n)
var val uint8
var err error
for i := 0; i < n; i++ {
val, err = b.ReadByte()
if err != nil {
return []byte{}, err
}
slice[i] = val
if b2, err = b.ReadByte(); err != nil {
return 0, err
}
return slice, nil
return uint16(b1) + uint16(b2)<<8, nil
}
// WriteUint64 writes a uint64