From 382d83c701def2f500d415c13262b0cce26bc8c0 Mon Sep 17 00:00:00 2001 From: Saxon1 Date: Mon, 4 Dec 2017 13:07:37 +1030 Subject: [PATCH] Created IsWritable function and also changed name of CanRead() to IsReadable() --- ringbuffer/RingBuffer.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/ringbuffer/RingBuffer.go b/ringbuffer/RingBuffer.go index 2372131c..bb7d8ec3 100644 --- a/ringbuffer/RingBuffer.go +++ b/ringbuffer/RingBuffer.go @@ -81,7 +81,7 @@ a call to Get without an accompanying call to DoneWriting. func (rb *ringBuffer) Get() ([]byte, error) { rb.mutex.Lock() defer rb.mutex.Unlock() - if rb.noOfElements == rb.size { + if !rb.IsWritable() { return nil, errors.New("Buffer full!") } if rb.currentlyWriting { @@ -129,7 +129,7 @@ second call to Read before a call to DoneReading. func (rb *ringBuffer) Read() ([]byte, error) { rb.mutex.Lock() defer rb.mutex.Unlock() - if !rb.CanRead() { + if !rb.IsReadable() { return nil, errors.New("Buffer is empty, nothging to read!") } if rb.currentlyReading { @@ -160,12 +160,23 @@ func (rb *ringBuffer) DoneReading() error { } /* -CanRead returns true if it is possible to read from the buffer, i.e. if +IsReadable returns true if it is possible to read from the buffer, i.e. if it is not empty. */ -func (rb *ringBuffer) CanRead() bool { +func (rb *ringBuffer) IsReadable() bool { if rb.first == -1 || rb.noOfElements == 0 { return false } return true } + +/* +IsWritable returns true if it is possible to write to the buffer, i.e. if +it is not full. +*/ +func (rb *ringBuffer) IsWritable() bool { + if rb.noOfElements == rb.size { + return false + } + return true +}