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.FileChannel;
33 import java.nio.channels.WritableByteChannel;
34
35 import org.apache.http.annotation.NotThreadSafe;
36 import org.apache.http.impl.io.HttpTransportMetricsImpl;
37 import org.apache.http.nio.FileContentEncoder;
38 import org.apache.http.nio.reactor.SessionOutputBuffer;
39
40 /**
41 * Content encoder that writes data without any transformation. The end of
42 * the content entity is demarcated by closing the underlying connection
43 * (EOF condition). Entities transferred using this input stream can be of
44 * unlimited length.
45 * <p>
46 * This decoder is optimized to transfer data directly from
47 * a {@link FileChannel} to the underlying I/O session's channel whenever
48 * possible avoiding intermediate buffering in the session buffer.
49 *
50 * @since 4.0
51 */
52 @NotThreadSafe
53 public class IdentityEncoder extends AbstractContentEncoder
54 implements FileContentEncoder {
55
56 public IdentityEncoder(
57 final WritableByteChannel channel,
58 final SessionOutputBuffer buffer,
59 final HttpTransportMetricsImpl metrics) {
60 super(channel, buffer, metrics);
61 }
62
63 public int write(final ByteBuffer src) throws IOException {
64 if (src == null) {
65 return 0;
66 }
67 assertNotCompleted();
68 int bytesWritten = this.channel.write(src);
69 if (bytesWritten > 0) {
70 this.metrics.incrementBytesTransferred(bytesWritten);
71 }
72 return bytesWritten;
73 }
74
75 public long transfer(
76 final FileChannel src,
77 long position,
78 long count) throws IOException {
79
80 if (src == null) {
81 return 0;
82 }
83 assertNotCompleted();
84 long bytesWritten = src.transferTo(position, count, this.channel);
85 if (bytesWritten > 0) {
86 this.metrics.incrementBytesTransferred(bytesWritten);
87 }
88 return bytesWritten;
89 }
90
91 @Override
92 public String toString() {
93 StringBuilder buffer = new StringBuilder();
94 buffer.append("[identity; completed: ");
95 buffer.append(this.completed);
96 buffer.append("]");
97 return buffer.toString();
98 }
99
100 }