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.hc.core5.http.impl.nio;
29  
30  import java.io.IOException;
31  import java.nio.ByteBuffer;
32  import java.nio.channels.FileChannel;
33  import java.nio.channels.ReadableByteChannel;
34  
35  import org.apache.hc.core5.http.impl.BasicHttpTransportMetrics;
36  import org.apache.hc.core5.http.nio.FileContentDecoder;
37  import org.apache.hc.core5.http.nio.SessionInputBuffer;
38  import org.apache.hc.core5.util.Args;
39  
40  /**
41   * Content decoder that reads data without any transformation. The end of the
42   * content entity is delineated by closing the underlying connection
43   * (EOF condition). Entities transferred using this input stream can be of
44   * unlimited length.
45   * <p>
46   * This decoder is optimized to transfer data directly from the underlying
47   * I/O session's channel to a {@link FileChannel}, whenever
48   * possible avoiding intermediate buffering in the session buffer.
49   *
50   * @since 4.0
51   */
52  public class IdentityDecoder extends AbstractContentDecoder implements FileContentDecoder {
53  
54      public IdentityDecoder(
55              final ReadableByteChannel channel,
56              final SessionInputBuffer buffer,
57              final BasicHttpTransportMetrics metrics) {
58          super(channel, buffer, metrics);
59      }
60  
61      /**
62       * Sets the completed status of this decoder. Normally this is not necessary
63       * (the decoder will automatically complete when the underlying channel
64       * returns EOF). It is useful to mark the decoder as completed if you have
65       * some other means to know all the necessary data has been read and want to
66       * reuse the underlying connection for more messages.
67       */
68      public void setCompleted(final boolean completed) {
69          this.completed = completed;
70      }
71  
72      @Override
73      public int read(final ByteBuffer dst) throws IOException {
74          Args.notNull(dst, "Byte buffer");
75          if (this.completed) {
76              return -1;
77          }
78  
79          final int bytesRead;
80          if (this.buffer.hasData()) {
81              bytesRead = this.buffer.read(dst);
82          } else {
83              bytesRead = readFromChannel(dst);
84          }
85          if (bytesRead == -1) {
86              this.completed = true;
87          }
88          return bytesRead;
89      }
90  
91      @Override
92      public long transfer(
93              final FileChannel dst,
94              final long position,
95              final long count) throws IOException {
96  
97          if (dst == null) {
98              return 0;
99          }
100         if (this.completed) {
101             return 0;
102         }
103 
104         long bytesRead;
105         if (this.buffer.hasData()) {
106             dst.position(position);
107             bytesRead = this.buffer.read(dst);
108         } else {
109             if (this.channel.isOpen()) {
110                 if (position > dst.size()) {
111                     throw new IOException("Position past end of file [" + position +
112                             " > " + dst.size() + "]");
113                 }
114                 bytesRead = dst.transferFrom(this.channel, position, count);
115                 if (count > 0 && bytesRead == 0) {
116                     bytesRead = this.buffer.fill(this.channel);
117                 }
118             } else {
119                 bytesRead = -1;
120             }
121             if (bytesRead > 0) {
122                 this.metrics.incrementBytesTransferred(bytesRead);
123             }
124         }
125         if (bytesRead == -1) {
126             this.completed = true;
127         }
128         return bytesRead;
129     }
130 
131     @Override
132     public String toString() {
133         final StringBuilder sb = new StringBuilder();
134         sb.append("[identity; completed: ");
135         sb.append(this.completed);
136         sb.append("]");
137         return sb.toString();
138     }
139 
140 }