View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  package org.apache.http.nio.client.methods;
28  
29  import java.io.IOException;
30  import java.nio.ByteBuffer;
31  import java.nio.CharBuffer;
32  import java.nio.charset.Charset;
33  import java.nio.charset.CharsetDecoder;
34  import java.nio.charset.CoderResult;
35  
36  import org.apache.http.HttpEntity;
37  import org.apache.http.entity.ContentType;
38  import org.apache.http.nio.ContentDecoder;
39  import org.apache.http.nio.IOControl;
40  import org.apache.http.nio.protocol.AbstractAsyncResponseConsumer;
41  import org.apache.http.protocol.HTTP;
42  import org.apache.http.util.Asserts;
43  
44  /**
45   * {@link org.apache.http.nio.protocol.HttpAsyncResponseConsumer} implementation that
46   * provides convenience methods for processing of textual content entities enclosed
47   * in an HTTP response.
48   *
49   * @since 4.0
50   */
51  public abstract class AsyncCharConsumer<T> extends AbstractAsyncResponseConsumer<T> {
52  
53      private final ByteBuffer bbuf;
54      private final CharBuffer cbuf;
55  
56      private CharsetDecoder charDecoder;
57  
58      public AsyncCharConsumer(final int bufSize) {
59          super();
60          this.bbuf = ByteBuffer.allocate(bufSize);
61          this.cbuf = CharBuffer.allocate(bufSize);
62      }
63  
64      public AsyncCharConsumer() {
65          this(8 * 1024);
66      }
67  
68      /**
69       * Invoked to process a {@link CharBuffer chunk} of content.
70       * The {@link IOControl} interface can be used to suspend input events
71       * if the consumer is temporarily unable to consume more content.
72       *
73       * @param buf chunk of content.
74       * @param ioControl I/O control of the underlying connection.
75       * @throws IOException in case of an I/O error
76       */
77      protected abstract void onCharReceived(
78              CharBuffer buf, IOControl ioControl) throws IOException;
79  
80      /**
81       * Invoked to create a @{link CharsetDecoder} for contentType.
82       * This allows to use different default charsets for different content
83       * types and set appropriate coding error actions.
84       *
85       * @param contentType response Content-Type or null if not specified.
86       * @return content decoder.
87       *
88       * @since 4.1
89       */
90      protected CharsetDecoder createDecoder(final ContentType contentType) {
91          Charset charset = contentType != null ? contentType.getCharset() : null;
92          if (charset == null) {
93              charset = HTTP.DEF_CONTENT_CHARSET;
94          }
95          return charset.newDecoder();
96      }
97  
98      @Override
99      protected final void onEntityEnclosed(
100             final HttpEntity entity, final ContentType contentType) throws IOException {
101         this.charDecoder = createDecoder(contentType != null ? contentType : ContentType.DEFAULT_TEXT);
102     }
103 
104     @Override
105     protected final void onContentReceived(
106             final ContentDecoder decoder, final IOControl ioControl) throws IOException {
107         Asserts.notNull(this.bbuf, "Byte buffer");
108 
109         final int bytesRead = decoder.read(this.bbuf);
110         if (bytesRead <= 0) {
111             return;
112         }
113         this.bbuf.flip();
114         final boolean completed = decoder.isCompleted();
115         CoderResult result = this.charDecoder.decode(this.bbuf, this.cbuf, completed);
116         handleDecodingResult(result, ioControl);
117         this.bbuf.compact();
118         if (completed) {
119             result = this.charDecoder.flush(this.cbuf);
120             handleDecodingResult(result, ioControl);
121         }
122     }
123 
124     private void handleDecodingResult(
125             final CoderResult result, final IOControl ioControl) throws IOException {
126         if (result.isError()) {
127             result.throwException();
128         }
129         this.cbuf.flip();
130         if (this.cbuf.hasRemaining()) {
131             onCharReceived(this.cbuf, ioControl);
132         }
133         this.cbuf.clear();
134     }
135 
136     @Override
137     protected void releaseResources() {
138     }
139 
140 }