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/26 13:16:51 UTC
[httpcomponents-core] 01/03: Text based entity and data consumers
to use CharCodingConfig constructor parameter
This is an automated email from the ASF dual-hosted git repository.
olegk pushed a commit to branch development
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git
commit bb73e3deb6d47dc5b523341657687cd161b24bc7
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Sep 26 14:50:03 2019 +0200
Text based entity and data consumers to use CharCodingConfig constructor parameter
---
.../entity/AbstractCharAsyncEntityConsumer.java | 16 ++++++-----
.../http/nio/entity/AbstractCharDataConsumer.java | 33 +++++++++++++++++++---
.../http/nio/entity/StringAsyncEntityConsumer.java | 15 ++++++++--
3 files changed, 50 insertions(+), 14 deletions(-)
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
index df452bc..34bf15c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharAsyncEntityConsumer.java
@@ -28,14 +28,13 @@ package org.apache.hc.core5.http.nio.entity;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
-import java.nio.charset.StandardCharsets;
import java.nio.charset.UnsupportedCharsetException;
import org.apache.hc.core5.concurrent.FutureCallback;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.EntityDetails;
import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.config.CharCodingConfig;
import org.apache.hc.core5.http.nio.AsyncEntityConsumer;
import org.apache.hc.core5.util.Args;
@@ -51,6 +50,13 @@ public abstract class AbstractCharAsyncEntityConsumer<T> extends AbstractCharDat
private volatile FutureCallback<T> resultCallback;
private volatile T content;
+ protected AbstractCharAsyncEntityConsumer(final int bufSize, final CharCodingConfig charCodingConfig) {
+ super(bufSize, charCodingConfig);
+ }
+
+ public AbstractCharAsyncEntityConsumer() {
+ }
+
/**
* Triggered to signal beginning of entity content stream.
*
@@ -73,11 +79,7 @@ public abstract class AbstractCharAsyncEntityConsumer<T> extends AbstractCharDat
this.resultCallback = resultCallback;
try {
final ContentType contentType = entityDetails != null ? ContentType.parse(entityDetails.getContentType()) : null;
- Charset charset = contentType != null ? contentType.getCharset() : null;
- if (charset == null) {
- charset = StandardCharsets.US_ASCII;
- }
- setCharset(charset);
+ setCharset(contentType != null ? contentType.getCharset() : null);
streamStart(contentType);
} catch (final UnsupportedCharsetException ex) {
throw new UnsupportedEncodingException(ex.getMessage());
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
index 91c4cc4..d89a431 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/AbstractCharDataConsumer.java
@@ -37,8 +37,10 @@ import java.util.List;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.config.CharCodingConfig;
import org.apache.hc.core5.http.nio.AsyncDataConsumer;
import org.apache.hc.core5.http.nio.CapacityChannel;
+import org.apache.hc.core5.util.Args;
/**
* Abstract text data consumer.
@@ -47,13 +49,23 @@ import org.apache.hc.core5.http.nio.CapacityChannel;
*/
public abstract class AbstractCharDataConsumer implements AsyncDataConsumer {
+ protected static final int DEF_BUF_SIZE = 8192;
private static final ByteBuffer EMPTY_BIN = ByteBuffer.wrap(new byte[0]);
- private final CharBuffer charbuf = CharBuffer.allocate(8192);
+ private final CharBuffer charbuf;
+ private final CharCodingConfig charCodingConfig;
- private volatile Charset charset = StandardCharsets.US_ASCII;
+ private volatile Charset charset;
private volatile CharsetDecoder charsetDecoder;
+ protected AbstractCharDataConsumer(final int bufSize, final CharCodingConfig charCodingConfig) {
+ this.charbuf = CharBuffer.allocate(Args.positive(bufSize, "Buffer size"));
+ this.charCodingConfig = charCodingConfig != null ? charCodingConfig : CharCodingConfig.DEFAULT;
+ }
+
+ public AbstractCharDataConsumer() {
+ this(DEF_BUF_SIZE, CharCodingConfig.DEFAULT);
+ }
/**
* Triggered to obtain the capacity increment.
*
@@ -76,7 +88,7 @@ public abstract class AbstractCharDataConsumer implements AsyncDataConsumer {
protected abstract void completed() throws IOException;
protected final void setCharset(final Charset charset) {
- this.charset = charset != null ? charset : StandardCharsets.US_ASCII;
+ this.charset = charset != null ? charset : charCodingConfig.getCharset();
this.charsetDecoder = null;
}
@@ -99,7 +111,20 @@ public abstract class AbstractCharDataConsumer implements AsyncDataConsumer {
private CharsetDecoder getCharsetDecoder() {
if (charsetDecoder == null) {
- charsetDecoder = charset != null ? charset.newDecoder() : StandardCharsets.US_ASCII.newDecoder();
+ Charset charset = this.charset;
+ if (charset == null) {
+ charset = charCodingConfig.getCharset();
+ }
+ if (charset == null) {
+ charset = StandardCharsets.US_ASCII;
+ }
+ charsetDecoder = charset.newDecoder();
+ if (charCodingConfig.getMalformedInputAction() != null) {
+ charsetDecoder.onMalformedInput(charCodingConfig.getMalformedInputAction());
+ }
+ if (charCodingConfig.getUnmappableInputAction() != null) {
+ charsetDecoder.onUnmappableCharacter(charCodingConfig.getUnmappableInputAction());
+ }
}
return charsetDecoder;
}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
index e1019af..275fce5 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/entity/StringAsyncEntityConsumer.java
@@ -31,6 +31,7 @@ import java.nio.CharBuffer;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.HttpException;
+import org.apache.hc.core5.http.config.CharCodingConfig;
import org.apache.hc.core5.util.Args;
import org.apache.hc.core5.util.CharArrayBuffer;
@@ -45,12 +46,20 @@ public class StringAsyncEntityConsumer extends AbstractCharAsyncEntityConsumer<S
private final int capacityIncrement;
private final CharArrayBuffer content;
- public StringAsyncEntityConsumer(final int capacityIncrement) {
- Args.positive(capacityIncrement, "Capacity increment");
- this.capacityIncrement = capacityIncrement;
+ public StringAsyncEntityConsumer(final int bufSize, final int capacityIncrement, final CharCodingConfig charCodingConfig) {
+ super(bufSize, charCodingConfig);
+ this.capacityIncrement = Args.positive(capacityIncrement, "Capacity increment");
this.content = new CharArrayBuffer(1024);
}
+ public StringAsyncEntityConsumer(final int capacityIncrement) {
+ this(DEF_BUF_SIZE, capacityIncrement, CharCodingConfig.DEFAULT);
+ }
+
+ public StringAsyncEntityConsumer(final CharCodingConfig charCodingConfig) {
+ this(DEF_BUF_SIZE, Integer.MAX_VALUE, charCodingConfig);
+ }
+
public StringAsyncEntityConsumer() {
this(Integer.MAX_VALUE);
}