|
|
@ -20,6 +20,7 @@ type StarBuffer struct {
|
|
|
|
pEnd uint64
|
|
|
|
pEnd uint64
|
|
|
|
cap uint64
|
|
|
|
cap uint64
|
|
|
|
isClose atomic.Value
|
|
|
|
isClose atomic.Value
|
|
|
|
|
|
|
|
isEnd atomic.Value
|
|
|
|
rmu sync.Mutex
|
|
|
|
rmu sync.Mutex
|
|
|
|
wmu sync.Mutex
|
|
|
|
wmu sync.Mutex
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -29,6 +30,7 @@ func NewStarBuffer(cap uint64) *StarBuffer {
|
|
|
|
rtnBuffer.cap = cap
|
|
|
|
rtnBuffer.cap = cap
|
|
|
|
rtnBuffer.datas = make([]byte, cap)
|
|
|
|
rtnBuffer.datas = make([]byte, cap)
|
|
|
|
rtnBuffer.isClose.Store(false)
|
|
|
|
rtnBuffer.isClose.Store(false)
|
|
|
|
|
|
|
|
rtnBuffer.isEnd.Store(false)
|
|
|
|
return rtnBuffer
|
|
|
|
return rtnBuffer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -48,7 +50,7 @@ func (star *StarBuffer) Len() uint64 {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarBuffer) getByte() (byte, error) {
|
|
|
|
func (star *StarBuffer) getByte() (byte, error) {
|
|
|
|
if star.isClose.Load().(bool) {
|
|
|
|
if star.isClose.Load().(bool) || (star.Len() == 0 && star.isEnd.Load().(bool)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if star.Len() == 0 {
|
|
|
|
if star.Len() == 0 {
|
|
|
@ -97,7 +99,7 @@ func (star *StarBuffer) Close() error {
|
|
|
|
return nil
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (star *StarBuffer) Read(buf []byte) (int, error) {
|
|
|
|
func (star *StarBuffer) Read(buf []byte) (int, error) {
|
|
|
|
if star.isClose.Load().(bool) {
|
|
|
|
if star.isClose.Load().(bool) || (star.Len() == 0 && star.isEnd.Load().(bool)) {
|
|
|
|
return 0, io.EOF
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if buf == nil {
|
|
|
|
if buf == nil {
|
|
|
@ -125,6 +127,10 @@ func (star *StarBuffer) Read(buf []byte) (int, error) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (star *StarBuffer) Write(bts []byte) (int, error) {
|
|
|
|
func (star *StarBuffer) Write(bts []byte) (int, error) {
|
|
|
|
|
|
|
|
if bts == nil && !star.isEnd.Load().(bool) {
|
|
|
|
|
|
|
|
star.isEnd.Store(true)
|
|
|
|
|
|
|
|
return 0, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
if bts == nil || star.isClose.Load().(bool) {
|
|
|
|
if bts == nil || star.isClose.Load().(bool) {
|
|
|
|
return 0, io.EOF
|
|
|
|
return 0, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|