hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r629418 - in /httpcomponents/httpcore/branches/limewire_contrib/module-nio/src: main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java
Date Wed, 20 Feb 2008 11:06:23 GMT
Author: olegk
Date: Wed Feb 20 03:06:18 2008
New Revision: 629418

URL: http://svn.apache.org/viewvc?rev=629418&view=rev
Log:
HTTPCORE-148: 

* Added BufferingNHttpEntity impl; 
* Removed atrociously ugly code in TestAsyncNHttpHandlers

Added:
    httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
  (with props)
Modified:
    httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java

Added: httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java?rev=629418&view=auto
==============================================================================
--- httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
(added)
+++ httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
Wed Feb 20 03:06:18 2008
@@ -0,0 +1,115 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ * 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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.nio.entity;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+
+import org.apache.http.HttpEntity;
+import org.apache.http.entity.HttpEntityWrapper;
+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.SimpleInputBuffer;
+
+public class BufferingNHttpEntity
+    extends HttpEntityWrapper implements ConsumingNHttpEntity {
+
+    private final static int BUFFER_SIZE = 2048;
+
+    private final SimpleInputBuffer buffer;
+    private boolean finished;
+    private boolean consumed;
+
+    public BufferingNHttpEntity(
+            final HttpEntity httpEntity,
+            final ByteBufferAllocator allocator) {
+        super(httpEntity);
+        this.buffer = new SimpleInputBuffer(BUFFER_SIZE, allocator);
+    }
+
+    public void consumeContent(
+            final ContentDecoder decoder,
+            final IOControl ioctrl) throws IOException {
+        this.buffer.consumeContent(decoder);
+        if (decoder.isCompleted()) {
+            this.finished = true;
+        }
+    }
+
+    public void finish() {
+        this.finished = true;
+    }
+
+    @Override
+    public void consumeContent() throws IOException {
+        throw new UnsupportedOperationException("Does not support #consumeContent");
+    }
+
+    @Override
+    public InputStream getContent() throws IOException {
+        if (!this.finished) {
+            throw new IllegalStateException("Entity content has not been fully received");
+        }
+        if (this.consumed) {
+            throw new IllegalStateException("Entity content has not been consumed");
+        }
+        this.consumed = true;
+        return new ContentInputStream(this.buffer);
+    }
+
+    @Override
+    public boolean isRepeatable() {
+        return false;
+    }
+
+    @Override
+    public boolean isStreaming() {
+        return true;
+    }
+
+    @Override
+    public void writeTo(final OutputStream outstream) throws IOException {
+        if (outstream == null) {
+            throw new IllegalArgumentException("Output stream may not be null");
+        }
+        InputStream instream = getContent();
+        byte[] buffer = new byte[BUFFER_SIZE];
+        int l;
+        // consume until EOF
+        while ((l = instream.read(buffer)) != -1) {
+            outstream.write(buffer, 0, l);
+        }
+    }
+
+}

Propchange: httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/main/java/org/apache/http/nio/entity/BufferingNHttpEntity.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java?rev=629418&r1=629417&r2=629418&view=diff
==============================================================================
--- httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java
(original)
+++ httpcomponents/httpcore/branches/limewire_contrib/module-nio/src/test/java/org/apache/http/nio/protocol/TestAsyncNHttpHandlers.java
Wed Feb 20 03:06:18 2008
@@ -62,20 +62,16 @@
 import org.apache.http.mockup.SimpleNHttpRequestHandlerResolver;
 import org.apache.http.mockup.TestHttpClient;
 import org.apache.http.mockup.TestHttpServer;
-import org.apache.http.nio.ContentDecoder;
-import org.apache.http.nio.IOControl;
 import org.apache.http.nio.NHttpClientHandler;
 import org.apache.http.nio.NHttpConnection;
 import org.apache.http.nio.NHttpServiceHandler;
+import org.apache.http.nio.entity.BufferingNHttpEntity;
 import org.apache.http.nio.entity.ConsumingNHttpEntity;
-import org.apache.http.nio.entity.ConsumingNHttpEntityTemplate;
-import org.apache.http.nio.entity.ContentListener;
 import org.apache.http.nio.entity.NByteArrayEntity;
 import org.apache.http.nio.entity.NStringEntity;
 import org.apache.http.nio.reactor.IOReactorExceptionHandler;
 import org.apache.http.nio.reactor.ListenerEndpoint;
 import org.apache.http.nio.util.HeapByteBufferAllocator;
-import org.apache.http.nio.util.SimpleInputBuffer;
 import org.apache.http.params.BasicHttpParams;
 import org.apache.http.params.CoreConnectionPNames;
 import org.apache.http.params.CoreProtocolPNames;
@@ -172,6 +168,7 @@
                 ex.printStackTrace();
                 return false;
             }
+
         });
     }
 
