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.annotation.NotThreadSafe;
36  import org.apache.http.impl.io.HttpTransportMetricsImpl;
37  import org.apache.http.nio.FileContentDecoder;
38  import org.apache.http.nio.reactor.SessionInputBuffer;
39  import org.apache.http.util.Args;
40  
41  /**
42   * Content decoder that reads data without any transformation. The end of the
43   * content entity is delineated by closing the underlying connection
44   * (EOF condition). Entities transferred using this input stream can be of
45   * unlimited length.
46   * <p>
47   * This decoder is optimized to transfer data directly from the underlying
48   * I/O session's channel to a {@link FileChannel}, whenever
49   * possible avoiding intermediate buffering in the session buffer.
50   *
51   * @since 4.0
52   */
53  @NotThreadSafe
54  public class IdentityDecoder extends AbstractContentDecoder
55          implements FileContentDecoder {
56  
57      public IdentityDecoder(
58              final ReadableByteChannel channel,
59              final SessionInputBuffer buffer,
60              final HttpTransportMetricsImpl metrics) {
61          super(channel, buffer, metrics);
62      }
63  
64      /**
65       * Sets the completed status of this decoder. Normally this is not necessary
66       * (the decoder will automatically complete when the underlying channel
67       * returns EOF). It is useful to mark the decoder as completed if you have
68       * some other means to know all the necessary data has been read and want to
69       * reuse the underlying connection for more messages.
70       */
71      public void setCompleted(final boolean completed) {
72          this.completed = completed;
73      }
74  
75      public int read(final ByteBuffer dst) throws IOException {
76          Args.notNull(dst, "Byte buffer");
77          if (this.completed) {
78              return -1;
79          }
80  
81          final int bytesRead;
82          if (this.buffer.hasData()) {
83              bytesRead = this.buffer.read(dst);
84          } else {
85              bytesRead = readFromChannel(dst);
86          }
87          if (bytesRead == -1) {
88              this.completed = true;
89          }
90          return bytesRead;
91      }
92  
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 }