/** * Nonblocking get the next object * * @return null of there is no data or the next available object */ public T read() { synchronized (access) { if (rpos == wpos) return null; return get(); } }
/** * Put all the bytes to the buffer. Blocks until the operation is completed. * * @param collection */ public void put(Iterable<T> collection) { for (T x : collection) { put(x); } }
/** * Get all data available from the buffer * @return */ public ArrayList<T> readAll() { synchronized (access) { int size = getAvailable(); ArrayList<T> result = new ArrayList<>(); for( int i=0; i<size; i++) result.add(get()); return result; } }
/** * Get the next object, block until available. */ public T get() { synchronized (access) { while (rpos == wpos) try { access.wait(); } catch (InterruptedException e) { } Object result = data[rpos]; rpos = advance(rpos); access.notifyAll(); return (T) result; } }