Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android/Java simple FiFO buffer

I looking for simple byte FiFo buffer. I have to put and get or a single byte or array. But I can put every time single byte and get array and vice versa.
Any idea or example code to help me ?

like image 617
Nuccio Epifanio Avatar asked Dec 13 '25 13:12

Nuccio Epifanio


1 Answers

You can use a LinkedList as a queue:

    Queue<String> qe=new LinkedList<String>();

    qe.add("b");
    qe.add("a");
    qe.add("c");
    qe.add("e");
    qe.add("d");

    Iterator it=qe.iterator();

    System.out.println("Initial Size of Queue :"+qe.size());

    while(it.hasNext())
    {
        String iteratorValue=(String)it.next();
        System.out.println("Queue Next Value :"+iteratorValue);
    }

    // get value and does not remove element from queue
    System.out.println("Queue peek :"+qe.peek());

    // get first value and remove that object from queue
    System.out.println("Queue poll :"+qe.poll());

    System.out.println("Final Size of Queue :"+qe.size());

If you also want to add priorities you can use a PriorityQueue

If you need it to be thread safe use ConcurrentLinkedQueue

Also, as @Leonidos says, you can use a ByteBuffer is you need low level I/O, but be careful.

Feel free to comment on the post if you need any clarifications on how to use them.

like image 181
user000001 Avatar answered Dec 15 '25 06:12

user000001



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!