Author: olegk
Date: Sun Jul 29 14:25:45 2012
New Revision: 1366853
URL: http://svn.apache.org/viewvc?rev=1366853&view=rev
Log:
Added shared i/o buffer classes (copied from HC core with some minor changes)
Added:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
(with props)
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
(with props)
Added:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
URL:
http://svn.apache.org/viewvc/cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java?rev=1366853&view=auto
==============================================================================
---
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
(added)
+++
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
Sun Jul 29 14:25:45 2012
@@ -0,0 +1,268 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.transport.http.asyncclient;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.nio.ContentDecoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.util.ByteBufferAllocator;
+import org.apache.http.nio.util.ExpandableBuffer;
+
+/**
+ * Content buffer that can be shared by multiple threads, usually the I/O
dispatch of
+ * an I/O reactor and a worker thread.
+ * <p/>
+ * The I/O dispatch thread is expect to transfer data from {@link
ContentDecoder} to the buffer
+ * by calling {@link #consumeContent(ContentDecoder)}.
+ * <p/>
+ * The worker thread is expected to read the data from the buffer by calling
+ * {@link #read()} or {@link #read(byte[], int, int)} methods.
+ * <p/>
+ * In case of an abnormal situation or when no longer needed the buffer must
be shut down
+ * using {@link #shutdown()} method.
+ */
+@ThreadSafe
+public class SharedInputBuffer extends ExpandableBuffer {
+
+ private final ReentrantLock lock;
+ private final Condition condition;
+
+ private volatile IOControl ioctrl;
+ private volatile boolean shutdown;
+ private volatile boolean endOfStream;
+
+ public SharedInputBuffer(int buffersize, final ByteBufferAllocator
allocator) {
+ super(buffersize, allocator);
+ this.lock = new ReentrantLock();
+ this.condition = this.lock.newCondition();
+ }
+
+ public void reset() {
+ if (this.shutdown) {
+ return;
+ }
+ this.lock.lock();
+ try {
+ clear();
+ this.endOfStream = false;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public int consumeContent(final ContentDecoder decoder) throws IOException
{
+ if (this.shutdown) {
+ return -1;
+ }
+ this.lock.lock();
+ try {
+ setInputMode();
+ int totalRead = 0;
+ int bytesRead;
+ while ((bytesRead = decoder.read(this.buffer)) > 0) {
+ totalRead += bytesRead;
+ }
+ if (bytesRead == -1 || decoder.isCompleted()) {
+ this.endOfStream = true;
+ }
+ if (!this.buffer.hasRemaining() && this.ioctrl != null) {
+ this.ioctrl.suspendInput();
+ }
+ this.condition.signalAll();
+
+ if (totalRead > 0) {
+ return totalRead;
+ } else {
+ if (this.endOfStream) {
+ return -1;
+ } else {
+ return 0;
+ }
+ }
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void setIOControl(final IOControl ioc) {
+ this.lock.lock();
+ try {
+ this.ioctrl = ioc;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public boolean hasData() {
+ this.lock.lock();
+ try {
+ return super.hasData();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public int available() {
+ this.lock.lock();
+ try {
+ return super.available();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public int capacity() {
+ this.lock.lock();
+ try {
+ return super.capacity();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public int length() {
+ this.lock.lock();
+ try {
+ return super.length();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ protected void waitForData() throws IOException {
+ this.lock.lock();
+ try {
+ try {
+ while (!super.hasData() && !this.endOfStream) {
+ if (this.shutdown) {
+ throw new InterruptedIOException("Input operation
aborted");
+ }
+ if (this.ioctrl != null) {
+ this.ioctrl.requestInput();
+ }
+ this.condition.await();
+ }
+ } catch (InterruptedException ex) {
+ throw new IOException("Interrupted while waiting for more
data");
+ }
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void close() {
+ if (this.shutdown) {
+ return;
+ }
+ this.endOfStream = true;
+ this.lock.lock();
+ try {
+ this.condition.signalAll();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void shutdown() {
+ if (this.shutdown) {
+ return;
+ }
+ this.shutdown = true;
+ this.lock.lock();
+ try {
+ this.condition.signalAll();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ protected boolean isShutdown() {
+ return this.shutdown;
+ }
+
+ protected boolean isEndOfStream() {
+ return this.shutdown || (!hasData() && this.endOfStream);
+ }
+
+ public int read() throws IOException {
+ if (this.shutdown) {
+ return -1;
+ }
+ this.lock.lock();
+ try {
+ if (!hasData()) {
+ waitForData();
+ }
+ if (isEndOfStream()) {
+ return -1;
+ }
+ return this.buffer.get() & 0xff;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public int read(final byte[] b, int off, int len) throws IOException {
+ if (this.shutdown) {
+ return -1;
+ }
+ if (b == null) {
+ return 0;
+ }
+ this.lock.lock();
+ try {
+ if (!hasData()) {
+ waitForData();
+ }
+ if (isEndOfStream()) {
+ return -1;
+ }
+ setOutputMode();
+ int chunk = len;
+ if (chunk > this.buffer.remaining()) {
+ chunk = this.buffer.remaining();
+ }
+ this.buffer.get(b, off, chunk);
+ return chunk;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public int read(final byte[] b) throws IOException {
+ if (this.shutdown) {
+ return -1;
+ }
+ if (b == null) {
+ return 0;
+ }
+ return read(b, 0, b.length);
+ }
+
+}
Propchange:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedInputBuffer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
URL:
http://svn.apache.org/viewvc/cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java?rev=1366853&view=auto
==============================================================================
---
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
(added)
+++
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
Sun Jul 29 14:25:45 2012
@@ -0,0 +1,262 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.cxf.transport.http.asyncclient;
+
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.util.concurrent.locks.Condition;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.http.annotation.ThreadSafe;
+import org.apache.http.nio.ContentEncoder;
+import org.apache.http.nio.IOControl;
+import org.apache.http.nio.util.ByteBufferAllocator;
+import org.apache.http.nio.util.ExpandableBuffer;
+
+/**
+ * Content buffer that can be shared by multiple threads, usually the I/O
dispatch of
+ * an I/O reactor and a worker thread.
+ * <p/>
+ * The I/O dispatch thread is expected to transfer data from the buffer to
+ * {@link ContentEncoder} by calling {@link #produceContent(ContentEncoder)}.
+ * <p/>
+ * The worker thread is expected to write data to the buffer by calling
+ * {@link #write(int)}, {@link #write(byte[], int, int)} or {@link
#writeCompleted()}
+ * <p/>
+ * In case of an abnormal situation or when no longer needed the buffer must be
+ * shut down using {@link #shutdown()} method.
+ */
+@ThreadSafe
+public class SharedOutputBuffer extends ExpandableBuffer {
+
+ private final ReentrantLock lock;
+ private final Condition condition;
+
+ private volatile IOControl ioctrl;
+ private volatile boolean shutdown;
+ private volatile boolean endOfStream;
+
+ public SharedOutputBuffer(int buffersize, final ByteBufferAllocator
allocator) {
+ super(buffersize, allocator);
+ this.lock = new ReentrantLock();
+ this.condition = this.lock.newCondition();
+ }
+
+ public void reset() {
+ if (this.shutdown) {
+ return;
+ }
+ this.lock.lock();
+ try {
+ clear();
+ this.endOfStream = false;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public boolean hasData() {
+ this.lock.lock();
+ try {
+ return super.hasData();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public int available() {
+ this.lock.lock();
+ try {
+ return super.available();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public int capacity() {
+ this.lock.lock();
+ try {
+ return super.capacity();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ @Override
+ public int length() {
+ this.lock.lock();
+ try {
+ return super.length();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void setIOControl(final IOControl ioc) {
+ this.lock.lock();
+ try {
+ this.ioctrl = ioc;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public int produceContent(final ContentEncoder encoder) throws IOException
{
+ if (this.shutdown) {
+ return -1;
+ }
+ this.lock.lock();
+ try {
+ setOutputMode();
+ int bytesWritten = 0;
+ if (super.hasData()) {
+ bytesWritten = encoder.write(this.buffer);
+ if (encoder.isCompleted()) {
+ this.endOfStream = true;
+ }
+ }
+ if (!super.hasData()) {
+ // No more buffered content
+ // If at the end of the stream, terminate
+ if (this.endOfStream && !encoder.isCompleted()) {
+ encoder.complete();
+ }
+ if (!this.endOfStream && this.ioctrl != null) {
+ // suspend output events
+ this.ioctrl.suspendOutput();
+ }
+ }
+ this.condition.signalAll();
+ return bytesWritten;
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void close() {
+ shutdown();
+ }
+
+ public void shutdown() {
+ if (this.shutdown) {
+ return;
+ }
+ this.shutdown = true;
+ this.lock.lock();
+ try {
+ this.condition.signalAll();
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void write(final byte[] b, int off, int len) throws IOException {
+ if (b == null) {
+ return;
+ }
+ this.lock.lock();
+ try {
+ if (this.shutdown || this.endOfStream) {
+ throw new IllegalStateException("Buffer already closed for
writing");
+ }
+ setInputMode();
+ int remaining = len;
+ while (remaining > 0) {
+ if (!this.buffer.hasRemaining()) {
+ flushContent();
+ setInputMode();
+ }
+ int chunk = Math.min(remaining, this.buffer.remaining());
+ this.buffer.put(b, off, chunk);
+ remaining -= chunk;
+ off += chunk;
+ }
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void write(final byte[] b) throws IOException {
+ if (b == null) {
+ return;
+ }
+ write(b, 0, b.length);
+ }
+
+ public void write(int b) throws IOException {
+ this.lock.lock();
+ try {
+ if (this.shutdown || this.endOfStream) {
+ throw new IllegalStateException("Buffer already closed for
writing");
+ }
+ setInputMode();
+ if (!this.buffer.hasRemaining()) {
+ flushContent();
+ setInputMode();
+ }
+ this.buffer.put((byte)b);
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void flush() throws IOException {
+ }
+
+ private void flushContent() throws IOException {
+ this.lock.lock();
+ try {
+ try {
+ while (super.hasData()) {
+ if (this.shutdown) {
+ throw new InterruptedIOException("Output operation
aborted");
+ }
+ if (this.ioctrl != null) {
+ this.ioctrl.requestOutput();
+ }
+ this.condition.await();
+ }
+ } catch (InterruptedException ex) {
+ throw new IOException("Interrupted while flushing the content
buffer");
+ }
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+ public void writeCompleted() throws IOException {
+ this.lock.lock();
+ try {
+ if (this.endOfStream) {
+ return;
+ }
+ this.endOfStream = true;
+ if (this.ioctrl != null) {
+ this.ioctrl.requestOutput();
+ }
+ } finally {
+ this.lock.unlock();
+ }
+ }
+
+}
Propchange:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange:
cxf/sandbox/dkulp_async_clients/http-hc/src/main/java/org/apache/cxf/transport/http/asyncclient/SharedOutputBuffer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain