• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Golang ioutils.WriteFull函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/reborndb/go/io/ioutils.WriteFull函数的典型用法代码示例。如果您正苦于以下问题:Golang WriteFull函数的具体用法?Golang WriteFull怎么用?Golang WriteFull使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: openSyncConn

func openSyncConn(target string, authCode string) (net.Conn, chan int64) {
	c := openNetConn(target)

	// send auth to master
	if len(authCode) > 0 {
		cmd := fmt.Sprintf("*2\r\n$4\r\nauth\r\n$%d\r\n%s\r\n", len(authCode), authCode)
		if _, err := ioutils.WriteFull(c, []byte(cmd)); err != nil {
			log.PanicError(err, "write auth command failed")
		}

		resp := make([]byte, 5)
		if _, err := io.ReadFull(c, resp); err != nil {
			log.PanicError(err, "read auth response failed")
		}

		if string(resp) != "+OK\r\n" {
			log.Panic("auth failed")
		}

	}

	if _, err := ioutils.WriteFull(c, []byte("*1\r\n$4\r\nsync\r\n")); err != nil {
		log.PanicError(err, "write sync command failed")
	}
	size := make(chan int64)
	go func() {
		var rsp string
		for {
			b := []byte{0}
			if _, err := c.Read(b); err != nil {
				log.PanicErrorf(err, "read sync response = '%s'", rsp)
			}
			if len(rsp) == 0 && b[0] == '\n' {
				size <- 0
				continue
			}
			rsp += string(b)
			if strings.HasSuffix(rsp, "\r\n") {
				break
			}
		}
		if rsp[0] != '$' {
			log.Panicf("invalid sync response, rsp = '%s'", rsp)
		}
		n, err := strconv.Atoi(rsp[1 : len(rsp)-2])
		if err != nil || n <= 0 {
			log.PanicErrorf(err, "invalid sync response = '%s', n = %d", rsp, n)
		}
		size <- int64(n)
	}()
	return c, size
}
开发者ID:hanxirui,项目名称:redis-port,代码行数:52,代码来源:utils.go


示例2: WriteFloat64