@@ -378,17 +375,17 @@
             public ConsumingNHttpEntity entityRequest(
                     HttpEntityEnclosingRequest request, HttpContext context)
                     throws HttpException, IOException {
-                return new ConsumingNHttpEntityTemplate(
+                return new BufferingNHttpEntity(
                         request.getEntity(),
-                        new ByteContentListener());
+                        new HeapByteBufferAllocator());
             }
 
             public void handle(
                     HttpRequest request, HttpResponse response, HttpContext context)
                     throws HttpException, IOException {
                 if (request instanceof HttpEntityEnclosingRequest) {
-                    ConsumingNHttpEntityTemplate incoming = (ConsumingNHttpEntityTemplate)((HttpEntityEnclosingRequest)
request).getEntity();
-                    byte[] b = ((ByteContentListener)incoming.getContentListener()).getContent();
+                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] b = EntityUtils.toByteArray(entity);
                     response.setEntity(new NByteArrayEntity(b));
                 } else {
                     NStringEntity outgoing = new NStringEntity("No content");
@@ -515,17 +512,17 @@
             public ConsumingNHttpEntity entityRequest(
                     HttpEntityEnclosingRequest request, HttpContext context)
                     throws HttpException, IOException {
-                return new ConsumingNHttpEntityTemplate(
+                return new BufferingNHttpEntity(
                         request.getEntity(),
-                        new ByteContentListener());
+                        new HeapByteBufferAllocator());
             }
 
             public void handle(
                     HttpRequest request, HttpResponse response, HttpContext context)
                     throws HttpException, IOException {
                 if (request instanceof HttpEntityEnclosingRequest) {
-                    ConsumingNHttpEntityTemplate incoming = (ConsumingNHttpEntityTemplate)((HttpEntityEnclosingRequest)
request).getEntity();
-                    byte[] b = ((ByteContentListener)incoming.getContentListener()).getContent();
+                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] b = EntityUtils.toByteArray(entity);
                     NByteArrayEntity outgoing = new NByteArrayEntity(b);
                     outgoing.setChunked(true);
                     response.setEntity(outgoing);
@@ -657,17 +654,17 @@
             public ConsumingNHttpEntity entityRequest(
                     HttpEntityEnclosingRequest request, HttpContext context)
                     throws HttpException, IOException {
-                return new ConsumingNHttpEntityTemplate(
+                return new BufferingNHttpEntity(
                         request.getEntity(),
-                        new ByteContentListener());
+                        new HeapByteBufferAllocator());
             }
 
             public void handle(
                     HttpRequest request, HttpResponse response, HttpContext context)
                     throws HttpException, IOException {
                 if (request instanceof HttpEntityEnclosingRequest) {
-                    ConsumingNHttpEntityTemplate incoming = (ConsumingNHttpEntityTemplate)((HttpEntityEnclosingRequest)
request).getEntity();
-                    byte[] b = ((ByteContentListener)incoming.getContentListener()).getContent();
+                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] b = EntityUtils.toByteArray(entity);
                     NByteArrayEntity outgoing = new NByteArrayEntity(b);
                     outgoing.setChunked(false);
                     response.setEntity(outgoing);
@@ -799,17 +796,17 @@
             public ConsumingNHttpEntity entityRequest(
                     HttpEntityEnclosingRequest request, HttpContext context)
                     throws HttpException, IOException {
-                return new ConsumingNHttpEntityTemplate(
+                return new BufferingNHttpEntity(
                         request.getEntity(),
-                        new ByteContentListener());
+                        new HeapByteBufferAllocator());
             }
 
             public void handle(
                     HttpRequest request, HttpResponse response, HttpContext context)
                     throws HttpException, IOException {
                 if (request instanceof HttpEntityEnclosingRequest) {
-                    ConsumingNHttpEntityTemplate incoming = (ConsumingNHttpEntityTemplate)((HttpEntityEnclosingRequest)
request).getEntity();
-                    byte[] b = ((ByteContentListener)incoming.getContentListener()).getContent();
+                    HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
+                    byte[] b = EntityUtils.toByteArray(entity);
                     NByteArrayEntity outgoing = new NByteArrayEntity(b);
                     outgoing.setChunked(true);
                     response.setEntity(outgoing);
@@ -934,9 +931,9 @@
             public ConsumingNHttpEntity entityRequest(
                     HttpEntityEnclosingRequest request, HttpContext context)
                     throws HttpException, IOException {
-                return new ConsumingNHttpEntityTemplate(
+                return new BufferingNHttpEntity(
                         request.getEntity(),
-                        new ByteContentListener());
+                        new HeapByteBufferAllocator());
             }
 
             public void handle(
@@ -1092,9 +1089,9 @@
             public ConsumingNHttpEntity entityRequest(
                     HttpEntityEnclosingRequest request, HttpContext context)
                     throws HttpException, IOException {
-                return new ConsumingNHttpEntityTemplate(
+                return new BufferingNHttpEntity(
                         request.getEntity(),
-                        new ByteContentListener());
+                        new HeapByteBufferAllocator());
             }
 
             public void handle(
@@ -1216,25 +1213,6 @@
                     assertEquals(getHeaders[j].toString(), headHeaders[j].toString());
                 }
             }
-        }
-    }
-
-    static class ByteContentListener implements ContentListener {
-        final SimpleInputBuffer input = new SimpleInputBuffer(2048, new HeapByteBufferAllocator());
-
-        public void contentAvailable(ContentDecoder decoder, IOControl ioctrl)
-                throws IOException {
-            input.consumeContent(decoder);
-        }
-
-        public void finished() {
-            input.reset();
-        }
-
-        byte[] getContent() throws IOException {
-            byte[] b = new byte[input.length()];
-            input.read(b);
-            return b;
         }
     }
 



Mime
View raw message