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.hc.core5.http.nio.entity;
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  import java.nio.charset.StandardCharsets;
36  import java.util.List;
37  
38  import org.apache.hc.core5.http.Header;
39  import org.apache.hc.core5.http.HttpException;
40  import org.apache.hc.core5.http.nio.AsyncDataConsumer;
41  import org.apache.hc.core5.http.nio.CapacityChannel;
42  
43  public abstract class AbstractCharDataConsumer implements AsyncDataConsumer {
44  
45      private static final ByteBuffer EMPTY_BIN = ByteBuffer.wrap(new byte[0]);
46  
47      private final CharBuffer charbuf = CharBuffer.allocate(8192);
48  
49      private volatile Charset charset = StandardCharsets.US_ASCII;
50      private volatile CharsetDecoder charsetDecoder;
51  
52      protected abstract int capacity();
53  
54      protected abstract void data(CharBuffer data, boolean endOfStream) throws IOException;
55  
56      protected abstract void completed() throws IOException;
57  
58      protected final void setCharset(final Charset charset) {
59          this.charset = charset != null ? charset : StandardCharsets.US_ASCII;
60          this.charsetDecoder = null;
61      }
62  
63      @Override
64      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
65          capacityChannel.update(capacity());
66      }
67  
68      private void checkResult(final CoderResult result) throws IOException {
69          if (result.isError()) {
70              result.throwException();
71          }
72      }
73  
74      private void doDecode(final boolean endOfStream) throws IOException {
75          charbuf.flip();
76          data(charbuf, endOfStream);
77          charbuf.clear();
78      }
79  
80      private CharsetDecoder getCharsetDecoder() {
81          if (charsetDecoder == null) {
82              charsetDecoder = charset != null ? charset.newDecoder() : StandardCharsets.US_ASCII.newDecoder();
83          }
84          return charsetDecoder;
85      }
86  
87      @Override
88      public final int consume(final ByteBuffer src) throws IOException {
89          final CharsetDecoder charsetDecoder = getCharsetDecoder();
90          while (src.hasRemaining()) {
91              checkResult(charsetDecoder.decode(src, charbuf, false));
92              doDecode(false);
93          }
94          return capacity();
95      }
96  
97      @Override
98      public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
99          final CharsetDecoder charsetDecoder = getCharsetDecoder();
100         checkResult(charsetDecoder.decode(EMPTY_BIN, charbuf, true));
101         doDecode(false);
102         checkResult(charsetDecoder.flush(charbuf));
103         doDecode(true);
104         completed();
105     }
106 
107 }