Golang bytes.Add类(方法)实例源码

下面列出了Golang bytes.Add 类(方法)源码代码实例,从而了解它的用法。

作者:jmettrau    项目:glaiv   
func readUntilCrLf(con *net.TCPConn) (line []byte, err os.Error) {

	buf := make([]byte, 1)
	var data []byte
	crSeen := false

	for {
		_, err := con.Read(buf)
		if err != nil {
			if err == os.EOF {
				break
			} else {
				return nil, err
			}
		}
		if crSeen {
			if buf[0] == 10 {
				break
			} else {
				crSeen = false
				data = bytes.Add(data, buf)
			}
		} else {
			if buf[0] == 13 {
				crSeen = true
			} else {
				data = bytes.Add(data, buf)
			}
		}
	}

	return data, nil
}

作者:ivanwy    项目:google-g   
// Extract regular text from the beginning of the pattern.
// That text can be used by doExecute to speed up matching.
func (re *Regexp) setPrefix() {
	var b []byte
	var utf = make([]byte, utf8.UTFMax)
	// First instruction is start; skip that.
	i := re.inst.At(0).(instr).next().index()
Loop:
	for i < re.inst.Len() {
		inst := re.inst.At(i).(instr)
		// stop if this is not a char
		if inst.kind() != _CHAR {
			break
		}
		// stop if this char can be followed by a match for an empty string,
		// which includes closures, ^, and $.
		switch re.inst.At(inst.next().index()).(instr).kind() {
		case _BOT, _EOT, _ALT:
			break Loop
		}
		n := utf8.EncodeRune(inst.(*_Char).char, utf)
		b = bytes.Add(b, utf[0:n])
		i = inst.next().index()
	}
	// point prefixStart instruction to first non-CHAR after prefix
	re.prefixStart = re.inst.At(i).(instr)
	re.prefixBytes = b
	re.prefix = string(b)
}

作者:rapgame    项目:golang-chin   
// UnmarshalJSON sets *m to a copy of data.
func (m *RawMessage) UnmarshalJSON(data []byte) os.Error {
	if m == nil {
		return os.NewError("json.RawMessage: UnmarshalJSON on nil pointer")
	}
	*m = bytes.Add((*m)[0:0], data)
	return nil
}

作者:GNA-SERVICES-IN    项目:MoNGat   
// readHandshake reads the next handshake message from
// the record layer.
// c.in.Mutex < L; c.out.Mutex < L.
func (c *Conn) readHandshake() (interface{}, os.Error) {
	for c.hand.Len() < 4 {
		if c.err != nil {
			return nil, c.err
		}
		c.readRecord(recordTypeHandshake)
	}

	data := c.hand.Bytes()
	n := int(data[1])<<16 | int(data[2])<<8 | int(data[3])
	if n > maxHandshake {
		c.sendAlert(alertInternalError)
		return nil, c.err
	}
	for c.hand.Len() < 4+n {
		if c.err != nil {
			return nil, c.err
		}
		c.readRecord(recordTypeHandshake)
	}
	data = c.hand.Next(4 + n)
	var m handshakeMessage
	switch data[0] {
	case typeClientHello:
		m = new(clientHelloMsg)
	case typeServerHello:
		m = new(serverHelloMsg)
	case typeCertificate:
		m = new(certificateMsg)
	case typeCertificateRequest:
		m = new(certificateRequestMsg)
	case typeCertificateStatus:
		m = new(certificateStatusMsg)
	case typeServerHelloDone:
		m = new(serverHelloDoneMsg)
	case typeClientKeyExchange:
		m = new(clientKeyExchangeMsg)
	case typeCertificateVerify:
		m = new(certificateVerifyMsg)
	case typeNextProtocol:
		m = new(nextProtoMsg)
	case typeFinished:
		m = new(finishedMsg)
	default:
		c.sendAlert(alertUnexpectedMessage)
		return nil, alertUnexpectedMessage
	}

	// The handshake message unmarshallers
	// expect to be able to keep references to data,
	// so pass in a fresh copy that won't be overwritten.
	data = bytes.Add(nil, data)

	if !m.unmarshal(data) {
		c.sendAlert(alertUnexpectedMessage)
		return nil, alertUnexpectedMessage
	}
	return m, nil
}

