1
0
mirror of https://github.com/golang/go synced 2024-11-12 07:00:21 -07:00

If ByteBuffer has never been used, b.buf is nil but Data() should still work.

Fix the bug using a (safe) shared global empty array.

R=rsc
DELTA=8  (8 added, 0 deleted, 0 changed)
OCL=21303
CL=21303
This commit is contained in:
Rob Pike 2008-12-16 13:01:39 -08:00
parent 30a1a8c922
commit a10267adcd

View File

@ -75,7 +75,15 @@ func (b *ByteBuffer) Len() int {
return b.len
}
// If the buffer is empty, Data() should still give a valid array.
// Use this variable as a surrogate. It's immutable (can't be
// grown, can't store any data) so it's safe to share.
var EmptyByteArray = new([]byte, 0)
func (b *ByteBuffer) Data() *[]byte {
if b.buf == nil {
return EmptyByteArray
}
return b.buf[b.off:b.len]
}