func (w *BufWriter) WriteFloat64(f float64) error {
	p := make([]byte, 8)
	bits := float64ToUint64(f)
	binary.BigEndian.PutUint64(p, bits)
	_, err := ioutils.WriteFull(w.w, p)
	return errors.Trace(err)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:7,代码来源:rowbuf.go


示例3: testPipe2

func (s *testIoPipeSuite) testPipe2(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	cc := 1024 * 128
	ss := "Hello world!!"

	go func() {
		for i := 0; i < cc; i++ {
			m := fmt.Sprintf("[%d]%s ", i, ss)
			_, err := ioutils.WriteFull(w, []byte(m))
			c.Assert(err, IsNil)
		}
		c.Assert(w.Close(), IsNil)
	}()

	time.Sleep(time.Millisecond * 100)

	buf := make([]byte, len(ss)*cc*2)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)

	buf = buf[:n]
	for i := 0; i < cc; i++ {
		m := fmt.Sprintf("[%d]%s ", i, ss)
		c.Assert(len(buf) >= len(m), Equals, true)
		c.Assert(string(buf[:len(m)]), Equals, m)
		buf = buf[len(m):]
	}

	c.Assert(len(buf), Equals, 0)
	c.Assert(r.Close(), IsNil)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:32,代码来源:pipe_test.go


示例4: testPipe2

func testPipe2(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	c := 1024 * 128
	s := "Hello world!!"

	go func() {
		for i := 0; i < c; i++ {
			m := fmt.Sprintf("[%d]%s ", i, s)
			_, err := ioutils.WriteFull(w, []byte(m))
			assert.ErrorIsNil(t, err)
		}
		assert.ErrorIsNil(t, w.Close())
	}()

	time.Sleep(time.Millisecond * 100)

	buf := make([]byte, len(s)*c*2)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	buf = buf[:n]
	for i := 0; i < c; i++ {
		m := fmt.Sprintf("[%d]%s ", i, s)
		assert.Must(t, len(buf) >= len(m))
		assert.Must(t, string(buf[:len(m)]) == m)
		buf = buf[len(m):]
	}
	assert.Must(t, len(buf) == 0)
	assert.ErrorIsNil(t, r.Close())
}
开发者ID:shitfSign,项目名称:qdb,代码行数:30,代码来源:pipe_test.go


示例5: TestWriteAfterWriterClose

func (s *testIoPipeSuite) TestWriteAfterWriterClose(c *C) {
	r, w := Pipe()

	ss := "hello"

	errs := make(chan error)

	go func() {
		_, err := ioutils.WriteFull(w, []byte(ss))
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)

		_, err = w.Write([]byte("world"))
		errs <- err
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(string(buf[:n]), Equals, ss)

	err = <-errs
	c.Assert(errors2.ErrorEqual(err, io.ErrClosedPipe), Equals, true)
	c.Assert(r.Close(), IsNil)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:25,代码来源:pipe_test.go


示例6: WriteVarbytes

func (w *BufWriter) WriteVarbytes(p []byte) error {
	if n := uint64(len(p)); n > maxVarbytesLen {
		return errors.Trace(ErrVarbytesLen)
	} else if err := w.WriteUvarint(n); err != nil {
		return errors.Trace(err)
	}
	_, err := ioutils.WriteFull(w.w, p)
	return errors.Trace(err)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:9,代码来源:rowbuf.go


示例7: testPipe3

func (s *testIoPipeSuite) testPipe3(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	ch := make(chan int)

	size := 4096

	go func() {
		buf := make([]byte, size)
		for {
			n, err := r.Read(buf)

			if errors2.ErrorEqual(err, io.EOF) {
				break
			}

			c.Assert(err, IsNil)
			ch <- n
		}

		c.Assert(r.Close(), IsNil)
		ch <- 0
	}()

	go func() {
		buf := make([]byte, size)
		for i := 1; i < size; i++ {
			n, err := ioutils.WriteFull(w, buf[:i])
			c.Assert(err, IsNil)
			c.Assert(n, Equals, i)
		}
		c.Assert(w.Close(), IsNil)
	}()

	sum := 0
	for i := 1; i < size; i++ {
		sum += i
	}
	for {
		n := <-ch
		if n == 0 {
			break
		}
		sum -= n
	}

	c.Assert(sum, Equals, 0)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:48,代码来源:pipe_test.go


示例8: testPipe4

func (s *testIoPipeSuite) testPipe4(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	key := []byte("spinlock aes-128")

	block := aes.BlockSize
	count := 1024 * 1024 * 128 / block

	go func() {
		buf := make([]byte, count*block)
		m, err := aes.NewCipher(key)
		c.Assert(err, IsNil)

		for i := 0; i < len(buf); i++ {
			buf[i] = byte(i)
		}

		e := cipher.NewCBCEncrypter(m, make([]byte, block))
		e.CryptBlocks(buf, buf)

		n, err := ioutils.WriteFull(w, buf)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
		c.Assert(n, Equals, len(buf))
	}()

	buf := make([]byte, count*block)
	m, err := aes.NewCipher(key)
	c.Assert(err, IsNil)

	_, err = ioutils.ReadFull(r, buf)
	c.Assert(err, IsNil)

	e := cipher.NewCBCDecrypter(m, make([]byte, block))
	e.CryptBlocks(buf, buf)

	for i := 0; i < len(buf); i++ {
		// make gocheck faster
		if buf[i] != byte(i) {
			c.Assert(buf[i], Equals, byte(i))
		}
	}

	_, err = ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(r.Close(), IsNil)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:47,代码来源:pipe_test.go


示例9: iocopy

func iocopy(r io.Reader, w io.Writer, p []byte, max int) int {
	if max <= 0 || len(p) == 0 {
		log.Panicf("invalid max = %d, len(p) = %d", max, len(p))
	}
	if len(p) > max {
		p = p[:max]
	}
	if n, err := r.Read(p); err != nil {
		log.PanicError(err, "read error")
	} else {
		p = p[:n]
	}
	if _, err := ioutils.WriteFull(w, p); err != nil {
		log.PanicError(err, "write full error")
	}
	return len(p)
}
开发者ID:hanxirui,项目名称:redis-port,代码行数:17,代码来源:utils.go


示例10: testPipe1

func testPipe1(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	s := "Hello world!!"

	go func(data []byte) {
		_, err := ioutils.WriteFull(w, data)
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
	}([]byte(s))

	buf := make([]byte, 64)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.Must(t, n == len(s))
	assert.Must(t, string(buf[:n]) == s)
	assert.ErrorIsNil(t, r.Close())
}
开发者ID:shitfSign,项目名称:qdb,代码行数:18,代码来源:pipe_test.go


示例11: testPipe1

func (s *testIoPipeSuite) testPipe1(c *C, fileName string) {
	r, w := s.openPipe(c, fileName)

	ss := "Hello world!!"

	go func(data []byte) {
		_, err := ioutils.WriteFull(w, data)
		c.Assert(err, IsNil)
		c.Assert(w.Close(), IsNil)
	}([]byte(ss))

	buf := make([]byte, 64)
	n, err := ioutils.ReadFull(r, buf)
	c.Assert(errors2.ErrorEqual(err, io.EOF), Equals, true)
	c.Assert(n, Equals, len(ss))
	c.Assert(string(buf[:n]), Equals, ss)
	c.Assert(r.Close(), IsNil)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:18,代码来源:pipe_test.go


示例12: testPipe3

func testPipe3(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	c := make(chan int)

	size := 4096

	go func() {
		buf := make([]byte, size)
		for {
			n, err := r.Read(buf)
			if errors.Equal(err, io.EOF) {
				break
			}
			assert.ErrorIsNil(t, err)
			c <- n
		}
		assert.ErrorIsNil(t, r.Close())
		c <- 0
	}()

	go func() {
		buf := make([]byte, size)
		for i := 1; i < size; i++ {
			n, err := ioutils.WriteFull(w, buf[:i])
			assert.ErrorIsNil(t, err)
			assert.Must(t, n == i)
		}
		assert.ErrorIsNil(t, w.Close())
	}()

	sum := 0
	for i := 1; i < size; i++ {
		sum += i
	}
	for {
		n := <-c
		if n == 0 {
			break
		}
		sum -= n
	}
	assert.Must(t, sum == 0)
}
开发者ID:shitfSign,项目名称:qdb,代码行数:44,代码来源:pipe_test.go


示例13: testPipe4

func testPipe4(t *testing.T, fileName string) {
	r, w := openPipe(t, fileName)

	key := []byte("spinlock aes-128")

	block := aes.BlockSize
	count := 1024 * 1024 * 128 / block

	go func() {
		buf := make([]byte, count*block)
		m, err := aes.NewCipher(key)
		assert.ErrorIsNil(t, err)
		for i := 0; i < len(buf); i++ {
			buf[i] = byte(i)
		}

		e := cipher.NewCBCEncrypter(m, make([]byte, block))
		e.CryptBlocks(buf, buf)

		n, err := ioutils.WriteFull(w, buf)
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
		assert.Must(t, n == len(buf))
	}()

	buf := make([]byte, count*block)
	m, err := aes.NewCipher(key)
	assert.ErrorIsNil(t, err)

	_, err = ioutils.ReadFull(r, buf)
	assert.ErrorIsNil(t, err)

	e := cipher.NewCBCDecrypter(m, make([]byte, block))
	e.CryptBlocks(buf, buf)

	for i := 0; i < len(buf); i++ {
		assert.Must(t, buf[i] == byte(i))
	}
	_, err = ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.ErrorIsNil(t, r.Close())
}
开发者ID:shitfSign,项目名称:qdb,代码行数:42,代码来源:pipe_test.go


示例14: TestPipeWriteClose

func TestPipeWriteClose(t *testing.T) {
	for _, u := range pipeTests {
		r, w := Pipe()
		c := make(chan int, 1)

		if u.async {
			go delayClose(t, r, c, u)
		} else {
			delayClose(t, r, c, u)
		}
		<-c

		n, err := ioutils.WriteFull(w, []byte("hello, world"))
		expect := u.err
		if expect == nil {
			expect = io.ErrClosedPipe
		}
		assert.Must(t, errors.Equal(err, expect))
		assert.Must(t, n == 0)
		assert.ErrorIsNil(t, w.Close())
	}
}
开发者ID:shitfSign,项目名称:qdb,代码行数:22,代码来源:pipe_test.go


示例15: TestPipeWriteClose

func (s *testIoPipeSuite) TestPipeWriteClose(c *C) {
	for _, u := range pipeTests {
		r, w := Pipe()
		ch := make(chan int, 1)

		if u.async {
			go s.delayClose(c, r, ch, u)
		} else {
			s.delayClose(c, r, ch, u)
		}
		<-ch

		n, err := ioutils.WriteFull(w, []byte("hello, world"))
		expect := u.err
		if expect == nil {
			expect = io.ErrClosedPipe
		}

		c.Assert(errors2.ErrorEqual(err, expect), Equals, true)

		c.Assert(n, Equals, 0)
		c.Assert(w.Close(), IsNil)
	}
}
开发者ID:CowLeo,项目名称:qdb,代码行数:24,代码来源:pipe_test.go


示例16: TestWriteAfterWriterClose

func TestWriteAfterWriterClose(t *testing.T) {
	r, w := Pipe()

	s := "hello"

	errs := make(chan error)

	go func() {
		_, err := ioutils.WriteFull(w, []byte(s))
		assert.ErrorIsNil(t, err)
		assert.ErrorIsNil(t, w.Close())
		_, err = w.Write([]byte("world"))
		errs <- err
	}()

	buf := make([]byte, 4096)
	n, err := ioutils.ReadFull(r, buf)
	assert.Must(t, errors.Equal(err, io.EOF))
	assert.Must(t, string(buf[:n]) == s)

	err = <-errs
	assert.Must(t, errors.Equal(err, io.ErrClosedPipe))
	assert.ErrorIsNil(t, r.Close())
}
开发者ID:shitfSign,项目名称:qdb,代码行数:24,代码来源:pipe_test.go


示例17: WriteBytes

func (w *BufWriter) WriteBytes(p []byte) error {
	_, err := ioutils.WriteFull(w.w, p)
	return errors.Trace(err)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:4,代码来源:rowbuf.go


示例18: WriteUint64

func (w *BufWriter) WriteUint64(s uint64) error {
	p := make([]byte, 8)
	binary.BigEndian.PutUint64(p, uint64(s))
	_, err := ioutils.WriteFull(w.w, p)
	return errors.Trace(err)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:6,代码来源:rowbuf.go


示例19: WriteUvarint

func (w *BufWriter) WriteUvarint(v uint64) error {
	p := make([]byte, binary.MaxVarintLen64)
	n := binary.PutUvarint(p, v)
	_, err := ioutils.WriteFull(w.w, p[:n])
	return errors.Trace(err)
}
开发者ID:CowLeo,项目名称:qdb,代码行数:6,代码来源:rowbuf.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang resp.NewArray函数代码示例发布时间:2022-05-28
下一篇:
Golang errors2.ErrorEqual函数代码示例发布时间:2022-05-28
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap