1 /*
2 * Copyright 2012 The Netty Project
3 *
4 * The Netty Project licenses this file to you under the Apache License,
5 * version 2.0 (the "License"); you may not use this file except in compliance
6 * with the License. You may obtain a copy of the License at:
7 *
8 * https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations
14 * under the License.
15 */
16 package io.netty.channel;
17
18 import io.netty.util.ReferenceCounted;
19
20 import java.io.IOException;
21 import java.nio.channels.FileChannel;
22 import java.nio.channels.WritableByteChannel;
23
24 /**
25 * A region of a file that is sent via a {@link Channel} which supports
26 * <a href="https://en.wikipedia.org/wiki/Zero-copy">zero-copy file transfer</a>.
27 *
28 * <h3>Check your operating system and JDK / JRE</h3>
29 *
30 * If your operating system (or JDK / JRE) does not support zero-copy file
31 * transfer, sending a file with {@link FileRegion} might fail or yield worse
32 * performance. For example, sending a large file doesn't work well in Windows.
33 *
34 * <h3>Not all transports support it</h3>
35 */
36 public interface FileRegion extends ReferenceCounted {
37
38 /**
39 * Returns the offset in the file where the transfer began.
40 */
41 long position();
42
43 /**
44 * Returns the bytes which was transferred already.
45 *
46 * @deprecated Use {@link #transferred()} instead.
47 */
48 @Deprecated
49 long transfered();
50
51 /**
52 * Returns the bytes which was transferred already.
53 * <p>
54 * Note: some asynchronous transports (such as the {@code io_uring} transport when falling
55 * back to a chunked send for non-{@link DefaultFileRegion} implementations) advance this
56 * counter when bytes have been queued for submission, which may be before they reach the
57 * peer. If the channel is closed or the write fails after queuing, the reported value may
58 * overstate the number of bytes actually delivered.
59 */
60 long transferred();
61
62 /**
63 * Returns the number of bytes to transfer.
64 */
65 long count();
66
67 /**
68 * Transfers the content of this file region to the specified channel.
69 *
70 * @param target the destination of the transfer
71 * @param position the relative offset of the file where the transfer
72 * begins from. For example, <tt>0</tt> will make the
73 * transfer start from {@link #position()}th byte and
74 * <tt>{@link #count()} - 1</tt> will make the last
75 * byte of the region transferred.
76 */
77 long transferTo(WritableByteChannel target, long position) throws IOException;
78
79 @Override
80 FileRegion retain();
81
82 @Override
83 FileRegion retain(int increment);
84
85 @Override
86 FileRegion touch();
87
88 @Override
89 FileRegion touch(Object hint);
90 }