作者:bortzmeye    项目:gron   
func EncodeSOA(soa SOArecord) []byte {
	var (
		result []byte
		temp32 []byte
	)
	mname := Encode(soa.Mname)
	length := len(mname)
	rname := Encode(soa.Rname)
	length = length + len(rname)
	length = length + (5 * 4) // Five 32-bits counter at the end
	/* "It's probably cleaner to write to a bytes.Buffer than to
	repeatedly call bytes.Add." Russ Cox, go-nuts ML */
	result = bytes.Add(result, mname)
	result = bytes.Add(result, rname)
	temp32 = make([]byte, 4)
	binary.BigEndian.PutUint32(temp32, soa.Serial)
	result = bytes.Add(result, temp32)
	binary.BigEndian.PutUint32(temp32, soa.Refresh)
	result = bytes.Add(result, temp32)
	binary.BigEndian.PutUint32(temp32, soa.Retry)
	result = bytes.Add(result, temp32)
	binary.BigEndian.PutUint32(temp32, soa.Expire)
	result = bytes.Add(result, temp32)
	binary.BigEndian.PutUint32(temp32, soa.Minimum)
	result = bytes.Add(result, temp32)
	return result[0:length]
}

作者:anvi    项目:gomong   
// To use with messages that receive a response from database
// 'opQuery', 'opGetMore'.
func (self *Connection) sendMessageToReply(m message, reqID int32) os.Error {
	body := m.Bytes()
	h := header(msgHeader{int32(len(body) + _HEADER_SIZE), reqID, 0, m.OpCode()})

	msg := bytes.Add(h, body)
	_, err := self.conn.Write(msg)

	return err
}

作者:MattDavisR    项目:pdfreade   
func Decode(b []byte) []byte {
	r := make([]byte, len(b)*2)[0:0]
	for {
		if b[0] != 128 {
			break
		}
		if b[1] == 3 {
			break
		}
		l := int(b[2]) + (int(b[3]) << 8) + (int(b[4]) << 16) + 6
		if b[1] == 1 {
			r = bytes.Add(r, b[6:l])
		} else {
			r = bytes.Add(r, hex.Encode(b[6:l]))
		}
		b = b[l:]
	}
	return r
}

作者:patrickx    项目:gomong   
func (c *Connection) writeMessage(m message) os.Error {
	body := m.Bytes()
	hb := header(int32(len(body)+16), m.RequestID(), 0, m.OpCode())
	msg := bytes.Add(hb, body)

	_, err := c.conn.Write(msg)

	last_req = m.RequestID()
	return err
}

作者:nstot    项目:gomong   
func (self *_Object) Bytes() []byte {
	buf := bytes.NewBuffer([]byte{})
	for k, v := range self.value {
		buf.WriteByte(byte(v.Kind()))
		buf.WriteString(k)
		buf.WriteByte(0)
		buf.Write(v.Bytes())
	}
	buf.WriteByte(0)

	l := buf.Len() + 4
	w32 := make([]byte, _WORD32)
	pack.PutUint32(w32, uint32(l))
	return bytes.Add(w32, buf.Bytes())
}

作者:IntegerCompan    项目:linaro-android-gc   
func Repeat(alu []byte, n int) {
	buf := bytes.Add(alu, alu)
	off := 0
	for n > 0 {
		m := n
		if m > Line {
			m = Line
		}
		buf1 := out.NextWrite(m + 1)
		copy(buf1, buf[off:])
		buf1[m] = '\n'
		if off += m; off >= len(alu) {
			off -= len(alu)
		}
		n -= m
	}
}

