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.ConnectionClosedException;
36  import org.apache.http.annotation.NotThreadSafe;
37  import org.apache.http.impl.io.HttpTransportMetricsImpl;
38  import org.apache.http.nio.FileContentDecoder;
39  import org.apache.http.nio.reactor.SessionInputBuffer;
40  
41  /**
42   * Content decoder that cuts off after a defined number of bytes. This class
43   * is used to receive content of HTTP messages where the end of the content
44   * entity is determined by the value of the <code>Content-Length header</code>.
45   * Entities transferred using this stream can be maximum {@link Long#MAX_VALUE}
46   * long.
47   * <p>
48   * This decoder is optimized to transfer data directly from the underlying
49   * I/O session's channel to a {@link FileChannel}, whenever
50   * possible avoiding intermediate buffering in the session buffer.
51   *
52   * @since 4.0
53   */
54  @NotThreadSafe
55  public class LengthDelimitedDecoder extends AbstractContentDecoder
56          implements FileContentDecoder {
57  
58      private final long contentLength;
59  
60      private long len;
61  
62      public LengthDelimitedDecoder(
63              final ReadableByteChannel channel,
64              final SessionInputBuffer buffer,
65              final HttpTransportMetricsImpl metrics,
66              long contentLength) {
67          super(channel, buffer, metrics);
68          if (contentLength < 0) {
69              throw new IllegalArgumentException("Content length may not be negative");
70          }
71          this.contentLength = contentLength;
72      }
73  
74      public int read(final ByteBuffer dst) throws IOException {
75          if (dst == null) {
76              throw new IllegalArgumentException("Byte buffer may not be null");
77          }
78          if (this.completed) {
79              return -1;
80          }
81          int chunk = (int) Math.min((this.contentLength - this.len), Integer.MAX_VALUE);
82  
83          int bytesRead;
84          if (this.buffer.hasData()) {
85              int maxLen = Math.min(chunk, this.buffer.length());
86              bytesRead = this.buffer.read(dst, maxLen);
87          } else {
88              if (dst.remaining() > chunk) {
89                  int oldLimit = dst.limit();
90                  int newLimit = oldLimit - (dst.remaining() - chunk);
91                  dst.limit(newLimit);
92                  bytesRead = this.channel.read(dst);
93                  dst.limit(oldLimit);
94              } else {
95                  bytesRead = this.channel.read(dst);
96              }
97              if (bytesRead > 0) {
98                  this.metrics.incrementBytesTransferred(bytesRead);
99              }
100         }
101         if (bytesRead == -1) {
102             this.completed = true;
103             if (this.len < this.contentLength) {
104                 throw new ConnectionClosedException(
105                         "Premature end of Content-Length delimited message body (expected: "
106                         + this.contentLength + "; received: " + this.len);
107             }
108         }
109         this.len += bytesRead;
110         if (this.len >= this.contentLength) {
111             this.completed = true;
112         }
113         if (this.completed && bytesRead == 0) {
114             return -1;
115         } else {
116             return bytesRead;
117         }
118     }
119 
120     public long transfer(
121             final FileChannel dst,
122             long position,
123             long count) throws IOException {
124 
125         if (dst == null) {
126             return 0;
127         }
128         if (this.completed) {
129             return -1;
130         }
131 
132         int chunk = (int) Math.min((this.contentLength - this.len), Integer.MAX_VALUE);
133 
134         long bytesRead;
135         if (this.buffer.hasData()) {
136             int maxLen = Math.min(chunk, this.buffer.length());
137             dst.position(position);
138             bytesRead = this.buffer.read(dst, maxLen);
139         } else {
140             if (count > chunk) {
141                 count = chunk;
142             }
143             if (this.channel.isOpen()) {
144                 if (position > dst.size()) {
145                     throw new IOException("Position past end of file [" + position +
146                             " > " + dst.size() + "]");
147                 }
148                 bytesRead = dst.transferFrom(this.channel, position, count);
149             } else {
150                 bytesRead = -1;
151             }
152             if (bytesRead > 0) {
153                 this.metrics.incrementBytesTransferred(bytesRead);
154             }
155         }
156         if (bytesRead == -1) {
157             this.completed = true;
158             if (this.len < this.contentLength) {
159                 throw new ConnectionClosedException(
160                         "Premature end of Content-Length delimited message body (expected: "
161                         + this.contentLength + "; received: " + this.len);
162             }
163         }
164         this.len += bytesRead;
165         if (this.len >= this.contentLength) {
166             this.completed = true;
167         }
168         return bytesRead;
169     }
170 
171     @Override
172     public String toString() {
173         StringBuilder buffer = new StringBuilder();
174         buffer.append("[content length: ");
175         buffer.append(this.contentLength);
176         buffer.append("; pos: ");
177         buffer.append(this.len);
178         buffer.append("; completed: ");
179         buffer.append(this.completed);
180         buffer.append("]");
181         return buffer.toString();
182     }
183 }