Package com.manning.nettyinaction.chapter8

Source Code of com.manning.nettyinaction.chapter8.DefaultFileRegionUsage

package com.manning.nettyinaction.chapter8;

import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.DefaultFileRegion;
import io.netty.channel.FileRegion;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
* @author <a href="mailto:nmaurer@redhat.com">Norman Maurer</a>
*/
public class DefaultFileRegionUsage {
    public static void transfer(Channel channel, File file) throws FileNotFoundException {
        FileInputStream in = new FileInputStream(file);
        FileRegion region = new DefaultFileRegion(in.getChannel(), 0, file.length());

        channel.writeAndFlush(region).addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (!future.isSuccess()) {
                    Throwable cause = future.cause();
                    // Do something
                }
            }
        });
    }
}
TOP

Related Classes of com.manning.nettyinaction.chapter8.DefaultFileRegionUsage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.