From 2dbcbbad103d478183fd2cd538bc06d93bb21c6b Mon Sep 17 00:00:00 2001 From: Dmitry Potapov Date: Wed, 28 Jan 2015 15:47:16 +0300 Subject: [PATCH] Customizable CharsetDecoder in AsyncCharConsumer --- .../nio/client/methods/AsyncCharConsumer.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java b/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java index bde8a1bd..1f5599fb 100644 --- a/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java +++ b/httpasyncclient/src/main/java/org/apache/http/nio/client/methods/AsyncCharConsumer.java @@ -78,15 +78,29 @@ public AsyncCharConsumer() { protected abstract void onCharReceived( CharBuffer buf, IOControl ioctrl) throws IOException; + /** + * Invoked to create a @{link CharsetDecoder} for contentType. + * This allows to use different default charsets for different content + * types and set appropriate coding error actions. + * + * @param contentType response Content-Type or null if not specified. + * @return content decoder. + */ + protected CharsetDecoder createDecoder(final ContentType contentType) { + final Charset charset; + if (contentType == null || contentType.getCharset() == null) { + charset = HTTP.DEF_CONTENT_CHARSET; + } else { + charset = contentType.getCharset(); + } + return charset.newDecoder(); + } + @Override protected final void onEntityEnclosed( final HttpEntity entity, final ContentType contentType) throws IOException { this.contentType = contentType != null ? contentType : ContentType.DEFAULT_TEXT; - Charset charset = this.contentType.getCharset(); - if (charset == null) { - charset = HTTP.DEF_CONTENT_CHARSET; - } - this.chardecoder = charset.newDecoder(); + this.chardecoder = createDecoder(contentType); } @Override