作者:kpumu    项目:gosphin   
func (self *Client) simpleQuery(command int, version int, size int, body []byte) ([]byte, os.Error) {
	conn, err := self.connect()
	if err != nil {
		return nil, err
	}

	header := make([]byte, 8+len(body))
	binary.BigEndian.PutUint16(header[0:2], uint16(command))
	binary.BigEndian.PutUint16(header[2:4], uint16(version))
	binary.BigEndian.PutUint32(header[4:8], uint32(size))

	request := bytes.Add(header, body)

	_, err = conn.Write(request)
	if err != nil {
		return nil, err
	}

	return self.getResponse(conn)
}

作者:beora    项目:fung   
// ReadAll takes an os.File as it's argument and reads in all of it until 
// it signals to stop with an EOF in 
func ReadAll(file * os.File) ([]byte, os.Error) { 
  const BUFSIZE = 1024
  result    := make([]byte, 0)
  buf       := make([]byte, BUFSIZE)
  var sub []byte 
  for { // ever     
    count, err2 := file.Read(buf)
    sub          = buf[0:count]
    // get right slice to append to result
    // append bytes to result
    if count > 0 { 
      result = bytes.Add(result, sub)
    }
    // if EOF, return the result. 
    if err2 == os.EOF { return result, nil }
    // on any other error return nil 
    if err2 != nil    { return nil   , err2 }    
  }
  // should not get here  
  return nil, os.NewError("Can't happen?!");
}

作者:gnanderso    项目:fcgig   
func TestRecords(t *testing.T) {
	for i, test := range recordsTests {
		buf := bytes.NewBuffer(test.raw)
		content, reqId, kind, err := readRecord(buf)
		if err != nil {
			t.Errorf("%d error reading record: %s", i, err.String())
			goto write
		}
		for buf.Len() > 0 {
			moreContent, _, _, err := readRecord(buf)
			if err != nil {
				t.Errorf("%d error reading additional record: %s", i, err.String())
				goto write
			}
			content = bytes.Add(content, moreContent)
		}
		if kind != test.kind {
			t.Errorf("%d got kind %d expected %d", i, kind, test.kind)
		}
		if reqId != test.reqId {
			t.Errorf("%d got request ID %d expected %d", i, kind, test.kind)
		}
		if !bytes.Equal(content, test.content) {
			t.Errorf("%d read wrong content", i)
		}
	write:
		buf.Reset()
		c := &lockReadWriteCloser{ReadWriteCloser: &nilCloser{buf}}
		err = writeRecord(c, test.kind, test.reqId, test.content)
		if err != nil {
			t.Errorf("%d error writing record: %s", i, err.String())
			continue
		}
		if !bytes.Equal(buf.Bytes(), test.raw) {
			t.Errorf("%d wrote wrong content", i)
		}
	}
}

作者:rapgame    项目:golang-chin   
func TestNextValueBig(t *testing.T) {
	var scan scanner
	item, rest, err := nextValue(jsonBig, &scan)
	if err != nil {
		t.Fatalf("nextValue: ", err)
	}
	if len(item) != len(jsonBig) || &item[0] != &jsonBig[0] {
		t.Errorf("invalid item: %d %d", len(item), len(jsonBig))
	}
	if len(rest) != 0 {
		t.Errorf("invalid rest: %d", len(rest))
	}

	item, rest, err = nextValue(bytes.Add(jsonBig, []byte("HELLO WORLD")), &scan)
	if err != nil {
		t.Fatalf("nextValue extra: ", err)
	}
	if len(item) != len(jsonBig) {
		t.Errorf("invalid item: %d %d", len(item), len(jsonBig))
	}
	if string(rest) != "HELLO WORLD" {
		t.Errorf("invalid rest: %d", len(rest))
	}
}

