当前位置: 首页>>代码示例>>Golang>>正文


Golang ByteOrder.PutUint16方法代码示例

本文整理汇总了Golang中encoding/binary.ByteOrder.PutUint16方法的典型用法代码示例。如果您正苦于以下问题:Golang ByteOrder.PutUint16方法的具体用法?Golang ByteOrder.PutUint16怎么用?Golang ByteOrder.PutUint16使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在encoding/binary.ByteOrder的用法示例。


在下文中一共展示了ByteOrder.PutUint16方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: PutUint16

// PutUint16 serializes the provided uint16 using the given byte order into a
// buffer from the free list and writes the resulting two bytes to the given
// writer.
func (l binaryFreeList) PutUint16(w io.Writer, byteOrder binary.ByteOrder, val uint16) error {
	buf := l.Borrow()[:2]
	byteOrder.PutUint16(buf, val)
	_, err := w.Write(buf)
	l.Return(buf)
	return err
}
开发者ID:decred,项目名称:dcrd,代码行数:10,代码来源:common.go

示例2: writeIFD

func writeIFD(w io.Writer, ifdOffset int, d []IfdEntry, enc binary.ByteOrder) error {
	var buf [ifdLen]byte
	// Make space for "pointer area" containing IFD entry data
	// longer than 4 bytes.
	parea := make([]byte, 1024)
	pstart := ifdOffset + ifdLen*len(d) + 6
	var o int // Current offset in parea.

	// The IFD has to be written with the tags in ascending order.
	sort.Sort(ifdSortedByCode(d))

	// Write the number of entries in this IFD.
	if err := binary.Write(w, enc, uint16(len(d))); err != nil {
		return err
	}
	for _, ent := range d {
		enc.PutUint16(buf[0:2], uint16(ent.tag.Code))
		enc.PutUint16(buf[2:4], uint16(ent.dataType))
		count := uint32(ent.count)
		enc.PutUint32(buf[4:8], count)
		datalen := int(count * lengths[ent.dataType])
		if datalen <= 4 {
			for i, b := range ent.rawData {
				buf[8+i] = b
			}
		} else {
			if (o + datalen) > len(parea) {
				newlen := len(parea) + 1024
				for (o + datalen) > newlen {
					newlen += 1024
				}
				newarea := make([]byte, newlen)
				copy(newarea, parea)
				parea = newarea
			}
			for i, b := range ent.rawData {
				parea[o+i] = b
			}
			enc.PutUint32(buf[8:12], uint32(pstart+o))
			o += datalen
		}
		if _, err := w.Write(buf[:]); err != nil {
			return err
		}
	}
	// The IFD ends with the offset of the next IFD in the file,
	// or zero if it is the last one (page 14).
	if err := binary.Write(w, enc, uint32(0)); err != nil {
		return err
	}
	_, err := w.Write(parea[:o])
	return err
}
开发者ID:hylhero,项目名称:go-spatial,代码行数:53,代码来源:geotiff.go

示例3: packUint

func packUint(order binary.ByteOrder, v interface{}) (data []byte) {
	switch val := v.(type) {
	case uint64:
		data = make([]byte, 8)
		order.PutUint64(data, val)
	case uint32:
		data = make([]byte, 4)
		order.PutUint32(data, val)
	case uint16:
		data = make([]byte, 2)
		order.PutUint16(data, val)
	case uint8:
		data = []byte{byte(val)}
	default:
		panic("unsupported type")
	}
	return data
}
开发者ID:rwcarlsen,项目名称:gobitmsg,代码行数:18,代码来源:structures.go

示例4: newSimpleProtocol

func newSimpleProtocol(n int, byteOrder binary.ByteOrder) *simpleProtocol {
	protocol := &simpleProtocol{
		n:  n,
		bo: byteOrder,
	}

	switch n {
	case 1:
		protocol.encodeHead = func(buffer []byte) {
			buffer[0] = byte(len(buffer) - n)
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(buffer[0])
		}
	case 2:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint16(buffer, uint16(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint16(buffer))
		}
	case 4:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint32(buffer, uint32(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint32(buffer))
		}
	case 8:
		protocol.encodeHead = func(buffer []byte) {
			byteOrder.PutUint64(buffer, uint64(len(buffer)-n))
		}
		protocol.decodeHead = func(buffer []byte) int {
			return int(byteOrder.Uint64(buffer))
		}
	default:
		panic("unsupported packet head size")
	}

	return protocol
}
开发者ID:ZhangTingkuo,项目名称:FishChatServer,代码行数:41,代码来源:protocol.go

示例5:

func (init *initConnT) send (out io.Writer) {
	var data [12]byte;
	data[0] = init.endianness;
	var end binary.ByteOrder;
	if data[0] == xLittleEndian {
		end = binary.LittleEndian;
	}
	else {
		end = binary.BigEndian;
	}
	end.PutUint16(data[2:4], init.protocolMajorVersion);
	end.PutUint16(data[4:6], init.protocolMinorVersion);
	end.PutUint16(data[6:8], init.authProtoNameSize);
	end.PutUint16(data[8:10], init.authProtoDataSize);
	out.Write(&data);
    out.Write(strings.Bytes(init.authProtoName));
    padding := 4 - (init.authProtoNameSize % 4);
    namePadding := make([]byte, padding);
    out.Write(namePadding);
    padding = 4 - (init.authProtoDataSize % 4);
    dataPadding := make([]byte, padding);
    out.Write(dataPadding);
}
开发者ID:zerathidune,项目名称:go-xlib,代码行数:23,代码来源:xlib.go

示例6: WriteUint16

func (b *Buffer) WriteUint16(u uint16, order binary.ByteOrder) error {
	i := b.Grows(2)
	order.PutUint16(b.Buf[i:], u)
	return nil
}
开发者ID:huangjiasingle,项目名称:gohper,代码行数:5,代码来源:buffer.go


注:本文中的encoding/binary.ByteOrder.PutUint16方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。