2 // ========================================================================
3 // Copyright (c) 1995-2014 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.server;
21 import java.io.IOException;
22 import java.io.InterruptedIOException;
24 import org.eclipse.jetty.util.ArrayQueue;
25 import org.eclipse.jetty.util.log.Log;
26 import org.eclipse.jetty.util.log.Logger;
29 * {@link QueuedHttpInput} holds a queue of items passed to it by calls to {@link #content(Object)}.
31 * {@link QueuedHttpInput} stores the items directly; if the items contain byte buffers, it does not copy them
32 * but simply holds references to the item, thus the caller must organize for those buffers to valid while
35 * To assist the caller, subclasses may override methods {@link #onAsyncRead()}, {@link #onContentConsumed(Object)}
36 * that can be implemented so that the caller will know when buffers are queued and consumed.
38 public abstract class QueuedHttpInput<T> extends HttpInput<T>
40 private final static Logger LOG = Log.getLogger(QueuedHttpInput.class);
42 private final ArrayQueue<T> _inputQ = new ArrayQueue<>(lock());
44 public QueuedHttpInput()
48 public void content(T item)
50 // The buffer is not copied here. This relies on the caller not recycling the buffer
51 // until the it is consumed. The onContentConsumed and onAllContentConsumed() callbacks are
52 // the signals to the caller that the buffers can be recycled.
56 boolean wasEmpty = _inputQ.isEmpty();
58 LOG.debug("{} queued {}", this, item);
71 T item = _inputQ.pollUnsafe();
74 onContentConsumed(item);
75 item = _inputQ.pollUnsafe();
82 protected T nextContent()
86 // Items are removed only when they are fully consumed.
87 T item = _inputQ.peekUnsafe();
88 // Skip consumed items at the head of the queue.
89 while (item != null && remaining(item) == 0)
92 onContentConsumed(item);
93 LOG.debug("{} consumed {}", this, item);
94 item = _inputQ.peekUnsafe();
100 protected void blockForContent() throws IOException
102 synchronized (lock())
104 while (_inputQ.isEmpty() && !isFinished() && !isEOF())
108 LOG.debug("{} waiting for content", this);
111 catch (InterruptedException e)
113 throw (IOException)new InterruptedIOException().initCause(e);
120 * Callback that signals that the given content has been consumed.
122 * @param item the consumed content
124 protected abstract void onContentConsumed(T item);
126 public void earlyEOF()
128 synchronized (lock())
135 public void messageComplete()
137 synchronized (lock())
139 super.messageComplete();