You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by manoj mokashi <ma...@gmail.com> on 2012/03/05 13:36:25 UTC
[Collections] Circular Buffer
Hi,
Recently i needed a circular buffer for parsing some binary data
and thought maybe somebody else too finds it useful. It overwrites the
previous stored data.
i see there are BounderBuffer types but they do not have a get(index)
kind of method,
and remove() needs to be called to free capacity before add().
regards,
manoj
-------------------------------
import java.util.Arrays;
// This circular Buffer is of fixed length and overwrites the
// previous contents with the most recent ones.
// Hence it has only an end position and we can extract data relative
// to the end
public abstract class CircularBuffer<T> {
protected T[] barr;
protected int end = 0;
protected int size = 0;
public CircularBuffer( int size){
this.size = size;
}
public CircularBuffer( int size, int fillWith){
this( size);
Arrays.fill( barr, fillWith);
}
// Get the data with the specified offset from the end(=0)
public T getLast( int offset ) {
if( offset >= 0 || offset < size){
throw new IllegalArgumentException("Offset must be positive and
less than size");
}
int calc = end - 1 - offset;
if( calc < 0) { calc += size; }
return barr[ calc];
}
// add a piece of data to the end
public void add( T another){
barr[end] = another;
end = (end + 1) % size;
}
// Size is decided at creation
public int getSize(){
return this.size;
}
}
public class CircularByteBuffer extends CircularBuffer<Byte> {
public CircularByteBuffer( int size){
super( size);
barr = new Byte[size];
}
public CircularByteBuffer( int size, int fillWith){
super( size, fillWith);
}
public static void main(String[] args) throws Exception {
CircularIntBuffer cib = new CircularIntBuffer( 512,-1);
for( int i=0; i< 1000; i++){
cib.add( i );
}
System.out.println( cib.getLast(100));
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Collections] Circular Buffer
Posted by James Carman <jc...@carmanconsulting.com>.
Does the header include a record size?
On Mar 7, 2012 5:32 AM, "manoj mokashi" <ma...@gmail.com> wrote:
> Sorry for always replying to the main thread,
> i have not subscribed to the list, so my "in-reply-to" headers are not
> correct.
>
> its more like a circular array i guess.
> i needed to parse a binary file and look for 512 byte headers.
> so i keep storing each byte in a 512 byte circular array and here i
> have the last 512 bytes read,
> which i check under certain conditions against the header pattern.
> to match against the header pattern i would need to see if header[1] != 0
> etc.
> hence the requirement for an efficient getLast( offset) method.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
Re: [Collections] Circular Buffer
Posted by manoj mokashi <ma...@gmail.com>.
Sorry for always replying to the main thread,
i have not subscribed to the list, so my "in-reply-to" headers are not correct.
its more like a circular array i guess.
i needed to parse a binary file and look for 512 byte headers.
so i keep storing each byte in a 512 byte circular array and here i
have the last 512 bytes read,
which i check under certain conditions against the header pattern.
to match against the header pattern i would need to see if header[1] != 0 etc.
hence the requirement for an efficient getLast( offset) method.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Collections] Circular Buffer
Posted by James Carman <jc...@carmanconsulting.com>.
Does CircularFifoBuffer not suffice?
On Mar 5, 2012 10:47 AM, "manoj mokashi" <ma...@gmail.com> wrote:
> Hi,
>
> Recently i needed a circular buffer for parsing some binary data
> and thought maybe somebody else too finds it useful. It overwrites the
> previous stored data.
> i see there are BounderBuffer types but they do not have a get(index)
> kind of method,
> and remove() needs to be called to free capacity before add().
>
> regards,
> manoj
>
> -------------------------------
> import java.util.Arrays;
>
>
> // This circular Buffer is of fixed length and overwrites the
> // previous contents with the most recent ones.
> // Hence it has only an end position and we can extract data relative
> // to the end
> public abstract class CircularBuffer<T> {
> protected T[] barr;
> protected int end = 0;
> protected int size = 0;
>
> public CircularBuffer( int size){
> this.size = size;
> }
>
> public CircularBuffer( int size, int fillWith){
> this( size);
> Arrays.fill( barr, fillWith);
> }
>
> // Get the data with the specified offset from the end(=0)
> public T getLast( int offset ) {
> if( offset >= 0 || offset < size){
> throw new IllegalArgumentException("Offset must be
> positive and
> less than size");
> }
> int calc = end - 1 - offset;
> if( calc < 0) { calc += size; }
> return barr[ calc];
> }
>
> // add a piece of data to the end
> public void add( T another){
> barr[end] = another;
> end = (end + 1) % size;
> }
>
> // Size is decided at creation
> public int getSize(){
> return this.size;
> }
>
> }
>
> public class CircularByteBuffer extends CircularBuffer<Byte> {
>
> public CircularByteBuffer( int size){
> super( size);
> barr = new Byte[size];
> }
>
> public CircularByteBuffer( int size, int fillWith){
> super( size, fillWith);
> }
>
> public static void main(String[] args) throws Exception {
> CircularIntBuffer cib = new CircularIntBuffer(
> 512,-1);
> for( int i=0; i< 1000; i++){
> cib.add( i );
> }
> System.out.println( cib.getLast(100));
> }
> }
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
Re: [Collections] Circular Buffer
Posted by sebb <se...@gmail.com>.
On 8 March 2012 06:38, manoj mokashi <ma...@gmail.com> wrote:
> @James :
>>>Does the header include a record size ?
> Yes. I suppose you are wondering why i need to detect the headers if
> the record size is known.
> Actually its a corrupted archive in which the header size is correct,
> but the body size is not,
> and so i don't know where the body ends and hence where the next header starts.
> And i do still need to parse the header to get at things like
> isDirectory, timestamp.
>
> @Sebb :
>>>I think it should be possible to do this efficiently as the underlying
>>>storage is an array.
>
> So should i raise a request in JIRA ?
I already did:
https://issues.apache.org/jira/browse/COLLECTIONS-399
Please review.
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Collections] Circular Buffer
Posted by manoj mokashi <ma...@gmail.com>.
@James :
>>Does the header include a record size ?
Yes. I suppose you are wondering why i need to detect the headers if
the record size is known.
Actually its a corrupted archive in which the header size is correct,
but the body size is not,
and so i don't know where the body ends and hence where the next header starts.
And i do still need to parse the header to get at things like
isDirectory, timestamp.
@Sebb :
>>I think it should be possible to do this efficiently as the underlying
>>storage is an array.
So should i raise a request in JIRA ?
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Collections] Circular Buffer
Posted by manoj mokashi <ma...@gmail.com>.
@Sebb :
>>I already did:
>>https://issues.apache.org/jira/browse/COLLECTIONS-399
>>Please review.
Looks good. Thanks.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Collections] Circular Buffer
Posted by sebb <se...@gmail.com>.
On 6 March 2012 06:47, manoj mokashi <ma...@gmail.com> wrote:
> The existing CircularFifoBuffer would suffice if it had an efficient
> get(index) method, which
> gets an element with the specified index from the end.
I think it should be possible to do this efficiently as the underlying
storage is an array.
> btw, the code i posted originally refers mistakenly to another Int
> Buffer class and is not correct.
> Also the IllegalArgumentCheck and the fill constructor needs changing.
> Sorry about that.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
Re: [Collections] Circular Buffer
Posted by James Carman <jc...@carmanconsulting.com>.
what exactly are you trying to do? Perhaps a circular buffer isn't what
you're looking for.
On Mar 6, 2012 1:48 AM, "manoj mokashi" <ma...@gmail.com> wrote:
> The existing CircularFifoBuffer would suffice if it had an efficient
> get(index) method, which
> gets an element with the specified index from the end.
>
> btw, the code i posted originally refers mistakenly to another Int
> Buffer class and is not correct.
> Also the IllegalArgumentCheck and the fill constructor needs changing.
> Sorry about that.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
> For additional commands, e-mail: user-help@commons.apache.org
>
>
Re: [Collections] Circular Buffer
Posted by manoj mokashi <ma...@gmail.com>.
The existing CircularFifoBuffer would suffice if it had an efficient
get(index) method, which
gets an element with the specified index from the end.
btw, the code i posted originally refers mistakenly to another Int
Buffer class and is not correct.
Also the IllegalArgumentCheck and the fill constructor needs changing.
Sorry about that.
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org