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      /**
63       * Sets the completed status of this decoder. Normally this is not necessary
64       * (the decoder will automatically complete when the underlying channel
65       * returns EOF). It is useful to mark the decoder as completed if you have
66       * some other means to know all the necessary data has been read and want to
67       * reuse the underlying connection for more messages.
68       */
69      public void setCompleted(final boolean completed) {
70          this.completed = completed;
71      }
72  
73      @Override
74      public int read(final ByteBuffer dst) throws IOException {
75          Args.notNull(dst, "Byte buffer");
76          if (this.completed) {
77              return -1;
78          }
79  
80          final int bytesRead;
81          if (this.buffer.hasData()) {
82              bytesRead = this.buffer.read(dst);
83          } else {
84              bytesRead = readFromChannel(dst);
85          }
86          if (bytesRead == -1) {
87              this.completed = true;
88          }
89          return bytesRead;
90      }
91  
92      @Override
93      public long transfer(
94              final FileChannel dst,
95              final long position,
96              final long count) throws IOException {
97  
98          if (dst == null) {
99              return 0;
100         }
101         if (this.completed) {
102             return 0;
103         }
104 
105         long bytesRead;
106         if (this.buffer.hasData()) {
107             dst.position(position);
108             bytesRead = this.buffer.read(dst);
109         } else {
110             if (this.channel.isOpen()) {
111                 if (position > dst.size()) {
112                     throw new IOException("Position past end of file [" + position +
113                             " > " + dst.size() + "]");
114                 }
115                 bytesRead = dst.transferFrom(this.channel, position, count);
116                 if (count > 0 && bytesRead == 0) {
117                     bytesRead = this.buffer.fill(this.channel);
118                 }
119             } else {
120                 bytesRead = -1;
121             }
122             if (bytesRead > 0) {
123                 this.metrics.incrementBytesTransferred(bytesRead);
124             }
125         }
126         if (bytesRead == -1) {
127             this.completed = true;
128         }
129         return bytesRead;
130     }
131 
132     @Override
133     public String toString() {
134         final StringBuilder sb = new StringBuilder();
135         sb.append("[identity; completed: ");
136         sb.append(this.completed);
137         sb.append("]");
138         return sb.toString();
139     }
140 
141 }