qq_35513234 2021-08-16 18:35 采纳率: 0%
浏览 36

netty 的ByteBuf 到达2048后不自动扩容,怎么办?

public class ChatCodec extends ByteToMessageCodec<ChatMessage> {

    @Override
    protected void encode(ChannelHandlerContext channelHandlerContext, ChatMessage chatMessage, ByteBuf out) throws Exception {
        System.out.println("开始编码");
        out.writeInt(chatMessage.getMagic());
        out.writeByte(chatMessage.getVersion());
        out.writeByte(chatMessage.getType());
        out.writeByte(chatMessage.getCharset());
        if(chatMessage.getType()==0){
            out.writeInt(chatMessage.getContentSize());
            out.writeBytes(chatMessage.getContent().getBytes());
        }else {
            out.writeInt(chatMessage.getNameSize());
            out.writeCharSequence(chatMessage.getNameTail(),StandardCharsets.UTF_8);
            out.writeInt(chatMessage.getContentSize());
            out.writeCharSequence(chatMessage.getContent(),StandardCharsets.UTF_8);
            out.writeLong(chatMessage.getDataSize());
            FileChannel fileChannel = new FileInputStream(chatMessage.getContent()).getChannel();
            System.out.println(System.currentTimeMillis());
            long c = 0;
            long size = chatMessage.getDataSize();
            while (size>100){
//   写入数据会自动 扩容    从256  256*2   256*2*2   .......
                System.out.println(out.writerIndex()+"     "+ out.capacity());
                size = size-100;
                int i = out.writeBytes(fileChannel, c, 100);
                System.out.println(out.isWritable());
                c=c+100;
            }
            out.writeBytes(fileChannel,c, (int) size);
            fileChannel.close();
            System.out.println("文件发送完毕");
        }


    }

    @Override
    protected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("开始解码");
        ChatMessage chatMessage = new ChatMessage();
        chatMessage.setMagic(in.readInt()).setVersion(in.readByte())
                .setType(in.readByte()).setCharset(in.readByte());
        if(chatMessage.getType() == 0){
            chatMessage.setContentSize(in.readInt());
            CharSequence charSequence = in.readCharSequence(chatMessage.getContentSize(), StandardCharsets.UTF_8);
            chatMessage.setContent(charSequence.toString());
            out.add(chatMessage);
        }else {
            chatMessage.setNameSize(in.readInt());
            chatMessage.setNameTail(in.readCharSequence(chatMessage.getNameSize(),StandardCharsets.UTF_8).toString());
            chatMessage.setContentSize(in.readInt());
            CharSequence charSequence = in.readCharSequence(chatMessage.getContentSize(), StandardCharsets.UTF_8);
            chatMessage.setContent(charSequence.toString());
            chatMessage.setDataSize(in.readLong());
            out.add(chatMessage);
            String path ="C:\\Users\\艾桑\\Desktop\\数据生成器\\netty\\netty-50000\\src\\main\\resources\\static\\download\\";
            File file1 = new File(path);
            if(file1.isDirectory()){
                file1.mkdirs();
            }
            File file = new File(path + UUID.randomUUID() + chatMessage.getNameTail());
            try {
                file.createNewFile();
            }catch (Exception e){
                e.printStackTrace();
            }
            long size = chatMessage.getDataSize();
            FileChannel fileChannel = new FileOutputStream(file).getChannel();
//  输出到文件只能输出剩下的字节数, 输出后不能扩容  ByteBuf为不可读状态  reader和writer都为2048
//  那么请问如何读取剩下的数据呢?  急
            while (size>0){
                try {
                    if(!in.isReadable()){
                        in.markWriterIndex();
                    }
                    int i = in.readBytes(fileChannel,in.readableBytes());
                    size =size -i;
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
            fileChannel.close();
            System.out.println(System.currentTimeMillis());
            System.out.println("文件接收完毕");

        }

    }


  • 写回答

1条回答 默认 最新

  • 关注

    发送大文件都是要循环分块处理的,定义字节数组为2048大小,循环处理即可。

    评论

报告相同问题?

问题事件

  • 创建了问题 8月16日

悬赏问题

  • ¥15 依据报错在原代吗格式的基础上解决问题
  • ¥15 在虚拟机中安装flash code
  • ¥15 单片机stm32f10x编写光敏电阻调节3.3伏大功率灯亮度(光强越大灯越暗,白天正常光强灯不亮,使用ADC,PWM等模块)望各位找一下错误或者提供一个可实现功能的代码
  • ¥20 verilog状态机方法流水灯
  • ¥15 pandas代码实现不了意图
  • ¥15 GD32H7 从存储器到外设SPI传输数据无法重复启用DMA
  • ¥25 LT码在高斯信道下的误码率仿真
  • ¥45 渲染完成之后将物体的材质贴图改变,自动化进行这个操作
  • ¥15 yolov5目标检测并显示目标出现的时间或视频帧
  • ¥15 电视版的优酷可以设置电影连续播放吗?