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.FileChannel;
33  import java.nio.channels.ReadableByteChannel;
34  
35  import org.apache.http.impl.io.HttpTransportMetricsImpl;
36  import org.apache.http.nio.FileContentDecoder;
37  import org.apache.http.nio.reactor.SessionInputBuffer;
38  import org.apache.http.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
53          implements FileContentDecoder {
54  
55      public IdentityDecoder(
56              final ReadableByteChannel channel,
57              final SessionInputBuffer buffer,
58              final HttpTransportMetricsImpl metrics) {
59          super(channel, buffer, metrics);
60      }
61  
62      @Override
63      public int read(final ByteBuffer dst) throws IOException {
64          Args.notNull(dst, "Byte buffer");
65          if (isCompleted()) {
66              return -1;
67          }
68  
69          final int bytesRead;
70          if (this.buffer.hasData()) {
71              bytesRead = this.buffer.read(dst);
72          } else {
73              bytesRead = readFromChannel(dst);
74          }
75          if (bytesRead == -1) {
76              setCompleted();
77          }
78          return bytesRead;
79      }
80  
81      @Override
82      public long transfer(
83              final FileChannel dst,
84              final long position,
85              final long count) throws IOException {
86  
87          if (dst == null) {
88              return 0;
89          }
90          if (isCompleted()) {
91              return 0;
92          }
93  
94          long bytesRead;
95          if (this.buffer.hasData()) {
96              final int maxLen = this.buffer.length();
97              dst.position(position);
98              bytesRead = this.buffer.read(dst, count < maxLen ? (int)count : maxLen);
99          } else {
100             if (this.channel.isOpen()) {
101                 if (position > dst.size()) {
102                     throw new IOException(String.format("Position past end of file [%,d > %,d]",
103                                     position, dst.size()));
104                 }
105                 bytesRead = dst.transferFrom(this.channel, position, count);
106                 if (count > 0 && bytesRead == 0) {
107                     bytesRead = this.buffer.fill(this.channel);
108                 }
109             } else {
110                 bytesRead = -1;
111             }
112             if (bytesRead > 0) {
113                 this.metrics.incrementBytesTransferred(bytesRead);
114             }
115         }
116         if (bytesRead == -1) {
117             setCompleted();
118         }
119         return bytesRead;
120     }
121 
122     @Override
123     public String toString() {
124         final StringBuilder sb = new StringBuilder();
125         sb.append("[identity; completed: ");
126         sb.append(this.completed);
127         sb.append("]");
128         return sb.toString();
129     }
130 
131 }