io.netty.channel.FileRegion Maven / Gradle / Ivy
/*
* Copyright 2012 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.channel;
import io.netty.util.ReferenceCounted;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.nio.channels.WritableByteChannel;
/**
* A region of a file that is sent via a {@link Channel} which supports
* zero-copy file transfer.
*
* Upgrade your JDK / JRE
*
* {@link FileChannel#transferTo(long, long, WritableByteChannel)} has at least
* four known bugs in the old versions of Sun JDK and perhaps its derived ones.
* Please upgrade your JDK to 1.6.0_18 or later version if you are going to use
* zero-copy file transfer.
*
* - 5103988
* - FileChannel.transferTo() should return -1 for EAGAIN instead throws IOException
* - 6253145
* - FileChannel.transferTo() on Linux fails when going beyond 2GB boundary
* - 6427312
* - FileChannel.transferTo() throws IOException "system call interrupted"
* - 6470086
* - FileChannel.transferTo(2147483647, 1, channel) causes "Value too large" exception
*
*
* Check your operating system and JDK / JRE
*
* If your operating system (or JDK / JRE) does not support zero-copy file
* transfer, sending a file with {@link FileRegion} might fail or yield worse
* performance. For example, sending a large file doesn't work well in Windows.
*
* Not all transports support it
*/
public interface FileRegion extends ReferenceCounted {
/**
* Returns the offset in the file where the transfer began.
*/
long position();
/**
* Returns the bytes which was transferred already.
*
* @deprecated Use {@link #transferred()} instead.
*/
@Deprecated
long transfered();
/**
* Returns the bytes which was transferred already.
*/
long transferred();
/**
* Returns the number of bytes to transfer.
*/
long count();
/**
* Transfers the content of this file region to the specified channel.
*
* @param target the destination of the transfer
* @param position the relative offset of the file where the transfer
* begins from. For example, 0 will make the
* transfer start from {@link #position()}th byte and
* {@link #count()} - 1 will make the last
* byte of the region transferred.
*/
long transferTo(WritableByteChannel target, long position) throws IOException;
@Override
FileRegion retain();
@Override
FileRegion retain(int increment);
@Override
FileRegion touch();
@Override
FileRegion touch(Object hint);
}