Essentially a circular buffer is a buffer which holds both read and write pointers and behaves in circular manner. Circular buffer seems to have no limit, once the real limit is reached, pointers resettled back to 0. Now everything in between read pointer and write pointer is the actual readable data. Simple implementation of circular buffer would look similar to this: #4021412 - Pastie My idea of implementation would be to use single buffer for all players for both input and output, since it's circular we don't need to care about resetting/clearing the buffer. Problem occurs only if we have two worker threads that are dealing with input and output in parallel, in that case two buffers per server should take place. Ideas on this? What would be pros and cons?
So, can this replace all buffers?
Yes Bot, but that doesn't mean it's always the best choice. I've always used separate buffers (which aren't always the best choice either, I'm well aware of this). Also, an interesting idea - what if you did some fancy Vector work instead of using an array of bytes?
Under the hood vector just copies array elements to a new array whenever it needs more capacity. I don't see a point to use vector in this case as we want to access buffer efficiently.
Vectors are outdated and synchronize everything. If you want a self-expanding collection use one of the util collections (LinkedList, ArrayList, ...) and use Collections.synchronizedList if you need a synchronized one. The only reason Vector is still in use is because some older API relies on it. Circular buffers are cool, but they will overwrite old data at an overflow, so it's quite dangerous.
Collections.synchronizedList is also out-dated. In one of google's java sessions when people discussed about conccurent solutions, they said that the best approach is to use java.util.concurrent package when possible as synchronized block is rather slow. For a concurrent circular buffer I'd use CopyOnWriteArrayList, but then again a concurrent buffer implementation is really hard to design and write, I see no use for it in RSPS.
CopyOnWriteArrayList for a buffer? With all the writing? I just wouldn't use a list either way and I don't think a buffer should be concurrently shared anyway.