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 2019/09/22 14:16:38 UTC
[httpcomponents-core] 01/02: Reduced public API footprint of data
buffering classes
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch async-io-handling
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit 462f82ba41e8623634a711575d819601a5e3d784
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Sun Sep 22 14:09:28 2019 +0200
Reduced public API footprint of data buffering classes
---
.../hc/core5/http/impl/nio/BufferedData.java | 85 ++++++++++++++++++++++
.../hc/core5/http/impl/nio/ExpandableBuffer.java | 13 ++--
.../http/impl/nio/SessionInputBufferImpl.java | 23 ++++--
.../http/impl/nio/SessionOutputBufferImpl.java | 23 ++++--
.../http/examples/AsyncReverseProxyExample.java | 11 +--
5 files changed, 127 insertions(+), 28 deletions(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/BufferedData.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/BufferedData.java
new file mode 100644
index 0000000..8b37751
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/BufferedData.java
@@ -0,0 +1,85 @@
+/*
+ * ====================================================================
+ * 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.impl.nio;
+
+import java.nio.ByteBuffer;
+
+import org.apache.hc.core5.util.Args;
+
+/**
+ * A buffer that expand its capacity on demand. Internally, this class is backed
+ * by an instance of {@link ByteBuffer}.
+ * <p>
+ * This class is not thread safe.
+ * </p>
+ * @since 5.0
+ */
+public class BufferedData extends ExpandableBuffer {
+
+ public static BufferedData allocate(final int bufferSize) {
+ return new BufferedData(bufferSize);
+ }
+
+ protected BufferedData(final int bufferSize) {
+ super(bufferSize);
+ }
+
+ @Override
+ public final boolean hasData() {
+ return super.hasData();
+ }
+
+ @Override
+ public final int length() {
+ return super.length();
+ }
+
+ @Override
+ public final int capacity() {
+ return super.capacity();
+ }
+
+ @Override
+ public final void clear() {
+ super.clear();
+ }
+
+ public final void put(final ByteBuffer src) {
+ Args.notNull(src, "Data source");
+ setInputMode();
+ final int requiredCapacity = buffer().position() + src.remaining();
+ ensureCapacity(requiredCapacity);
+ buffer().put(src);
+ }
+
+ public final ByteBuffer data() {
+ setOutputMode();
+ return buffer();
+ }
+
+}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ExpandableBuffer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ExpandableBuffer.java
index 10c7143..5bb9f9d 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ExpandableBuffer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/ExpandableBuffer.java
@@ -30,6 +30,8 @@ package org.apache.hc.core5.http.impl.nio;
import java.nio.BufferOverflowException;
import java.nio.ByteBuffer;
+import org.apache.hc.core5.annotation.Internal;
+
/**
* A buffer that expand its capacity on demand. Internally, this class is backed
* by an instance of {@link ByteBuffer}.
@@ -38,6 +40,7 @@ import java.nio.ByteBuffer;
* </p>
* @since 4.0
*/
+@Internal
public class ExpandableBuffer {
public enum Mode {
@@ -70,11 +73,11 @@ public class ExpandableBuffer {
*
* @return current input/output mode.
*/
- public Mode mode() {
+ protected Mode mode() {
return this.mode;
}
- public ByteBuffer buffer() {
+ protected ByteBuffer buffer() {
return this.buffer;
}
@@ -158,7 +161,7 @@ public class ExpandableBuffer {
* @return {@code true} if there is data in the buffer,
* {@code false} otherwise.
*/
- public boolean hasData() {
+ protected boolean hasData() {
setOutputMode();
return this.buffer.hasRemaining();
}
@@ -171,7 +174,7 @@ public class ExpandableBuffer {
*
* @return buffer length.
*/
- public int length() {
+ protected int length() {
setOutputMode();
return this.buffer.remaining();
}
@@ -181,7 +184,7 @@ public class ExpandableBuffer {
*
* @return buffer length.
*/
- public int capacity() {
+ protected int capacity() {
setInputMode();
return this.buffer.remaining();
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionInputBufferImpl.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionInputBufferImpl.java
index d5a0948..a91261c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionInputBufferImpl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionInputBufferImpl.java
@@ -42,13 +42,7 @@ import org.apache.hc.core5.http.nio.SessionInputBuffer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
-/**
- * Default implementation of {@link SessionInputBuffer} based on
- * the {@link ExpandableBuffer} class.
- *
- * @since 4.0
- */
-public class SessionInputBufferImpl extends ExpandableBuffer implements SessionInputBuffer {
+class SessionInputBufferImpl extends ExpandableBuffer implements SessionInputBuffer {
private final CharsetDecoder charDecoder;
private final int lineBuffersize;
@@ -116,6 +110,21 @@ public class SessionInputBufferImpl extends ExpandableBuffer implements SessionI
this(bufferSize, 256);
}
+ @Override
+ public int length() {
+ return super.length();
+ }
+
+ @Override
+ public boolean hasData() {
+ return super.hasData();
+ }
+
+ @Override
+ public int capacity() {
+ return super.capacity();
+ }
+
public void put(final ByteBuffer src) {
if (src != null && src.hasRemaining()) {
setInputMode();
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionOutputBufferImpl.java b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionOutputBufferImpl.java
index 163bad3..317be12 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionOutputBufferImpl.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/impl/nio/SessionOutputBufferImpl.java
@@ -42,13 +42,7 @@ import org.apache.hc.core5.http.nio.SessionOutputBuffer;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
-/**
- * Default implementation of {@link SessionOutputBuffer} based on
- * the {@link ExpandableBuffer} class.
- *
- * @since 4.0
- */
-public class SessionOutputBufferImpl extends ExpandableBuffer implements SessionOutputBuffer {
+class SessionOutputBufferImpl extends ExpandableBuffer implements SessionOutputBuffer {
private static final byte[] CRLF = new byte[] {Chars.CR, Chars.LF};
@@ -104,6 +98,21 @@ public class SessionOutputBufferImpl extends ExpandableBuffer implements Session
}
@Override
+ public int length() {
+ return super.length();
+ }
+
+ @Override
+ public boolean hasData() {
+ return super.hasData();
+ }
+
+ @Override
+ public int capacity() {
+ return super.capacity();
+ }
+
+ @Override
public int flush(final WritableByteChannel channel) throws IOException {
Args.notNull(channel, "Channel");
setOutputMode();
diff --git a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java
index b0592f4..da0acb1 100644
--- a/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java
+++ b/httpcore5/src/test/java/org/apache/hc/core5/http/examples/AsyncReverseProxyExample.java
@@ -62,7 +62,7 @@ import org.apache.hc.core5.http.impl.bootstrap.AsyncRequesterBootstrap;
import org.apache.hc.core5.http.impl.bootstrap.AsyncServerBootstrap;
import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncRequester;
import org.apache.hc.core5.http.impl.bootstrap.HttpAsyncServer;
-import org.apache.hc.core5.http.impl.nio.ExpandableBuffer;
+import org.apache.hc.core5.http.impl.nio.BufferedData;
import org.apache.hc.core5.http.message.BasicHttpRequest;
import org.apache.hc.core5.http.message.BasicHttpResponse;
import org.apache.hc.core5.http.nio.AsyncClientEndpoint;
@@ -210,19 +210,12 @@ public class AsyncReverseProxyExample {
server.awaitShutdown(TimeValue.MAX_VALUE);
}
- private static class ProxyBuffer extends ExpandableBuffer {
+ private static class ProxyBuffer extends BufferedData {
ProxyBuffer(final int bufferSize) {
super(bufferSize);
}
- void put(final ByteBuffer src) {
- setInputMode();
- final int requiredCapacity = buffer().position() + src.remaining();
- ensureCapacity(requiredCapacity);
- buffer().put(src);
- }
-
int write(final DataStreamChannel channel) throws IOException {
setOutputMode();
if (buffer().hasRemaining()) {