4 package it
.lilik
.capturemjpeg
.utils
;
6 import java
.io
.ByteArrayInputStream
;
9 * A circular buffer of <code>ByteArrayInputStream</code>
11 * @author Alessio Caiazza
12 * @author Cosimo Cecchi
15 public class CircularBuffer
{
18 private ByteArrayInputStream buffer
[];
19 /** last item in <code>CircularBuffer</code> */
21 /** first valid item in <code>CircularBuffer</code> */
23 /** size of <code>CircularBuffer</code> */
26 private boolean empty
;
29 static final int DEFAULT_SIZE
= 5;
34 public CircularBuffer(int size
) {
39 this.buffer
= new ByteArrayInputStream
[size
];
45 public CircularBuffer() {
46 this(CircularBuffer
.DEFAULT_SIZE
);
52 public synchronized int getLastIdx() {
57 * @return the firstIdx
59 public synchronized int getFirstIdx() {
64 * Add a an element to the <code>CircularBuffer</code>
66 * @param data the new element
68 public synchronized void push(ByteArrayInputStream data
) {
69 buffer
[this.lastIdx
] = data
;
70 if( this.lastIdx
== this.firstIdx
&& !this.empty
) {
71 //we have overwritten an element
73 this.firstIdx
%= this.size
;
78 this.lastIdx
%= this.size
;
82 * It counts elements in buffer
83 * @return the number of valid elements
85 public synchronized int getLength() {
88 int len
= this.lastIdx
- this.firstIdx
;
90 len
= this.size
+ len
;
91 return len
== 0 ?
this.size
-1 : len
;
96 * Get the first available element.
98 * @return the first element
99 * @throws IndexOutOfVoundsExceptions
101 public synchronized ByteArrayInputStream
pop() {
103 //TODO:change Exception type
104 throw new IndexOutOfBoundsException("Empty buffer");
106 ByteArrayInputStream res
= buffer
[this.firstIdx
];
107 buffer
[this.firstIdx
] = null;
109 this.firstIdx
%= this.size
;
110 if (this.firstIdx
== this.lastIdx
)
117 * @return the status of the buffer
119 public synchronized boolean isEmpty() {
123 public int getSize() {