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      @Override
62      public int read(final ByteBuffer dst) throws IOException {
63          Args.notNull(dst, "Byte buffer");
64          if (isCompleted()) {
65              return -1;
66          }
67  
68          final int bytesRead;
69          if (this.buffer.hasData()) {
70              bytesRead = this.buffer.read(dst);
71          } else {
72              bytesRead = readFromChannel(dst);
73          }
74          if (bytesRead == -1) {
75              setCompleted();
76          }
77          return bytesRead;
78      }
79  
80      @Override
81      public long transfer(
82              final FileChannel dst,
83              final long position,
84              final long count) throws IOException {
85  
86          if (dst == null) {
87              return 0;
88          }
89          if (isCompleted()) {
90              return 0;
91          }
92  
93          long bytesRead;
94          if (this.buffer.hasData()) {
95              dst.position(position);
96              bytesRead = this.buffer.read(dst);
97          } else {
98              if (this.channel.isOpen()) {
99                  if (position > dst.size()) {
100                     throw new IOException("Position past end of file [" + position +
101                             " > " + dst.size() + "]");
102                 }
103                 bytesRead = dst.transferFrom(this.channel, position, count);
104                 if (count > 0 && bytesRead == 0) {
105                     bytesRead = this.buffer.fill(this.channel);
106                 }
107             } else {
108                 bytesRead = -1;
109             }
110             if (bytesRead > 0) {
111                 this.metrics.incrementBytesTransferred(bytesRead);
112             }
113         }
114         if (bytesRead == -1) {
115             setCompleted();
116         }
117         return bytesRead;
118     }
119 
120     @Override
121     public String toString() {
122         final StringBuilder sb = new StringBuilder();
123         sb.append("[identity; completed: ");
124         sb.append(this.completed);
125         sb.append("]");
126         return sb.toString();
127     }
128 
129 }