本文整理汇总了Golang中encoding/binary.Write函数的典型用法代码示例。如果您正苦于以下问题:Golang Write函数的具体用法?Golang Write怎么用?Golang Write使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Write函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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
}
示例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
}
示例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
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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()
}
示例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()
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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")
}