作者:GNA-SERVICES-IN    项目:MoNGat   
// ReadContinuedLineBytes is like ReadContinuedLine but
// returns a []byte instead of a string.
func (r *Reader) ReadContinuedLineBytes() ([]byte, os.Error) {
	// Read the first line.
	line, err := r.ReadLineBytes()
	if err != nil {
		return line, err
	}
	if len(line) == 0 { // blank line - no continuation
		return line, nil
	}
	line = trim(line)

	// Look for a continuation line.
	c, err := r.R.ReadByte()
	if err != nil {
		// Delay err until we read the byte next time.
		return line, nil
	}
	if c != ' ' && c != '\t' {
		// Not a continuation.
		r.R.UnreadByte()
		return line, nil
	}

	// Read continuation lines.
	for {
		// Consume leading spaces; one already gone.
		for {
			c, err = r.R.ReadByte()
			if err != nil {
				break
			}
			if c != ' ' && c != '\t' {
				r.R.UnreadByte()
				break
			}
		}
		var cont []byte
		cont, err = r.ReadLineBytes()
		cont = trim(cont)
		line = bytes.Add(line, space)
		line = bytes.Add(line, cont)
		if err != nil {
			break
		}

		// Check for leading space on next line.
		if c, err = r.R.ReadByte(); err != nil {
			break
		}
		if c != ' ' && c != '\t' {
			r.R.UnreadByte()
			break
		}
	}

	// Delay error until next call.
	if len(line) > 0 {
		err = nil
	}
	return line, err
}

作者:nicholass    项目:gober   
func TestEncodeFloat64(t *testing.T) {
	expected := bytes.Add([]byte{MAGIC, FLOAT}, strings.Bytes(fmt.Sprintf("%.20e", float64(1.2e3))))
	testEncode(t, "Encoding float64 failed", 1.2e3, expected)
}

作者:nicholass    项目:gober   
func TestEncodeString(t *testing.T) {
	expected := bytes.Add([]byte{MAGIC, BIN, 0, 0, 0, 3}, strings.Bytes("foo"))
	testEncode(t, "Encoding string failed", "foo", expected)
}

