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.ConnectionClosedException;
36  import org.apache.hc.core5.http.impl.BasicHttpTransportMetrics;
37  import org.apache.hc.core5.http.nio.FileContentDecoder;
38  import org.apache.hc.core5.http.nio.SessionInputBuffer;
39  import org.apache.hc.core5.util.Args;
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}.
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  public class LengthDelimitedDecoder extends AbstractContentDecoder implements FileContentDecoder {
55  
56      private final long contentLength;
57  
58      private long len;
59  
60      public LengthDelimitedDecoder(
61              final ReadableByteChannel channel,
62              final SessionInputBuffer buffer,
63              final BasicHttpTransportMetrics metrics,
64              final long contentLength) {
65          super(channel, buffer, metrics);
66          Args.notNegative(contentLength, "Content length");
67          this.contentLength = contentLength;
68      }
69  
70      @Override
71      public int read(final ByteBuffer dst) throws IOException {
72          Args.notNull(dst, "Byte buffer");
73          if (isCompleted()) {
74              return -1;
75          }
76          final int chunk = (int) Math.min((this.contentLength - this.len), Integer.MAX_VALUE);
77  
78          final int bytesRead;
79          if (this.buffer.hasData()) {
80              final int maxLen = Math.min(chunk, this.buffer.length());
81              bytesRead = this.buffer.read(dst, maxLen);
82          } else {
83              bytesRead = readFromChannel(dst, chunk);
84          }
85          if (bytesRead == -1) {
86              setCompleted();
87              if (this.len < this.contentLength) {
88                  throw new ConnectionClosedException(
89                                  "Premature end of Content-Length delimited message body (expected: %d; received: %d)",
90                                  this.contentLength, this.len);
91              }
92          }
93          this.len += bytesRead;
94          if (this.len >= this.contentLength) {
95              this.completed = true;
96          }
97          if (this.completed && bytesRead == 0) {
98              return -1;
99          }
100         return bytesRead;
101     }
102 
103     @Override
104     public long transfer(
105             final FileChannel dst,
106             final long position,
107             final long count) throws IOException {
108 
109         if (dst == null) {
110             return 0;
111         }
112         if (isCompleted()) {
113             return -1;
114         }
115 
116         final int chunk = (int) Math.min((this.contentLength - this.len), Integer.MAX_VALUE);
117 
118         final long bytesRead;
119         if (this.buffer.hasData()) {
120             final int maxLen = Math.min(chunk, this.buffer.length());
121             dst.position(position);
122             bytesRead = this.buffer.read(dst, count < maxLen ? (int)count : maxLen);
123         } else {
124             if (this.channel.isOpen()) {
125                 if (position > dst.size()) {
126                     throw new IOException(String.format("Position past end of file [%d > %d]",
127                                     position, dst.size()));
128                 }
129                 bytesRead = dst.transferFrom(this.channel, position, count < chunk ? count : chunk);
130             } else {
131                 bytesRead = -1;
132             }
133             if (bytesRead > 0) {
134                 this.metrics.incrementBytesTransferred(bytesRead);
135             }
136         }
137         if (bytesRead == -1) {
138             setCompleted();
139             if (this.len < this.contentLength) {
140                 throw new ConnectionClosedException(
141                                 "Premature end of Content-Length delimited message body (expected: %d; received: %d)",
142                                 this.contentLength, this.len);
143             }
144         }
145         this.len += bytesRead;
146         if (this.len >= this.contentLength) {
147             this.completed = true;
148         }
149         return bytesRead;
150     }
151 
152     @Override
153     public String toString() {
154         final StringBuilder sb = new StringBuilder();
155         sb.append("[content length: ");
156         sb.append(this.contentLength);
157         sb.append("; pos: ");
158         sb.append(this.len);
159         sb.append("; completed: ");
160         sb.append(this.completed);
161         sb.append("]");
162         return sb.toString();
163     }
164 }