~ubuntu-branches/ubuntu/trusty/httpcomponents-core/trusty

« back to all changes in this revision

Viewing changes to httpcore-nio/src/main/java/org/apache/http/impl/nio/codecs/ChunkEncoder.java

  • Committer: Bazaar Package Importer
  • Author(s): David Paleino
  • Date: 2010-06-12 08:37:34 UTC
  • Revision ID: james.westby@ubuntu.com-20100612083734-1y8kp6qm4sjk60az
Tags: upstream-4.0.1
ImportĀ upstreamĀ versionĀ 4.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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 $
 
3
 * $Revision: 744538 $
 
4
 * $Date: 2009-02-14 18:20:23 +0100 (Sat, 14 Feb 2009) $
 
5
 *
 
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
 
14
 *
 
15
 *   http://www.apache.org/licenses/LICENSE-2.0
 
16
 *
 
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
 
22
 * under the License.
 
23
 * ====================================================================
 
24
 *
 
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/>.
 
29
 *
 
30
 */
 
31
 
 
32
package org.apache.http.impl.nio.codecs;
 
33
 
 
34
import java.io.IOException;
 
35
import java.nio.ByteBuffer;
 
36
import java.nio.channels.WritableByteChannel;
 
37
 
 
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;
 
42
 
 
43
/**
 
44
 * Implements chunked transfer coding. The content is sent in small chunks.
 
45
 * Entities transferred using this decoder can be of unlimited length. 
 
46
 *  
 
47
 *
 
48
 * @version $Revision: 744538 $
 
49
 *
 
50
 * @since 4.0
 
51
 */
 
52
public class ChunkEncoder extends AbstractContentEncoder {
 
53
    
 
54
    private final CharArrayBuffer lineBuffer;
 
55
    
 
56
    private final BufferInfo bufferinfo;
 
57
    
 
58
    public ChunkEncoder(
 
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;
 
65
    }
 
66
 
 
67
    public int write(final ByteBuffer src) throws IOException {
 
68
        if (src == null) {
 
69
            return 0;
 
70
        }
 
71
        assertNotCompleted();
 
72
        int chunk = src.remaining();
 
73
        if (chunk == 0) {
 
74
            return 0;
 
75
        }
 
76
        
 
77
        long bytesWritten = this.buffer.flush(this.channel);
 
78
        if (bytesWritten > 0) {
 
79
            this.metrics.incrementBytesTransferred(bytesWritten);
 
80
        }
 
81
        int avail = this.bufferinfo.available();
 
82
        if (avail == 0) {
 
83
            return 0;
 
84
        }
 
85
 
 
86
        // subtract the length of the longest chunk header
 
87
        // 12345678\r\n
 
88
        avail -= 10;
 
89
 
 
90
        if (avail < chunk) {
 
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);
 
98
            src.limit(oldlimit);
 
99
        } else {
 
100
            // write all
 
101
            this.lineBuffer.clear();
 
102
            this.lineBuffer.append(Integer.toHexString(chunk));
 
103
            this.buffer.writeLine(this.lineBuffer);
 
104
            this.buffer.write(src);
 
105
        }
 
106
        this.lineBuffer.clear();
 
107
        this.buffer.writeLine(this.lineBuffer);
 
108
        return chunk;
 
109
    }
 
110
 
 
111
    @Override
 
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()
 
120
    }
 
121
    
 
122
    @Override
 
123
    public String toString() {
 
124
        StringBuffer buffer = new StringBuffer();
 
125
        buffer.append("[chunk-coded; completed: ");
 
126
        buffer.append(this.completed);
 
127
        buffer.append("]");
 
128
        return buffer.toString();
 
129
    }
 
130
    
 
131
}