netty nio 示例

1

public class HttpServer {
    int port ;
    public HttpServer(int port){
        this.port = port;
    }
    public void start() throws Exception{
        ServerBootstrap bootstrap = new ServerBootstrap();
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup work = new NioEventLoopGroup();
        bootstrap.group(boss,work)
                .handler(new LoggingHandler(LogLevel.DEBUG))
                .channel(NioServerSocketChannel.class)
                .childHandler(new HttpServerInitializer());

        ChannelFuture f = bootstrap.bind(new InetSocketAddress(port)).sync();
        System.out.println(" server start up on port : " + port);
        f.channel().closeFuture().sync();
    }
}
1.bootstrap为启动引导器。
2.指定了使用两个时间循环器。EventLoopGroup
3.指定使用Nio模式。(NioServerSocketChannel.class)
4.初始化器为HttpServerInitializer

2

public class HttpServerInitializer extends ChannelInitializer<SocketChannel>{

    @Override
    protected void initChannel(SocketChannel channel) throws Exception {
        ChannelPipeline pipeline = channel.pipeline();
        pipeline.addLast(new HttpServerCodec());// http 编解码
        pipeline.addLast("httpAggregator",new HttpObjectAggregator(512*1024)); // http 消息聚合器                                                                     512*1024为接收的最大contentlength
        pipeline.addLast(new HttpRequestHandler());// 请求处理器
    }
}
1. channel 代表了一个socket.
2. ChannelPipeline 就是一个“羊肉串”,这个“羊肉串”里边的每一块羊肉就是一个 handler.
   handler分为两种,inbound handler,outbound handler 。顾名思义,分别处理 流入,流出。
3. HttpServerCodec 是 http消息的编解码器。
4. HttpObjectAggregator是Http消息聚合器,Aggregator这个单次就是“聚合,聚集”的意思。http消息在传输的过程中可能是一片片的消息片端,所以当服务器接收到的是一片片的时候,就需要HttpObjectAggregator来把它们聚合起来。
5. 接收到请求之后,你要做什么,准备怎么做,就在HttpRequestHandler中实现。

NIO 读写一个文件的流程

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;

public class NioTest {

    public static void main(String[] args) throws Exception {
        String inputFile = "nio/NioTest13_in.txt";
        String outFile = "nio/NioTest13_out.txt";
		// 1、创建文件流对象
        RandomAccessFile inputRandomAccessFile = new RandomAccessFile(inputFile, "r");//只读模式
        RandomAccessFile outRandomAccessFile = new RandomAccessFile(outFile, "rw");//读写模式
		// 2、创建通道
        FileChannel inputFileChannel= inputRandomAccessFile.getChannel(); // 获取读通道
        FileChannel outFileChannel= outRandomAccessFile.getChannel(); // 获取写通道

        // 创建内存映射文件
        // 3、给BUff分配空间
        MappedByteBuffer mappedByteBuffer = inputFileChannel.map(FileChannel.MapMode.READ_ONLY, 0, inputRandomAccessFile.length());

        System.out.println(mappedByteBuffer.get(12));

        Charset charset = Charset.forName("UTF-8");
        //Charset charset = Charset.forName("iso-8859-1");
        // 创建解码器
        CharsetDecoder charsetDecoder = charset.newDecoder();
        // 创建编码器
        CharsetEncoder charsetEncoder = charset.newEncoder();

        // 解码 字节->字符
        CharBuffer charBuffer = charsetDecoder.decode(mappedByteBuffer);
        // 编码 字符->字节
        ByteBuffer byteBuffer = charsetEncoder.encode(charBuffer);
		// 4、写入数据到Buffer
        outFileChannel.write(byteBuffer);
		// 5、调用clear()方法或者compact()方法
        inputFileChannel.close();
        outFileChannel.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值