2
* $HeadURL: https://svn.apache.org/repos/asf/httpcomponents/httpcore/tags/4.0.1/httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java $
4
* $Date: 2009-02-14 18:20:23 +0100 (Sat, 14 Feb 2009) $
6
* ====================================================================
7
* Licensed to the Apache Software Foundation (ASF) under one
8
* or more contributor license agreements. See the NOTICE file
9
* distributed with this work for additional information
10
* regarding copyright ownership. The ASF licenses this file
11
* to you under the Apache License, Version 2.0 (the
12
* "License"); you may not use this file except in compliance
13
* with the License. You may obtain a copy of the License at
15
* http://www.apache.org/licenses/LICENSE-2.0
17
* Unless required by applicable law or agreed to in writing,
18
* software distributed under the License is distributed on an
19
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
20
* KIND, either express or implied. See the License for the
21
* specific language governing permissions and limitations
23
* ====================================================================
25
* This software consists of voluntary contributions made by many
26
* individuals on behalf of the Apache Software Foundation. For more
27
* information on the Apache Software Foundation, please see
28
* <http://www.apache.org/>.
32
package org.apache.http.impl.nio.codecs;
34
import java.io.IOException;
35
import java.nio.ByteBuffer;
36
import java.nio.channels.WritableByteChannel;
38
import org.apache.http.impl.io.HttpTransportMetricsImpl;
39
import org.apache.http.nio.reactor.SessionOutputBuffer;
40
import org.apache.http.nio.util.BufferInfo;
41
import org.apache.http.util.CharArrayBuffer;
44
* Implements chunked transfer coding. The content is sent in small chunks.
45
* Entities transferred using this decoder can be of unlimited length.
48
* @version $Revision: 744538 $
52
public class ChunkEncoder extends AbstractContentEncoder {
54
private final CharArrayBuffer lineBuffer;
56
private final BufferInfo bufferinfo;
59
final WritableByteChannel channel,
60
final SessionOutputBuffer buffer,
61
final HttpTransportMetricsImpl metrics) {
62
super(channel, buffer, metrics);
63
this.lineBuffer = new CharArrayBuffer(16);
64
this.bufferinfo = (BufferInfo) buffer;
67
public int write(final ByteBuffer src) throws IOException {
72
int chunk = src.remaining();
77
long bytesWritten = this.buffer.flush(this.channel);
78
if (bytesWritten > 0) {
79
this.metrics.incrementBytesTransferred(bytesWritten);
81
int avail = this.bufferinfo.available();
86
// subtract the length of the longest chunk header
91
// write no more than 'avail' bytes
92
this.lineBuffer.clear();
93
this.lineBuffer.append(Integer.toHexString(avail));
94
this.buffer.writeLine(this.lineBuffer);
95
int oldlimit = src.limit();
96
src.limit(src.position() + avail);
97
this.buffer.write(src);
101
this.lineBuffer.clear();
102
this.lineBuffer.append(Integer.toHexString(chunk));
103
this.buffer.writeLine(this.lineBuffer);
104
this.buffer.write(src);
106
this.lineBuffer.clear();
107
this.buffer.writeLine(this.lineBuffer);
112
public void complete() throws IOException {
113
assertNotCompleted();
114
this.lineBuffer.clear();
115
this.lineBuffer.append("0");
116
this.buffer.writeLine(this.lineBuffer);
117
this.lineBuffer.clear();
118
this.buffer.writeLine(this.lineBuffer);
119
this.completed = true; // == super.complete()
123
public String toString() {
124
StringBuffer buffer = new StringBuffer();
125
buffer.append("[chunk-coded; completed: ");
126
buffer.append(this.completed);
128
return buffer.toString();