Jonathan Hart | 44bdbfc | 2020-04-14 17:45:47 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior University |
| 3 | * Copyright (c) 2011, 2012 Open Networking Foundation |
| 4 | * Copyright 2013, Big Switch Networks, Inc. This library was generated by the LoxiGen Compiler. |
| 5 | * Copyright 2018, Red Hat, Inc. |
| 6 | */ |
| 7 | package goloxi |
| 8 | |
| 9 | import ( |
| 10 | "bytes" |
| 11 | "encoding/binary" |
| 12 | ) |
| 13 | |
| 14 | type Encoder struct { |
| 15 | buffer *bytes.Buffer |
| 16 | } |
| 17 | |
| 18 | func NewEncoder() *Encoder { |
| 19 | return &Encoder{ |
| 20 | buffer: new(bytes.Buffer), |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | func (e *Encoder) PutChar(c byte) { |
| 25 | e.buffer.WriteByte(c) |
| 26 | } |
| 27 | |
| 28 | func (e *Encoder) PutUint8(i uint8) { |
| 29 | e.buffer.WriteByte(i) |
| 30 | } |
| 31 | |
| 32 | func (e *Encoder) PutUint16(i uint16) { |
| 33 | var tmp [2]byte |
| 34 | binary.BigEndian.PutUint16(tmp[0:2], i) |
| 35 | e.buffer.Write(tmp[:]) |
| 36 | } |
| 37 | |
| 38 | func (e *Encoder) PutUint32(i uint32) { |
| 39 | var tmp [4]byte |
| 40 | binary.BigEndian.PutUint32(tmp[0:4], i) |
| 41 | e.buffer.Write(tmp[:]) |
| 42 | } |
| 43 | |
| 44 | func (e *Encoder) PutUint64(i uint64) { |
| 45 | var tmp [8]byte |
| 46 | binary.BigEndian.PutUint64(tmp[0:8], i) |
| 47 | e.buffer.Write(tmp[:]) |
| 48 | } |
| 49 | |
| 50 | func (e *Encoder) PutUint128(i Uint128) { |
| 51 | var tmp [16]byte |
| 52 | binary.BigEndian.PutUint64(tmp[0:8], i.Hi) |
| 53 | binary.BigEndian.PutUint64(tmp[8:16], i.Lo) |
| 54 | e.buffer.Write(tmp[:]) |
| 55 | } |
| 56 | |
| 57 | func (e *Encoder) Write(b []byte) { |
| 58 | e.buffer.Write(b) |
| 59 | } |
| 60 | |
| 61 | func (e *Encoder) Bytes() []byte { |
| 62 | return e.buffer.Bytes() |
| 63 | } |