2

I have this FIFO which I'm going to use to store data from network server:

 Buffer nServerFifo = (Buffer) BufferUtils.synchronizedBuffer(new CircularFifoBuffer(200));

    // Insert into to the Network Server Buffer    
    public void nServerFifoAdd(String string){
        nServerFifo.add(string);        
    }

    // Get data from the Network Server Buffer
    public Object nServerFifoGet(){         
        Object string = nServerFifo.get();    
        nServerFifo.remove(string);
        return string;           
    }

My question is what is the proper way to store data insert and get data from the buffer? Do I need to delete the data after I get if or this is done by the buffer? Do you have Idea what is the maximum String length size that I can store into the buffer?

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
user1285928
  • 1,328
  • 29
  • 98
  • 147

2 Answers2

3

Its Better use ArrayBlockingQueue class which is present in java.util.concurrent package, which is Thread Safe.

BlockingQueue<String> queue = new ArrayBlockingQueue<String>(100);

queue.put("Vivek");   // To insert value into Queue

queue.take();         // To take the value out of Queue
Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75
1

Assuming you're using the classes from Commons Collections:

  1. If you use the remove() method, you do not need to additionally delete anything
  2. The buffer stores object references, not strings, so it is limited by the number of slots allocated. In your case you could add up to 200 strings, of arbitrary length, limited by the total memory available to your program, before the buffer is full.

As Kumar pointed out, you're probably better off using the Java runtime library queueing classes instead, as they handle synchronization for you while the Commons Collections classes require you to lock on the iterator yourself.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190