本文整理匯總了Golang中encoding/binary.Read函數的典型用法代碼示例。如果您正苦於以下問題:Golang Read函數的具體用法?Golang Read怎麽用?Golang Read使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Read函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: convertIPPDateToTime
// convertIPPDateToTime converts an RFC 2579 date to a time.Time object.
func convertIPPDateToTime(date *C.ipp_uchar_t) time.Time {
r := bytes.NewReader(C.GoBytes(unsafe.Pointer(date), 11))
var year uint16
var month, day, hour, min, sec, dsec uint8
binary.Read(r, binary.BigEndian, &year)
binary.Read(r, binary.BigEndian, &month)
binary.Read(r, binary.BigEndian, &day)
binary.Read(r, binary.BigEndian, &hour)
binary.Read(r, binary.BigEndian, &min)
binary.Read(r, binary.BigEndian, &sec)
binary.Read(r, binary.BigEndian, &dsec)
var utcDirection, utcHour, utcMin uint8
binary.Read(r, binary.BigEndian, &utcDirection)
binary.Read(r, binary.BigEndian, &utcHour)
binary.Read(r, binary.BigEndian, &utcMin)
var utcOffset time.Duration
utcOffset += time.Duration(utcHour) * time.Hour
utcOffset += time.Duration(utcMin) * time.Minute
var loc *time.Location
if utcDirection == '-' {
loc = time.FixedZone("", -int(utcOffset.Seconds()))
} else {
loc = time.FixedZone("", int(utcOffset.Seconds()))
}
nsec := int(dsec) * 100 * int(time.Millisecond)
return time.Date(int(year), time.Month(month), int(day), int(hour), int(min), int(sec), nsec, loc)
}
示例2: ReadNewInvalidState
func ReadNewInvalidState(in io.Reader) *NewInvalidState {
var reason, gameMode byte
binary.Read(in, ByteOrder, &reason)
binary.Read(in, ByteOrder, &gameMode)
ptr := NewInvalidState{reason, gameMode}
return &ptr
}
示例3: ReadFrom
func (e *indexEntry) ReadFrom(r io.Reader) (n int64, err error) {
var deleted byte
if err = binary.Read(r, binary.BigEndian, &deleted); err != nil {
return 0, err
}
e.deleted = (deleted != 0)
var value_len uint32
if err = binary.Read(r, binary.BigEndian, &value_len); err != nil {
return 0, err
}
value := make([]byte, int(value_len))
for i := 0; i < int(value_len); i++ {
var b byte
if err = binary.Read(r, binary.BigEndian, &b); err != nil {
return 0, err
}
value[i] = b
}
e.value = string(value)
var id int64
if err = binary.Read(r, binary.BigEndian, &id); err != nil {
return 0, err
}
e.id = id
return int64(binary.Size(deleted) + binary.Size(value) + binary.Size(id)), nil
}
示例4: ReadTimestampedEntryInto
// ReadTimestampedEntryInto parses the byte-stream representation of a
// TimestampedEntry from |r| and populates the struct |t| with the data. See
// RFC section 3.4 for details on the format.
// Returns a non-nil error if there was a problem.
func ReadTimestampedEntryInto(r io.Reader, t *TimestampedEntry) error {
var err error
if err = binary.Read(r, binary.BigEndian, &t.Timestamp); err != nil {
return err
}
if err = binary.Read(r, binary.BigEndian, &t.EntryType); err != nil {
return err
}
switch t.EntryType {
case X509LogEntryType:
if t.X509Entry, err = readVarBytes(r, CertificateLengthBytes); err != nil {
return err
}
case PrecertLogEntryType:
if err := binary.Read(r, binary.BigEndian, &t.PrecertEntry.IssuerKeyHash); err != nil {
return err
}
if t.PrecertEntry.TBSCertificate, err = readVarBytes(r, PreCertificateLengthBytes); err != nil {
return err
}
case XJSONLogEntryType:
if t.JSONData, err = readVarBytes(r, JSONLengthBytes); err != nil {
return err
}
default:
return fmt.Errorf("unknown EntryType: %d", t.EntryType)
}
t.Extensions, err = readVarBytes(r, ExtensionsLengthBytes)
return nil
}
示例5: readStats
func readStats(r io.Reader, typ string) (*ReferenceStats, error) {
var (
vOff uint64
stats ReferenceStats
err error
)
err = binary.Read(r, binary.LittleEndian, &vOff)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats chunk begin virtual offset: %v", typ, err)
}
stats.Chunk.Begin = makeOffset(vOff)
err = binary.Read(r, binary.LittleEndian, &vOff)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats chunk end virtual offset: %v", typ, err)
}
stats.Chunk.End = makeOffset(vOff)
err = binary.Read(r, binary.LittleEndian, &stats.Mapped)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats mapped count: %v", typ, err)
}
err = binary.Read(r, binary.LittleEndian, &stats.Unmapped)
if err != nil {
return nil, fmt.Errorf("%s: failed to read index stats unmapped count: %v", typ, err)
}
return &stats, nil
}
示例6: handleEventAll
func (w *Watcher) handleEventAll(data []byte) {
buf := bytes.NewBuffer(data)
msg := &cnMsg{}
hdr := &procEventHeader{}
binary.Read(buf, byteOrder, msg)
binary.Read(buf, byteOrder, hdr)
switch hdr.What {
case PROC_EVENT_FORK:
event := &forkProcEvent{}
binary.Read(buf, byteOrder, event)
ppid := int(event.ParentTgid)
pid := int(event.ChildTgid)
w.Fork <- &ProcEventFork{ParentPid: ppid, ChildPid: pid}
case PROC_EVENT_EXEC:
event := &execProcEvent{}
binary.Read(buf, byteOrder, event)
pid := int(event.ProcessTgid)
w.Exec <- &ProcEventExec{Pid: pid}
case PROC_EVENT_EXIT:
event := &exitProcEvent{}
binary.Read(buf, byteOrder, event)
pid := int(event.ProcessTgid)
w.Exit <- &ProcEventExit{Pid: pid}
}
}
示例7: Parse
// Parse initialises the read loop and begins parsing the incoming request
func (p *Parser) Parse() {
b := make([]byte, 2)
Read:
for {
n, err := p.Conn.Read(b)
switch {
case err == io.EOF:
break Read
case n == 0:
goto Read
}
switch string(b) {
case "1W": // window length
binary.Read(p.Conn, binary.BigEndian, &p.wlen)
case "1C": // frame length
binary.Read(p.Conn, binary.BigEndian, &p.plen)
if err := p.read(); err != nil {
log.Printf("[%s] error parsing %v", p.Conn.RemoteAddr().String(), err)
break Read
}
if err := p.ack(); err != nil {
log.Printf("[%s] error acking %v", p.Conn.RemoteAddr().String(), err)
break Read
}
default:
// This really shouldn't happen
log.Printf("[%s] Received unknown type", p.Conn.RemoteAddr().String(), err)
break Read
}
}
}
示例8: handleHead
func handleHead(reader io.Reader) (seq []byte, flags uint8, count uint16, err error) {
var seqLength uint16
err = binary.Read(reader, binary.BigEndian, &seqLength)
if err != nil {
return
}
seq = make([]byte, seqLength)
_, err = io.ReadFull(reader, seq)
if err != nil {
fmt.Println("read seq")
return
}
err = binary.Read(reader, binary.BigEndian, &flags)
if err != nil {
fmt.Println("read flags")
return
}
err = binary.Read(reader, binary.BigEndian, &count)
if err != nil {
fmt.Println("read count")
return
}
return
}
示例9: Decode
// Decode fills the packet withinformation of the packet.
func (r *wireMasterResponse) Decode(packet io.Reader, n int) error {
if r.Ips == nil {
r.Ips = make([]wireIP, 0, 50)
}
err := binary.Read(packet, byteOrder, &r.Head)
if err != nil {
return err
}
if !reflect.DeepEqual(r.Head.Magic, masterResponseHeader) {
return errors.New("Header does not match.")
}
remaining := n - binary.Size(r.Head.Magic)
ipsize := binary.Size(wireIP{})
for ; remaining >= ipsize; remaining -= ipsize {
ip := wireIP{}
// Normal little endian read.
if err := binary.Read(packet, byteOrder, &ip.Oct); err != nil {
return err
}
// Seperate read because of big endian requirement
if err := binary.Read(packet, binary.BigEndian, &ip.Port); err != nil {
return err
}
r.Ips = append(r.Ips, ip)
}
return nil
}
示例10: Parse
func Parse(d []byte) (*Key, error) {
if len(d) != 78 {
return nil, fmt.Errorf("Input of wrong length %d (expected 78).", len(d))
}
b := bytes.NewBuffer(d)
var version uint32
binary.Read(b, binary.BigEndian, &version)
k := Key{version: version}
binary.Read(b, binary.BigEndian, &k.depth)
k.parent = make([]byte, 4)
b.Read(k.parent)
binary.Read(b, binary.BigEndian, &k.index)
k.code = make([]byte, 32)
b.Read(k.code)
ser := make([]byte, 33)
b.Read(ser)
switch version {
case BitcoinExtendedPrivateKeyVersion, BitcoinTestnetExtendedPrivateKeyVersion:
k.prvKey = parse256(ser[1:33])
case BitcoinExtendedPublicKeyVersion, BitcoinTestnetExtendedPublicKeyVersion:
k.pubKey = ser
default:
return nil, fmt.Errorf("Input has unrecognized version %x.", version)
}
return &k, nil
}
示例11: makeUniqueMcs
func makeUniqueMcs(catRange uint32) (string, error) {
var (
n uint32
c1, c2 uint32
mcs string
tries = 1000000
err error
)
for i := 0; i < tries; i++ {
binary.Read(rand.Reader, binary.LittleEndian, &n)
c1 = n % catRange
binary.Read(rand.Reader, binary.LittleEndian, &n)
c2 = n % catRange
if c1 == c2 {
continue
}
if c1 > c2 {
c1, c2 = c2, c1
}
mcs = fmt.Sprintf("s0:c%d,c%d", c1, c2)
err = mcsAdd(mcs)
if err == nil {
return mcs, nil
}
}
return "", fmt.Errorf("couldn't generate unique MCS after %d tries! (last err=%v)", tries, err)
}
示例12: readProtoMessage
// Read a protobuf message from a client
func (client *Client) readProtoMessage() (msg *Message, err error) {
var (
length uint32
kind uint16
)
// Read the message type (16-bit big-endian unsigned integer)
err = binary.Read(client.reader, binary.BigEndian, &kind)
if err != nil {
return
}
// Read the message length (32-bit big-endian unsigned integer)
err = binary.Read(client.reader, binary.BigEndian, &length)
if err != nil {
return
}
buf := make([]byte, length)
_, err = io.ReadFull(client.reader, buf)
if err != nil {
return
}
msg = &Message{
buf: buf,
kind: kind,
client: client,
}
return
}
示例13: readVendorID
func readVendorID(b []byte) (leftovers []byte, vendorID uint16, err error) {
buf := bytes.NewBuffer(b)
leftovers = b
var openByte, appByte, closeByte byte
if openByte, err = buf.ReadByte(); openByte != 0x3e {
return
}
if appByte, err = buf.ReadByte(); appByte != 0x22 && appByte != 0x21 {
return
}
if appByte == 0x22 {
if err = binary.Read(buf, binary.BigEndian, &vendorID); err != nil {
return
}
} else {
var vendorIDByte byte
if err = binary.Read(buf, binary.BigEndian, &vendorIDByte); err != nil {
return
}
vendorID = uint16(vendorIDByte)
}
if closeByte, err = buf.ReadByte(); closeByte != 0x3f {
return
}
bytesRead := len(b) - buf.Len()
leftovers = b[bytesRead:]
return
}
示例14: Read
func (pbp *PBP) Read(rc io.ReadCloser) error {
binary.Read(rc, binary.LittleEndian, &pbp.cookie)
if pbp.cookie == 0x464C457f {
fmt.Printf("File is an elf, converting to empty PBP")
bytes, _ := ioutil.ReadAll(rc)
pbp.data[6] = append([]byte{0x7f, 0x45, 0x4c, 0x46}[:], bytes...)
pbp.cookie = 0x50425000
pbp.version = 0x00010000
return nil
}
if pbp.cookie != 0x50425000 {
return errors.New("bad cookie")
}
binary.Read(rc, binary.LittleEndian, &pbp.version)
for i := 0; i < 8; i++ {
binary.Read(rc, binary.LittleEndian, &pbp.offsets[i])
}
for i := 0; i < 7; i++ {
pbp.data[i] = make([]byte, pbp.offsets[i+1]-pbp.offsets[i])
if len(pbp.data[i]) > 0 {
_, err := rc.Read(pbp.data[i])
if err != nil {
return err
}
}
}
var err error
pbp.data[7], err = ioutil.ReadAll(rc)
return err
}
示例15: UnpackageData
// 解析內部數據包
func UnpackageData(body []byte) (PkgHead, []byte, InnerPkgTail, error) {
p := bytes.NewReader(body)
head := PkgHead{}
tail := InnerPkgTail{}
var jsonStr []byte
err := binary.Read(p, binary.BigEndian, &head)
if err != nil {
return head, nil, tail, errors.New("read pkghead error!!!", err, body)
}
if int(head.PkgLen)+SIZEOF_INNERTAIL != len(body) {
return head, nil, tail, errors.New("data package len error!!!", head.PkgLen, len(body))
}
jsonStr = make([]byte, int(head.PkgLen)-SIZEOF_PKGHEAD)
if err := binary.Read(p, binary.BigEndian, &jsonStr); err != nil {
return head, nil, tail, errors.New("read pkgbody error!!!", err, body)
}
if err := binary.Read(p, binary.BigEndian, &tail); err != nil {
return head, nil, tail, errors.New("read pkgtail error!!!", err, body)
}
return head, jsonStr, tail, nil
}