File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -557,8 +557,17 @@ func (b *LinkBuffer) Bytes() []byte {
557
557
}
558
558
559
559
// GetBytes will read and fill the slice p as much as possible.
560
+ // If p is not passed, return all readable bytes.
560
561
func (b * LinkBuffer ) GetBytes (p [][]byte ) (vs [][]byte ) {
561
562
node , flush := b .read , b .flush
563
+ if len (p ) == 0 {
564
+ n := 0
565
+ for ; node != flush ; node = node .next {
566
+ n ++
567
+ }
568
+ node = b .read
569
+ p = make ([][]byte , n )
570
+ }
562
571
var i int
563
572
for i = 0 ; node != flush && i < len (p ); node = node .next {
564
573
if node .Len () > 0 {
Original file line number Diff line number Diff line change @@ -599,10 +599,19 @@ func (b *LinkBuffer) Bytes() []byte {
599
599
}
600
600
601
601
// GetBytes will read and fill the slice p as much as possible.
602
+ // If p is not passed, return all readable bytes.
602
603
func (b * LinkBuffer ) GetBytes (p [][]byte ) (vs [][]byte ) {
603
604
b .Lock ()
604
605
defer b .Unlock ()
605
606
node , flush := b .read , b .flush
607
+ if len (p ) == 0 {
608
+ n := 0
609
+ for ; node != flush ; node = node .next {
610
+ n ++
611
+ }
612
+ node = b .read
613
+ p = make ([][]byte , n )
614
+ }
606
615
var i int
607
616
for i = 0 ; node != flush && i < len (p ); node = node .next {
608
617
if node .Len () > 0 {
Original file line number Diff line number Diff line change @@ -84,6 +84,31 @@ func TestLinkBuffer(t *testing.T) {
84
84
Equal (t , buf .Len (), 100 )
85
85
}
86
86
87
+ func TestGetBytes (t * testing.T ) {
88
+ buf := NewLinkBuffer ()
89
+ var (
90
+ num = 10
91
+ b = 1
92
+ expectedLen = 0
93
+ )
94
+ for i := 0 ; i < num ; i ++ {
95
+ expectedLen += b
96
+ n , err := buf .WriteBinary (make ([]byte , b ))
97
+ MustNil (t , err )
98
+ Equal (t , n , b )
99
+ b *= 10
100
+ }
101
+ buf .Flush ()
102
+ Equal (t , int (buf .length ), expectedLen )
103
+ bs := buf .GetBytes (nil )
104
+ actualLen := 0
105
+ for i := 0 ; i < len (bs ); i ++ {
106
+ actualLen += len (bs [i ])
107
+ }
108
+ Equal (t , actualLen , expectedLen )
109
+
110
+ }
111
+
87
112
// TestLinkBufferWithZero test more case with n is invalid.
88
113
func TestLinkBufferWithInvalid (t * testing.T ) {
89
114
// clean & new
You can’t perform that action at this time.
0 commit comments