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              final int maxLen = this.buffer.length();
96              dst.position(position);
97              bytesRead = this.buffer.read(dst, count < maxLen ? (int)count : maxLen);
98          } else {
99              if (this.channel.isOpen()) {
100                 if (position > dst.size()) {
101                     throw new IOException("Position past end of file [" + position +
102                             " > " + dst.size() + "]");
103                 }
104                 bytesRead = dst.transferFrom(this.channel, position, count);
105                 if (count > 0 && bytesRead == 0) {
106                     bytesRead = this.buffer.fill(this.channel);
107                 }
108             } else {
109                 bytesRead = -1;
110             }
111             if (bytesRead > 0) {
112                 this.metrics.incrementBytesTransferred(bytesRead);
113             }
114         }
115         if (bytesRead == -1) {
116             setCompleted();
117         }
118         return bytesRead;
119     }
120 
121     @Override
122     public String toString() {
123         final StringBuilder sb = new StringBuilder();
124         sb.append("[identity; completed: ");
125         sb.append(this.completed);
126         sb.append("]");
127         return sb.toString();
128     }
129 
130 }