BufferSkipObserver(Observer<? super U> actual, int count, int skip, Callable<U> bufferSupplier) { this.downstream = actual; this.count = count; this.skip = skip; this.bufferSupplier = bufferSupplier; this.buffers = new ArrayDeque<U>(); }
/** * Returns a flattened list of Throwables from tree-like CompositeException chain. * @param t the starting throwable * @return the list of Throwables flattened in a depth-first manner */ public static List<Throwable> flatten(Throwable t) { List<Throwable> list = new ArrayList<Throwable>(); ArrayDeque<Throwable> deque = new ArrayDeque<Throwable>(); deque.offer(t); while (!deque.isEmpty()) { Throwable e = deque.removeFirst(); if (e instanceof CompositeException) { CompositeException ce = (CompositeException) e; List<Throwable> exceptions = ce.getExceptions(); for (int i = exceptions.size() - 1; i >= 0; i--) { deque.offerFirst(exceptions.get(i)); } } else { list.add(e); } } return list; }
public DefaultBlockingPool( Supplier<T> generator, int limit ) { this.objects = new ArrayDeque<>(limit); this.maxSize = limit; for (int i = 0; i < limit; i++) { objects.add(generator.get()); } this.lock = new ReentrantLock(); this.notEnough = lock.newCondition(); }
@Override public AsyncResult poll() throws InterruptedException { lock.lockInterruptibly(); try { while (queue.isEmpty() || !queue.peek().isDone()) { headIsCompleted.await(); } notFull.signalAll(); LOG.debug("Polled head element from ordered stream element queue. New filling degree " + "({}/{}).", queue.size() - 1, capacity); return queue.poll(); } finally { lock.unlock(); } }
private void processAtomEnded(long atomEndPosition) throws ParserException { while (!containerAtoms.isEmpty() && containerAtoms.peek().endPosition == atomEndPosition) { Atom.ContainerAtom containerAtom = containerAtoms.pop(); if (containerAtom.type == Atom.TYPE_moov) { // We've reached the end of the moov atom. Process it and prepare to read samples. processMoovAtom(containerAtom); containerAtoms.clear(); parserState = STATE_READING_SAMPLE; } else if (!containerAtoms.isEmpty()) { containerAtoms.peek().add(containerAtom); } } if (parserState != STATE_READING_SAMPLE) { enterReadingAtomHeaderState(); } }
/** * @return true if this iterator has a "next" element */ @Override public boolean hasNext() { return !pathStack.isEmpty(); }
@Override public AsyncResult peekBlockingly() throws InterruptedException { lock.lockInterruptibly(); try { while (queue.isEmpty() || !queue.peek().isDone()) { headIsCompleted.await(); } LOG.debug("Peeked head element from ordered stream element queue with filling degree " + "({}/{}).", queue.size(), capacity); return queue.peek(); } finally { lock.unlock(); } }
@Override public void add(BufferOrEvent boe) { bytesBlocked += pageSize; currentBuffers.add(boe); }
/** * Get the number of elements in this queue added via one of the {@link #add(ByteBuf)} methods. * @return the number of elements in this queue. */ protected final int size() { return bufAndListenerPairs.size(); }
@Override public void onNext(T t) { final ArrayDeque<UnicastSubject<T>> ws = windows; long i = index; long s = skip; if (i % s == 0 && !cancelled) { wip.getAndIncrement(); UnicastSubject<T> w = UnicastSubject.create(capacityHint, this); ws.offer(w); downstream.onNext(w); } long c = firstEmission + 1; for (UnicastSubject<T> w : ws) { w.onNext(t); } if (c >= count) { ws.poll().onComplete(); if (ws.isEmpty() && cancelled) { this.upstream.dispose(); return; } firstEmission = c - s; } else { firstEmission = c; } index = i + 1; }
private void onContainerAtomRead(ContainerAtom container) throws ParserException { if (container.type == Atom.TYPE_moov) { onMoovContainerAtomRead(container); } else if (container.type == Atom.TYPE_moof) { onMoofContainerAtomRead(container); } else if (!containerAtoms.isEmpty()) { containerAtoms.peek().add(container); } }
@Override public final O dequeueOutputBuffer() throws E { synchronized (lock) { maybeThrowException(); if (queuedOutputBuffers.isEmpty()) { return null; } return queuedOutputBuffers.removeFirst(); } }
@Override void handleClose() { if (pendingPushes.remove(this)) { completionHandler.fail("Push reset by client"); } else { concurrentStreams--; while ((maxConcurrentStreams == null || concurrentStreams < maxConcurrentStreams) && pendingPushes.size() > 0) { Push push = pendingPushes.pop(); concurrentStreams++; context.runOnContext(v -> { push.complete(); }); } response.handleClose(); } }
private List<T> takeObjects(int elementNum) throws InterruptedException { final List<T> list = new ArrayList<>(elementNum); final ReentrantLock lock = this.lock; lock.lockInterruptibly(); try { while (objects.size() < elementNum) { notEnough.await(); } for (int i = 0; i < elementNum; i++) { list.add(objects.pop()); } return list; } finally { lock.unlock(); } }
@Override public Bitmap get(int width, int height, Bitmap.Config config) { return bitmaps.isEmpty() ? null : bitmaps.removeLast(); }