作者:lougxin    项目:golang-chin   
//.........这里部分代码省略.........
			// If that fails, fall back to mop-up field named "Any".
			if sv != nil {
				k := fieldName(t.Name.Local)
				any := -1
				for i, n := 0, styp.NumField(); i < n; i++ {
					f := styp.Field(i)
					if strings.ToLower(f.Name) == k {
						if err := p.unmarshal(sv.FieldByIndex(f.Index), &t); err != nil {
							return err
						}
						continue Loop
					}
					if any < 0 && f.Name == "Any" {
						any = i
					}
				}
				if any >= 0 {
					if err := p.unmarshal(sv.FieldByIndex(styp.Field(any).Index), &t); err != nil {
						return err
					}
					continue Loop
				}
			}
			// Not saving sub-element but still have to skip over it.
			if err := p.Skip(); err != nil {
				return err
			}

		case EndElement:
			break Loop

		case CharData:
			if saveData != nil {
				data = bytes.Add(data, t)
			}

		case Comment:
			if saveComment != nil {
				comment = bytes.Add(comment, t)
			}
		}
	}

	var err os.Error
	// Helper functions for integer and unsigned integer conversions
	var itmp int64
	getInt64 := func() bool {
		itmp, err = strconv.Atoi64(string(data))
		// TODO: should check sizes
		return err == nil
	}
	var utmp uint64
	getUint64 := func() bool {
		utmp, err = strconv.Atoui64(string(data))
		// TODO: check for overflow?
		return err == nil
	}
	var ftmp float64
	getFloat64 := func() bool {
		ftmp, err = strconv.Atof64(string(data))
		// TODO: check for overflow?
		return err == nil
	}

	// Save accumulated data and comments
	switch t := saveData.(type) {

作者:machinau    项目:go-pla   
func main() {
	pg_adr, err := net.ResolveTCPAddr("152.7.16.113:5432")
	if err != nil {
		log.Exitf("Error while resolving address: %s", err)
	}
	conn, err := net.DialTCP("tcp", nil, pg_adr)
	if err != nil {
		log.Exitf("Error while connecting: %s", err)
	}
	log.Stdout("I think we connected")

	strs := make([]string, 4)
	strs[0] = "user"
	strs[1] = "alex"
	strs[2] = "database"
	strs[3] = "ar_test"
	str2 := strings.Join(strs, "\x00") + "\x00\x00"

	mesg2 := strings.Bytes(str2)
	hmesg := make([]byte, 4+4)
	binary.BigEndian.PutUint32(hmesg[4:8], uint32(3<<16))
	hmesg = bytes.Add(hmesg, mesg2)
	binary.BigEndian.PutUint32(hmesg[0:4], uint32(len(hmesg)))
	n, err := conn.Write(hmesg)

	if err != nil {
		log.Exitf("Error writing TCP: %s", err)
	}
	log.Stdoutf("wrote %d", n)

	result := make([]byte, 12)
	// the largest response we can get is 12 bytes
	if n, err = conn.Read(result); err != nil {
		log.Exitf("Error reading TCP (Read %d bytes): %s", n, err)
	}
	log.Stdoutf("%c", result[0])

	switch string(result[0]) {
	case psql_constants.Authentication:
		log.Stdout("Got authentication message")
	default:
		log.Exit("Did not get authentication message")
	}

	mesglen := binary.BigEndian.Uint32(result[1:5])
	mesgtype := binary.BigEndian.Uint32(result[5:9])

	fmt.Println(mesglen, mesgtype)

	passhash := md5.New()
	passhash.Write(strings.Bytes("jar1!" + "alex"))
	salthash := md5.New()
	salthash.Write(passhash.Sum())
	salthash.Write(result[9:12])

	passresponse := make([]byte, 5)
	passresponse[0] = 'p'
	binary.BigEndian.PutUint32(passresponse[1:5], uint32(4+salthash.Size()+1))
	passresponse = bytes.Add(passresponse, strings.Bytes("md5"))
	passresponse = bytes.Add(passresponse, salthash.Sum())
	passresponse = bytes.AddByte(passresponse, byte(0))
	fmt.Println(passresponse)
	n, err = conn.Write(passresponse)
	if err != nil {
		log.Exitf("Error writing TCP: %s", err)
	}
	log.Stdoutf("wrote %d", n)

	result = make([]byte, 18)
	// the largest response we can get is 12 bytes
	if n, err = conn.Read(result); err != nil {
		log.Exitf("Error reading TCP (Read %d bytes): %s", n, err)
	}
	fmt.Println(result)

	conn.Close()
}

作者:peernod    项目:gobi   
func (c *Client) run(complete chan bool) {
	print("client running\n")
	print("sending handshake\n")
	c.HandShake()
	print("waiting for handshake\n")
	if !c.WaitHandShake() {
		complete <- false
		return
	}

	c.processHandShake(msg)
	blank := make([]byte, 9)
	msg := make([]byte, 49+pstrlen)
	buffer := bytes.NewBuffer(msg)
	buffer.WriteByte(pstrlen)
	buffer.WriteString(c.torrent.Pstr)
	buffer.Write(blank)
	buffer.Write(&(c.torrent.info_hash))
	buffer.Write(&(c.torrent.peer_id))
	_, err = c.conn.Write(buffer.Bytes())
	return

	for {
		print("waiting for msg\n")
		_, err := c.conn.Read(c.buffer[0:4]) //read msg length
		if err != nil {
			complete <- false
			return
		}

		length := binary.BigEndian.Uint32(c.buffer[0:4])
		print("recieved msg length" + strconv.Itoa(int(length)) + "\n")
		switch {
		case length == 0:
			print("keepalive msg")
			continue

		case length >= MaxInMsgSize:
			complete <- true
			return
		}
		_, err = c.conn.Read(c.buffer[4 : 4+length])
		if err != nil {
			complete <- true
			return
		}
		print("msg recieved payload\n")
		msg := new(message)
		msg.length = length
		msg.msgId = c.buffer[4]
		bytes.Add(msg.payLoad, c.buffer[5:len(c.buffer)])
		err = c.processMsg(msg)
		if err != nil {
			print(err.String())
			complete <- true
			return
		}
		runtime.Gosched()

	}

}


问题


面经


文章

微信
公众号

扫码关注公众号