2

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 ?

1

2 Answers 2

8

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.

Sign up to request clarification or add additional context in comments.

Comments

2

If you do intensive I/O you better adapt ByteBuffer for your needs. It works very fast and allows to write and read single byte data or byte array. ByteBuffer is tricky sometimes, be careful )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.