java - Circular buffer implementation in Android -


how can achieve circular buffer implementation in android?

is there way can re-use pre-defined method if exists? or have support c standard libraries in android?

in android development first preference use java rather c implementing these things. ofcourse can in c (using jni) requires overheads i.e. need implement own garbage collection logic along code of circular buffer whereas in java can achieved automatically. . see below class if works case..

import java.nio.bufferoverflowexception; import java.nio.bufferunderflowexception;  public class customcircularbuffer<t> {    private t[] buffer;    private int tail;    private int head;     public customcircularbuffer(int n) {     buffer = (t[]) new object[n];     tail = 0;     head = 0;   }    public void add(t toadd) {     if (head != (tail - 1)) {         buffer[head++] = toadd;     } else {         throw new bufferoverflowexception();     }     head = head % buffer.length;   }    public t get() {     t t = null;     int adjtail = tail > head ? tail - buffer.length : tail;     if (adjtail < head) {         t = (t) buffer[tail++];         tail = tail % buffer.length;     } else {         throw new bufferunderflowexception();     }     return t;   }    public string tostring() {     return "customcircularbuffer(size=" + buffer.length + ", head=" + head + ", tail=" + tail + ")";   } } 

here other useful links can give necessary explanations ..

example

another example

in depth article


Comments

Post a Comment