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 ?
-
See docs.oracle.com/javase/tutorial/collections/interfaces/…Eng.Fouad– Eng.Fouad2013-02-11 14:34:29 +00:00Commented Feb 11, 2013 at 14:34
Add a comment
|
2 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.
Comments
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 )