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  /**
44   * Abstract text data consumer.
45   *
46   * @since 5.0
47   */
48  public abstract class AbstractCharDataConsumer implements AsyncDataConsumer {
49  
50      private static final ByteBuffer EMPTY_BIN = ByteBuffer.wrap(new byte[0]);
51  
52      private final CharBuffer charbuf = CharBuffer.allocate(8192);
53  
54      private volatile Charset charset = StandardCharsets.US_ASCII;
55      private volatile CharsetDecoder charsetDecoder;
56  
57      /**
58       * Triggered to obtain the current capacity of the consumer.
59       *
60       * @return the number of bytes this consumer is prepared to process.
61       */
62      protected abstract int capacity();
63  
64      /**
65       * Triggered to pass incoming data packet to the data consumer.
66       *
67       * @param src the data packet.
68       * @param endOfStream flag indicating whether this data packet is the last in the data stream.
69       *
70       */
71      protected abstract void data(CharBuffer src, boolean endOfStream) throws IOException;
72  
73      /**
74       * Triggered to signal completion of data processing.
75       */
76      protected abstract void completed() throws IOException;
77  
78      protected final void setCharset(final Charset charset) {
79          this.charset = charset != null ? charset : StandardCharsets.US_ASCII;
80          this.charsetDecoder = null;
81      }
82  
83      @Override
84      public final void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
85          capacityChannel.update(capacity());
86      }
87  
88      private void checkResult(final CoderResult result) throws IOException {
89          if (result.isError()) {
90              result.throwException();
91          }
92      }
93  
94      private void doDecode(final boolean endOfStream) throws IOException {
95          charbuf.flip();
96          data(charbuf, endOfStream);
97          charbuf.clear();
98      }
99  
100     private CharsetDecoder getCharsetDecoder() {
101         if (charsetDecoder == null) {
102             charsetDecoder = charset != null ? charset.newDecoder() : StandardCharsets.US_ASCII.newDecoder();
103         }
104         return charsetDecoder;
105     }
106 
107     @Override
108     public final int consume(final ByteBuffer src) throws IOException {
109         final CharsetDecoder charsetDecoder = getCharsetDecoder();
110         while (src.hasRemaining()) {
111             checkResult(charsetDecoder.decode(src, charbuf, false));
112             doDecode(false);
113         }
114         return capacity();
115     }
116 
117     @Override
118     public final void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
119         final CharsetDecoder charsetDecoder = getCharsetDecoder();
120         checkResult(charsetDecoder.decode(EMPTY_BIN, charbuf, true));
121         doDecode(false);
122         checkResult(charsetDecoder.flush(charbuf));
123         doDecode(true);
124         completed();
125     }
126 
127 }