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.WritableByteChannel;
33  
34  import org.apache.http.annotation.NotThreadSafe;
35  import org.apache.http.impl.io.HttpTransportMetricsImpl;
36  import org.apache.http.io.BufferInfo;
37  import org.apache.http.nio.reactor.SessionOutputBuffer;
38  import org.apache.http.util.CharArrayBuffer;
39  
40  /**
41   * Implements chunked transfer coding. The content is sent in small chunks.
42   * Entities transferred using this decoder can be of unlimited length.
43   *
44   * @since 4.0
45   */
46  @NotThreadSafe
47  public class ChunkEncoder extends AbstractContentEncoder {
48  
49      private final CharArrayBuffer lineBuffer;
50  
51      private final BufferInfo bufferinfo;
52  
53      public ChunkEncoder(
54              final WritableByteChannel channel,
55              final SessionOutputBuffer buffer,
56              final HttpTransportMetricsImpl metrics) {
57          super(channel, buffer, metrics);
58          this.lineBuffer = new CharArrayBuffer(16);
59          if (buffer instanceof BufferInfo) {
60              this.bufferinfo = (BufferInfo) buffer;
61          } else {
62              this.bufferinfo = null;
63          }
64      }
65  
66      public int write(final ByteBuffer src) throws IOException {
67          if (src == null) {
68              return 0;
69          }
70          assertNotCompleted();
71          int chunk = src.remaining();
72          if (chunk == 0) {
73              return 0;
74          }
75  
76          long bytesWritten = this.buffer.flush(this.channel);
77          if (bytesWritten > 0) {
78              this.metrics.incrementBytesTransferred(bytesWritten);
79          }
80          int avail;
81          if (this.bufferinfo != null) {
82              avail = this.bufferinfo.available();
83          } else {
84              avail = 4096;
85          }
86  
87          // subtract the length of the longest chunk header
88          // 12345678\r\n
89          // <chunk-data>\r\n
90          avail -= 12;
91          if (avail <= 0) {
92              return 0;
93          } else if (avail < chunk) {
94              // write no more than 'avail' bytes
95              chunk = avail;
96              this.lineBuffer.clear();
97              this.lineBuffer.append(Integer.toHexString(chunk));
98              this.buffer.writeLine(this.lineBuffer);
99              int oldlimit = src.limit();
100             src.limit(src.position() + chunk);
101             this.buffer.write(src);
102             src.limit(oldlimit);
103         } else {
104             // write all
105             this.lineBuffer.clear();
106             this.lineBuffer.append(Integer.toHexString(chunk));
107             this.buffer.writeLine(this.lineBuffer);
108             this.buffer.write(src);
109         }
110         this.lineBuffer.clear();
111         this.buffer.writeLine(this.lineBuffer);
112         return chunk;
113     }
114 
115     @Override
116     public void complete() throws IOException {
117         assertNotCompleted();
118         this.lineBuffer.clear();
119         this.lineBuffer.append("0");
120         this.buffer.writeLine(this.lineBuffer);
121         this.lineBuffer.clear();
122         this.buffer.writeLine(this.lineBuffer);
123         this.completed = true; // == super.complete()
124     }
125 
126     @Override
127     public String toString() {
128         StringBuilder buffer = new StringBuilder();
129         buffer.append("[chunk-coded; completed: ");
130         buffer.append(this.completed);
131         buffer.append("]");
132         return buffer.toString();
133     }
134 
135 }