You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2017/04/14 11:14:38 UTC
svn commit: r1791356 - in /httpcomponents/httpcore/trunk/httpcore5/src:
main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
Author: olegk
Date: Fri Apr 14 11:14:38 2017
New Revision: 1791356
URL: http://svn.apache.org/viewvc?rev=1791356&view=rev
Log:
Added BasicAsyncEntityConsumer
Added:
httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java (with props)
Modified:
httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
Added: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java?rev=1791356&view=auto
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java (added)
+++ httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java Fri Apr 14 11:14:38 2017
@@ -0,0 +1,79 @@
+/*
+ * ====================================================================
+ * 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.hc.core5.http.nio.entity;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.apache.hc.core5.http.ContentType;
+import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.util.ByteArrayBuffer;
+
+public class BasicAsyncEntityConsumer extends AbstractBinAsyncEntityConsumer<byte[]> {
+
+
+ private final ByteArrayBuffer buffer;
+
+ public BasicAsyncEntityConsumer() {
+ super();
+ this.buffer = new ByteArrayBuffer(1024);
+ }
+
+ @Override
+ protected void streamStart(final ContentType contentType) throws HttpException, IOException {
+ }
+
+ @Override
+ protected int capacity() {
+ return Integer.MAX_VALUE;
+ }
+
+ @Override
+ protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException {
+ if (src == null) {
+ return;
+ }
+ if (src.hasArray()) {
+ buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining());
+ } else {
+ while (src.hasRemaining()) {
+ buffer.append(src.get());
+ }
+ }
+ }
+
+ @Override
+ protected byte[] generateContent() throws IOException {
+ return buffer.toByteArray();
+ }
+
+ @Override
+ public void releaseResources() {
+ }
+
+}
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: httpcomponents/httpcore/trunk/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/BasicAsyncEntityConsumer.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java
URL: http://svn.apache.org/viewvc/httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java?rev=1791356&r1=1791355&r2=1791356&view=diff
==============================================================================
--- httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java (original)
+++ httpcomponents/httpcore/trunk/httpcore5/src/test/java/org/apache/hc/core5/http/nio/entity/TestAbstractBinAsyncEntityConsumer.java Fri Apr 14 11:14:38 2017
@@ -35,42 +35,20 @@ import org.apache.hc.core5.concurrent.Fu
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
import org.apache.hc.core5.http.impl.BasicEntityDetails;
-import org.apache.hc.core5.http.impl.nio.ExpandableBuffer;
import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
+import org.apache.hc.core5.util.ByteArrayBuffer;
import org.junit.Assert;
import org.junit.Test;
public class TestAbstractBinAsyncEntityConsumer {
- private static class InternalBuffer extends ExpandableBuffer {
-
- InternalBuffer(final int bufferSize) {
- super(bufferSize);
- }
-
- void write(final ByteBuffer src) {
- setInputMode();
- final int requiredCapacity = buffer().position() + src.remaining();
- ensureCapacity(requiredCapacity);
- buffer().put(src);
- }
-
- byte[] toByteArray() {
- setOutputMode();
- final byte[] bytes = new byte[buffer().remaining()];
- buffer().get(bytes);
- return bytes;
- }
-
- }
-
static private class ByteArrayAsyncEntityConsumer extends AbstractBinAsyncEntityConsumer<byte[]> {
- private final InternalBuffer buffer;
+ private final ByteArrayBuffer buffer;
public ByteArrayAsyncEntityConsumer() {
super();
- this.buffer = new InternalBuffer(1024);
+ this.buffer = new ByteArrayBuffer(1024);
}
@Override
@@ -84,7 +62,16 @@ public class TestAbstractBinAsyncEntityC
@Override
protected void data(final ByteBuffer src, final boolean endOfStream) throws IOException {
- buffer.write(src);
+ if (src == null) {
+ return;
+ }
+ if (src.hasArray()) {
+ buffer.append(src.array(), src.arrayOffset() + src.position(), src.remaining());
+ } else {
+ while (src.hasRemaining()) {
+ buffer.append(src.get());
+ }
+ }
}
@Override