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