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.WritableByteChannel;
33  import java.util.List;
34  
35  import org.apache.hc.core5.http.FormattedHeader;
36  import org.apache.hc.core5.http.Header;
37  import org.apache.hc.core5.http.impl.BasicHttpTransportMetrics;
38  import org.apache.hc.core5.http.message.BasicLineFormatter;
39  import org.apache.hc.core5.http.nio.SessionOutputBuffer;
40  import org.apache.hc.core5.util.CharArrayBuffer;
41  
42  /**
43   * Implements chunked transfer coding. The content is sent in small chunks.
44   * Entities transferred using this decoder can be of unlimited length.
45   *
46   * @since 4.0
47   */
48  public class ChunkEncoder extends AbstractContentEncoder {
49  
50      private final int chunkSizeHint;
51      private final CharArrayBuffer lineBuffer;
52  
53      /**
54       * @param channel underlying channel.
55       * @param buffer  session buffer.
56       * @param metrics transport metrics.
57       *
58       * @since 5.0
59       */
60      public ChunkEncoder(
61              final WritableByteChannel channel,
62              final SessionOutputBuffer buffer,
63              final BasicHttpTransportMetrics metrics,
64              final int chunkSizeHint) {
65          super(channel, buffer, metrics);
66          this.chunkSizeHint = chunkSizeHint > 0 ? chunkSizeHint : 0;
67          this.lineBuffer = new CharArrayBuffer(16);
68      }
69  
70      public ChunkEncoder(
71              final WritableByteChannel channel,
72              final SessionOutputBuffer buffer,
73              final BasicHttpTransportMetrics metrics) {
74          this(channel, buffer, metrics, 0);
75      }
76  
77      @Override
78      public int write(final ByteBuffer src) throws IOException {
79          if (src == null) {
80              return 0;
81          }
82          assertNotCompleted();
83  
84          int total = 0;
85          while (src.hasRemaining()) {
86              int chunk = src.remaining();
87              int avail;
88              avail = this.buffer.capacity();
89  
90              // subtract the length of the longest chunk header
91              // 12345678\r\n
92              // <chunk-data>\r\n
93              avail -= 12;
94              if (avail > 0) {
95                  if (avail < chunk) {
96                      // write no more than 'avail' bytes
97                      chunk = avail;
98                      this.lineBuffer.clear();
99                      this.lineBuffer.append(Integer.toHexString(chunk));
100                     this.buffer.writeLine(this.lineBuffer);
101                     final int oldlimit = src.limit();
102                     src.limit(src.position() + chunk);
103                     this.buffer.write(src);
104                     src.limit(oldlimit);
105                 } else {
106                     // write all
107                     this.lineBuffer.clear();
108                     this.lineBuffer.append(Integer.toHexString(chunk));
109                     this.buffer.writeLine(this.lineBuffer);
110                     this.buffer.write(src);
111                 }
112                 this.lineBuffer.clear();
113                 this.buffer.writeLine(this.lineBuffer);
114                 total += chunk;
115             }
116             if (this.buffer.length() >= this.chunkSizeHint || src.hasRemaining()) {
117                 final int bytesWritten = flushToChannel();
118                 if (bytesWritten == 0) {
119                     break;
120                 }
121             }
122         }
123         return total;
124     }
125 
126     @Override
127     public void complete(final List<? extends Header> trailers) throws IOException {
128         assertNotCompleted();
129         this.lineBuffer.clear();
130         this.lineBuffer.append("0");
131         this.buffer.writeLine(this.lineBuffer);
132         writeTrailers(trailers);
133         this.lineBuffer.clear();
134         this.buffer.writeLine(this.lineBuffer);
135         super.complete(trailers);
136     }
137 
138     private void writeTrailers(final List<? extends Header> trailers) throws IOException {
139         if (trailers != null) {
140             for (int i = 0; i < trailers.size(); i++) {
141                 final Header header = trailers.get(i);
142                 if (header instanceof FormattedHeader) {
143                     final CharArrayBuffer chbuffer = ((FormattedHeader) header).getBuffer();
144                     buffer.writeLine(chbuffer);
145                 } else {
146                     this.lineBuffer.clear();
147                     BasicLineFormatter.INSTANCE.formatHeader(this.lineBuffer, header);
148                     buffer.writeLine(this.lineBuffer);
149                 }
150             }
151         }
152     }
153 
154     @Override
155     public String toString() {
156         final StringBuilder sb = new StringBuilder();
157         sb.append("[chunk-coded; completed: ");
158         sb.append(isCompleted());
159         sb.append("]");
160         return sb.toString();
161     }
162 
163 }