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  
28  package org.apache.http.impl.nio.codecs;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.nio.channels.ReadableByteChannel;
33  
34  import org.apache.http.impl.io.HttpTransportMetricsImpl;
35  import org.apache.http.nio.ContentDecoder;
36  import org.apache.http.nio.reactor.SessionInputBuffer;
37  import org.apache.http.util.Args;
38  
39  /**
40   * Abstract {@link ContentDecoder} that serves as a base for all content
41   * decoder implementations.
42   *
43   * @since 4.0
44   */
45  public abstract class AbstractContentDecoder implements ContentDecoder {
46  
47      protected final ReadableByteChannel channel;
48      protected final SessionInputBuffer buffer;
49      protected final HttpTransportMetricsImpl metrics;
50  
51      protected boolean completed;
52  
53      /**
54       * Creates an instance of this class.
55       *
56       * @param channel the source channel.
57       * @param buffer the session input buffer that can be used to store
58       *    session data for intermediate processing.
59       * @param metrics Transport metrics of the underlying HTTP transport.
60       */
61      public AbstractContentDecoder(
62              final ReadableByteChannel channel,
63              final SessionInputBuffer buffer,
64              final HttpTransportMetricsImpl metrics) {
65          super();
66          Args.notNull(channel, "Channel");
67          Args.notNull(buffer, "Session input buffer");
68          Args.notNull(metrics, "Transport metrics");
69          this.buffer = buffer;
70          this.channel = channel;
71          this.metrics = metrics;
72      }
73  
74      @Override
75      public boolean isCompleted() {
76          return this.completed;
77      }
78  
79      /**
80       * Reads from the channel to the destination.
81       *
82       * @param dst destination.
83       * @return number of bytes transferred.
84       *
85       * @since 4.3
86       */
87      protected int readFromChannel(final ByteBuffer dst) throws IOException {
88          final int bytesRead = this.channel.read(dst);
89          if (bytesRead > 0) {
90              this.metrics.incrementBytesTransferred(bytesRead);
91          }
92          return bytesRead;
93      }
94  
95      /**
96       * Reads from the channel to the session buffer.
97       * @return number of bytes transferred.
98       *
99       * @since 4.3
100      */
101     protected int fillBufferFromChannel() throws IOException {
102         final int bytesRead = this.buffer.fill(this.channel);
103         if (bytesRead > 0) {
104             this.metrics.incrementBytesTransferred(bytesRead);
105         }
106         return bytesRead;
107     }
108 
109     /**
110      * Reads from the channel to the destination.
111      *
112      * @param dst destination.
113      * @param limit max number of bytes to transfer.
114      * @return number of bytes transferred.
115      *
116      * @since 4.3
117      */
118     protected int readFromChannel(final ByteBuffer dst, final int limit) throws IOException {
119         final int bytesRead;
120         if (dst.remaining() > limit) {
121             final int oldLimit = dst.limit();
122             final int newLimit = oldLimit - (dst.remaining() - limit);
123             dst.limit(newLimit);
124             bytesRead = this.channel.read(dst);
125             dst.limit(oldLimit);
126         } else {
127             bytesRead = this.channel.read(dst);
128         }
129         if (bytesRead > 0) {
130             this.metrics.incrementBytesTransferred(bytesRead);
131         }
132         return bytesRead;
133     }
134 
135 }