本文整理汇总了Golang中encoding/binary.Write函数的典型用法代码示例。如果您正苦于以下问题:Golang Write函数的具体用法?Golang Write怎么用?Golang Write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Write函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: bigIntToNetIPv6
// bigIntToNetIPv6 is a helper function that correctly returns a net.IP with the
// correctly padded values.
func bigIntToNetIPv6(bi *big.Int) *net.IP {
x := make(net.IP, IPv6len)
ipv6Bytes := bi.Bytes()
// It's possibe for ipv6Bytes to be less than IPv6len bytes in size. If
// they are different sizes we to pad the size of response.
if len(ipv6Bytes) < IPv6len {
buf := new(bytes.Buffer)
buf.Grow(IPv6len)
for i := len(ipv6Bytes); i < IPv6len; i++ {
if err := binary.Write(buf, binary.BigEndian, byte(0)); err != nil {
panic(fmt.Sprintf("Unable to pad byte %d of input %v: %v", i, bi, err))
}
}
for _, b := range ipv6Bytes {
if err := binary.Write(buf, binary.BigEndian, b); err != nil {
panic(fmt.Sprintf("Unable to preserve endianness of input %v: %v", bi, err))
}
}
ipv6Bytes = buf.Bytes()
}
i := copy(x, ipv6Bytes)
if i != IPv6len {
panic("IPv6 wrong size")
}
return &x
}
开发者ID:luizbafilho,项目名称:fusis,代码行数:32,代码来源:ipv6addr.go
示例2: Write
func (obj GCAddEffectPacket) Write(buf io.Writer, code uint8) error {
binary.Write(buf, binary.LittleEndian, obj.ObjectID)
binary.Write(buf, binary.LittleEndian, obj.EffectID)
binary.Write(buf, binary.LittleEndian, obj.Duration)
return nil
}
开发者ID:sguzwf,项目名称:ouster,代码行数:7,代码来源:gc.go
示例3: WriteTo
func (e *indexEntry) WriteTo(w io.Writer) (n int64, err error) {
deleted := byte(0)
if e.deleted {
deleted = 1
}
if err = binary.Write(w, binary.BigEndian, deleted); err != nil {
return 0, err
}
value_len := uint32(len(e.value))
if err = binary.Write(w, binary.BigEndian, value_len); err != nil {
return 0, err
}
if err = binary.Write(w, binary.BigEndian, []byte(e.value)); err != nil {
return 0, err
}
if err = binary.Write(w, binary.BigEndian, e.id); err != nil {
return 0, err
}
return int64(binary.Size(deleted) + binary.Size([]byte(e.value)) + binary.Size(e.id)), nil
}
开发者ID:screscent,项目名称:epos,代码行数:25,代码来源:index.go
示例4: WriteEMessage
func (client *Client) WriteEMessage(emsg *EMessage) []byte {
buffer := new(bytes.Buffer)
binary.Write(buffer, binary.BigEndian, emsg.msgid)
binary.Write(buffer, binary.BigEndian, emsg.device_id)
SendMessage(buffer, emsg.msg)
return buffer.Bytes()
}
开发者ID:reasonz,项目名称:im_service,代码行数:7,代码来源:storage_server.go
示例5: serializeV1PrecertSCTSignatureInput
func serializeV1PrecertSCTSignatureInput(timestamp uint64, issuerKeyHash [issuerKeyHashLength]byte, tbs []byte, ext CTExtensions) ([]byte, error) {
if err := checkCertificateFormat(tbs); err != nil {
return nil, err
}
if err := checkExtensionsFormat(ext); err != nil {
return nil, err
}
var buf bytes.Buffer
if err := binary.Write(&buf, binary.BigEndian, V1); err != nil {
return nil, err
}
if err := binary.Write(&buf, binary.BigEndian, CertificateTimestampSignatureType); err != nil {
return nil, err
}
if err := binary.Write(&buf, binary.BigEndian, timestamp); err != nil {
return nil, err
}
if err := binary.Write(&buf, binary.BigEndian, PrecertLogEntryType); err != nil {
return nil, err
}
if _, err := buf.Write(issuerKeyHash[:]); err != nil {
return nil, err
}
if err := writeVarBytes(&buf, tbs, CertificateLengthBytes); err != nil {
return nil, err
}
if err := writeVarBytes(&buf, ext, ExtensionsLengthBytes); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
开发者ID:jfrazelle,项目名称:cfssl,代码行数:31,代码来源:serialization.go
示例6: SerializeTimestampedEntry
// SerializeTimestampedEntry writes timestamped entry to Writer.
// In case of error, w may contain garbage.
func SerializeTimestampedEntry(w io.Writer, t *TimestampedEntry) error {
if err := binary.Write(w, binary.BigEndian, t.Timestamp); err != nil {
return err
}
if err := binary.Write(w, binary.BigEndian, t.EntryType); err != nil {
return err
}
switch t.EntryType {
case X509LogEntryType:
if err := writeVarBytes(w, t.X509Entry, CertificateLengthBytes); err != nil {
return err
}
case PrecertLogEntryType:
if err := binary.Write(w, binary.BigEndian, t.PrecertEntry.IssuerKeyHash); err != nil {
return err
}
if err := writeVarBytes(w, t.PrecertEntry.TBSCertificate, PreCertificateLengthBytes); err != nil {
return err
}
case XJSONLogEntryType:
// TODO: Pending google/certificate-transparency#1243, replace
// with ObjectHash once supported by CT server.
//jsonhash := objecthash.CommonJSONHash(string(t.JSONData))
if err := writeVarBytes(w, []byte(t.JSONData), JSONLengthBytes); err != nil {
return err
}
default:
return fmt.Errorf("unknown EntryType: %d", t.EntryType)
}
writeVarBytes(w, t.Extensions, ExtensionsLengthBytes)
return nil
}
开发者ID:jfrazelle,项目名称:cfssl,代码行数:34,代码来源:serialization.go
示例7: MarshalPacket
func MarshalPacket(pack interface{}) ([]byte, error) {
et := typetest(pack)
if et < EncodingTypeNil || et > EncodingTypeMax {
return nil, errors.New("MarshalPacket unkown data type")
}
if encodingArray[et] == nil {
return nil, errors.New("MarshalPacket unkown data type")
}
data, err := encodingArray[et].Marshal(pack)
if err != nil {
return nil, fmt.Errorf("%v %v", pack, err.Error())
}
packetid, err := getPacketId(pack)
if err != nil {
return nil, fmt.Errorf("%v %v", pack, err.Error())
}
ph := PacketHeader{
EncodeType: int16(et),
PacketId: int16(packetid),
}
w := bytes.NewBuffer(nil)
binary.Write(w, binary.LittleEndian, &ph)
binary.Write(w, binary.LittleEndian, data)
return w.Bytes(), nil
}
开发者ID:zwczou,项目名称:goserver,代码行数:30,代码来源:encoding.go
示例8: Package
func (this *DataPackage) Package() []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, this.Head)
binary.Write(buf, binary.BigEndian, this.Body)
binary.Write(buf, binary.BigEndian, this.Tail)
return buf.Bytes()
}
开发者ID:wxaxiaoyao,项目名称:linux,代码行数:7,代码来源:comm.go
示例9: cipherPacket
func cipherPacket(cmd uint8, data []byte) []byte {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, cmd)
binary.Write(buf, binary.BigEndian, uint16(len(data)))
buf.Write(data)
return buf.Bytes()
}
开发者ID:badfortrains,项目名称:spotcontrol,代码行数:7,代码来源:shannon.go
示例10: flushIndex
func (w *Writer) flushIndex() error {
w.trailer.DataIndexOffset = w.curOffset
w.trailer.DataIndexCount = uint32(len(w.blocks))
w.fp.Write(IndexMagic)
w.curOffset += uint64(len(IndexMagic))
for _, b := range w.blocks {
if err := binary.Write(w.fp, binary.BigEndian, b.offset); err != nil {
return err
}
w.curOffset += uint64(binary.Size(b.offset))
if err := binary.Write(w.fp, binary.BigEndian, b.size); err != nil {
return err
}
w.curOffset += uint64(binary.Size(b.size))
if i, err := writeUvarint(w.fp, uint64(len(b.firstKeyBytes))); err != nil {
return err
} else {
w.curOffset += uint64(i)
}
if i, err := w.fp.Write(b.firstKeyBytes); err != nil {
return err
} else {
w.curOffset += uint64(i)
}
}
return nil
}
开发者ID:jcdavis,项目名称:quiver,代码行数:33,代码来源:writer.go
示例11: Call
func (space *Space) Call(procName string, returnTuple bool, args ...TupleField) (tuples [][][]byte, err error) {
body := new(bytes.Buffer)
flags := BoxFlags
if returnTuple == true {
flags |= BoxReturnTuple
}
err = binary.Write(body, binary.LittleEndian, flags)
if err != nil {
return
}
err = String(procName).Pack(body)
if err != nil {
return
}
err = binary.Write(body, binary.LittleEndian, int32(len(args)))
if err != nil {
return
}
for _, field := range args {
field.Pack(body)
}
tuples, err = space.request(CallOp, body)
return
}
开发者ID:DenisCheremisov,项目名称:go-tarantool,代码行数:30,代码来源:tarantool.go
示例12: Delete
// Refactor: same as Insert but Op number
func (space *Space) Delete(tuple []TupleField, returnTuple bool) (tuples [][][]byte, err error) {
body := new(bytes.Buffer)
flags := BoxFlags
if returnTuple == true {
flags |= BoxReturnTuple
}
requestBody := []int32{space.spaceNo, flags}
err = binary.Write(body, binary.LittleEndian, requestBody)
if err != nil {
return
}
err = binary.Write(body, binary.LittleEndian, int32(len(tuple)))
if err != nil {
return
}
for _, field := range tuple {
field.Pack(body)
}
tuples, err = space.request(DeleteOp, body)
return
}
开发者ID:DenisCheremisov,项目名称:go-tarantool,代码行数:27,代码来源:tarantool.go
示例13: MarshalForSignature
func (m *EntryBlockResponse) MarshalForSignature() ([]byte, error) {
var buf primitives.Buffer
binary.Write(&buf, binary.BigEndian, m.Type())
t := m.GetTimestamp()
data, err := t.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(data)
m.EBlockCount = uint32(len(m.EBlocks))
binary.Write(&buf, binary.BigEndian, m.EBlockCount)
for _, eb := range m.EBlocks {
bin, err := eb.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(bin)
}
m.EntryCount = uint32(len(m.Entries))
binary.Write(&buf, binary.BigEndian, m.EntryCount)
for _, e := range m.Entries {
bin, err := e.MarshalBinary()
if err != nil {
return nil, err
}
buf.Write(bin)
}
return buf.DeepCopyBytes(), nil
}
开发者ID:FactomProject,项目名称:factomd,代码行数:34,代码来源:entryBlockResponse.go
示例14: writeNumber
func writeNumber(b io.Writer, num int) (err error) {
// The first two bits encode the size:
// 00 = 1 byte
// 01 = 2 bytes
// 10 = 3 bytes
// 11 = 4 bytes
switch {
case num < 64: // 2^(8-2)
// Fits in one byte.
binary.Write(b, binary.BigEndian, uint8(num))
case num < 16384: // 2^(16-2)
// Fits in two bytes.
binary.Write(b, binary.BigEndian, uint16(num|0x4000)) // Set the size to 01.
case num < 4194304: // 2^(24-2)
// Fits in three bytes.
// Drop the two least significant bytes and set the size to 10.
binary.Write(b, binary.BigEndian, uint8((num>>16)|0x80))
// Drop the two most significant bytes.
binary.Write(b, binary.BigEndian, uint16(num&0xffff))
case num < 1073741824: // 2^(32-2):
// Fits in four bytes.
// Set the size to 11.
binary.Write(b, binary.BigEndian, uint32(num|0xc0000000))
default:
err = fmt.Errorf("Number is too big: %d", num)
}
return err
}
开发者ID:sergiusens,项目名称:go-subunit,代码行数:28,代码来源:subunit.go
示例15: WritePage
// Writes the tny_page to the specified writer. We want to do this
// in Go so that we can write a page to local disk, Hadoop, S3, etc
func (self *TnyPage) WritePage(writer *bufio.Writer) {
// Lets write out the page...
// fmt.Printf("Writing page for %s (ptr: %p, len: %d):\n", self.Column.Name, self.cptr, self.Length())
if err := binary.Write(writer, binary.LittleEndian, int32(self.Depth())); err != nil {
panic("Unable to write page header (depth) (" + err.Error() + ")")
}
if err := binary.Write(writer, binary.LittleEndian, int32(self.Length())); err != nil {
panic("Unable to write page header (length) (" + err.Error() + ")")
}
// fmt.Printf("Wrote header, writing data now...\n")
for d := 0; d < int(self.Depth()); d++ {
bmp := self.BitmapAtDepth(d)
bmpSlice := bmp.Slice()
// fmt.Printf("\t%s\n", bmp.BitString(100))
// fmt.Printf("Writing slice %d...\n", d)
if err := binary.Write(writer, binary.LittleEndian, bmpSlice); err != nil {
panic("Unable to write page (" + err.Error() + ")")
}
}
// fmt.Printf("Write Page (%d): ", depth)
// for i := 0; i < 10; i++ {
// fmt.Printf("%d, ", self.Access(i))
// }
// fmt.Printf("\n")
}
开发者ID:yonglehou,项目名称:tnydb,代码行数:36,代码来源:TnyPage.go
示例16: Serialize
func (m *JointTrajectory) Serialize(w io.Writer) (err error) {
if err = ros.SerializeMessageField(w, "Header", &m.Header); err != nil {
return err
}
// Write size little endian
err = binary.Write(w, binary.LittleEndian, uint32(len(m.JointNames)))
if err != nil {
return fmt.Errorf("could not write array length: %s", err)
}
for _, elem := range m.JointNames {
if err = ros.SerializeMessageField(w, "string", &elem); err != nil {
return err
}
}
// Write size little endian
err = binary.Write(w, binary.LittleEndian, uint32(len(m.Points)))
if err != nil {
return fmt.Errorf("could not write array length: %s", err)
}
for _, elem := range m.Points {
if err = ros.SerializeMessageField(w, "JointTrajectoryPoint", &elem); err != nil {
return err
}
}
return
}
开发者ID:ppg,项目名称:rosgo,代码行数:29,代码来源:JointTrajectory.msg.go
示例17: PackageData
// 打包
func PackageData(head PkgHead, body []byte, tail InnerPkgTail) *bytes.Buffer {
buf := new(bytes.Buffer)
binary.Write(buf, binary.BigEndian, head)
binary.Write(buf, binary.BigEndian, body)
binary.Write(buf, binary.BigEndian, tail)
return buf
}
开发者ID:wxaxiaoyao,项目名称:linux,代码行数:8,代码来源:comm.go
示例18: NewSprite
func NewSprite(vertices []float32, program *gg.Program, texture *gg.Texture) (*Sprite, error) {
buf := new(bytes.Buffer)
if err := binary.Write(buf, binary.LittleEndian, vertices); err != nil {
return nil, err
}
pvbo := gg.CreateBuffer()
gg.BindBuffer(gg.ARRAY_BUFFER, pvbo)
gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)
texVertices := []float32{
0, 0,
0, 1,
1, 1,
1, 0,
}
buf.Reset()
if err := binary.Write(buf, binary.LittleEndian, texVertices); err != nil {
return nil, err
}
tvbo := gg.CreateBuffer()
gg.BindBuffer(gg.ARRAY_BUFFER, tvbo)
gg.BufferData(gg.ARRAY_BUFFER, buf.Bytes(), gg.STATIC_DRAW)
return &Sprite{
pvbo: pvbo,
tvbo: tvbo,
program: program,
tex: texture,
}, nil
}
开发者ID:dmac,项目名称:gg,代码行数:30,代码来源:texture.go
示例19: decryptAES
func decryptAES(src, key, privParam []byte, engineBoots, engineTime int32) (
dst []byte, err error) {
if len(privParam) != 8 {
err = ArgumentError{
Value: len(privParam),
Message: "Invalid AES PrivParameter length",
}
return
}
block, err := aes.NewCipher(key[:16])
if err != nil {
return
}
var buf bytes.Buffer
binary.Write(&buf, binary.BigEndian, engineBoots)
binary.Write(&buf, binary.BigEndian, engineTime)
iv := append(buf.Bytes(), privParam...)
dst = make([]byte, len(src))
mode := cipher.NewCFBDecrypter(block, iv)
mode.XORKeyStream(dst, src)
return
}
开发者ID:mgenov,项目名称:snmpgo,代码行数:27,代码来源:security.go
示例20: MarshalBinary
func (e *Entry) MarshalBinary() ([]byte, error) {
buf := new(bytes.Buffer)
// 1 byte Version
if err := binary.Write(buf, binary.BigEndian, e.Version); err != nil {
return buf.Bytes(), err
}
// 32 byte ChainID
buf.Write(e.ChainID.Bytes())
// ExtIDs
if ext, err := e.MarshalExtIDsBinary(); err != nil {
return buf.Bytes(), err
} else {
// 2 byte size of ExtIDs
if err := binary.Write(buf, binary.BigEndian, int16(len(ext))); err != nil {
return buf.Bytes(), err
}
// binary ExtIDs
buf.Write(ext)
}
// Content
buf.Write(e.Content)
return buf.Bytes(), nil
}
开发者ID:FactomProject,项目名称:FactomCode,代码行数:29,代码来源:entry.go
注:本文中的encoding/binary.Write函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论