netty-自定义HttpPostMultipartRequestExDecoder

HttpPostMultipartRequestExDecoder.getFileUpload

    if(size == 0 && requestContentLength > CustomDiskFileUpload.DISK_FILE_THRESHOLD) {
              LOGGER.info("request content length:{}, size:{}, use disk file", requestContentLength, size);
          currentFileUpload = factory.createDiskFileUpload(request,
                  cleanString(nameAttribute.getValue()), cleanString(filenameAttribute.getValue()),
                  contentType, mechanism.value(), localCharset,
                  size);
      }

CustomHttpDataFactory

public FileUpload createDiskFileUpload(HttpRequest request, String name, String filename,
                                       String contentType, String contentTransferEncoding, Charset charset,
                                       long size) {
        FileUpload fileUpload = new CustomDiskFileUpload(name, filename, contentType,
                contentTransferEncoding, charset, size);
        fileUpload.setMaxSize(maxSize);
        checkHttpDataSize(fileUpload);
        List<HttpData> fileToDelete = getList(request);
        fileToDelete.add(fileUpload);
        return fileUpload;
    }      

HttpPostMultipartRequestExDecoder getFileUpload

if (!loadDataMultipart(undecodedChunk, delimiter, currentFileUpload)) {
            // Delimiter is not found. Need more chunks.
            return null;
        }   

HttpPostMultipartRequestExDecoder loadDataMultipart

    private static boolean loadDataMultipart(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
        if (!undecodedChunk.hasArray()) {
            return loadDataMultipartStandard(undecodedChunk, delimiter, httpData);
        }  

HttpPostMultipartRequestExDecoder
1.遍历一遍 是否是分隔符–CgHnWodRCoGzI1yH0FKcfd_7KlyPpaWfOv末尾
2.httpData.addContent(content, delimiterFound);
3.重置读index

 private static boolean loadDataMultipartStandard(ByteBuf undecodedChunk, String delimiter, HttpData httpData) {
        int pageSize = DEFAULT_PAGE_SIZE;
        final int startReaderIndex = undecodedChunk.readerIndex();
        final int delimeterLength = delimiter.length();
        int index = 0;
        int lastPosition = startReaderIndex;
        byte prevByte = HttpConstants.LF;
        boolean delimiterFound = false;
        boolean finish = false;
        byte[] bytes = new byte[pageSize];
        int len = 0,startIndex = 0;
        while (undecodedChunk.isReadable()) {
            if(httpData instanceof FileUpload){
                if(finish){
                    break;
                }
                try {
                    len = Math.min(pageSize, undecodedChunk.readableBytes());
                    startIndex = undecodedChunk.readerIndex();
                    undecodedChunk.readBytes(bytes, 0, len);
                    undecodedChunk.readerIndex(startIndex + len);
                    for (int i = 0; i < len; i++) {
                        final byte nextByte = bytes[i];
                        // Check the delimiter
                        if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
                            index++;
                            if (delimeterLength == index) {
                                delimiterFound = true;
                                finish = true;
                                break;
                            }
                            continue;
                        }
                        lastPosition = startIndex + i + 1;
                        if (nextByte == HttpConstants.LF) {
                            index = 0;
                            lastPosition -= (prevByte == HttpConstants.CR) ? 2 : 1;
                        }
                        prevByte = nextByte;
                    }
                }catch(Exception e){
                    throw e;
                }
            }else {
                final byte nextByte = undecodedChunk.readByte();
                // Check the delimiter
                if (prevByte == HttpConstants.LF && nextByte == delimiter.codePointAt(index)) {
                    index++;
                    if (delimeterLength == index) {
                        delimiterFound = true;
                        break;
                    }
                    continue;
                }
                lastPosition = undecodedChunk.readerIndex();
                if (nextByte == HttpConstants.LF) {
                    index = 0;
                    lastPosition -= (prevByte == HttpConstants.CR) ? 2 : 1;
                }
                prevByte = nextByte;
            }
        }
        if (prevByte == HttpConstants.CR) {
            lastPosition--;
        }

        ByteBuf content = undecodedChunk.retainedSlice(startReaderIndex, lastPosition - startReaderIndex);
        try {
            httpData.addContent(content, delimiterFound);
        } catch (IOException e) {
            throw new ErrorDataDecoderException(e);
        }
        undecodedChunk.readerIndex(lastPosition);
        return delimiterFound;
    }             

CustomDiskFileUpload.addContent

public void addContent(ByteBuf buffer, boolean last)
            throws IOException {
        if (buffer != null) {
            try {
                int localsize = buffer.readableBytes();
                checkSize(size + localsize);
                if (definedSize > 0 && definedSize < size + localsize) {
                    throw new IOException("Out of size: " + (size + localsize) +
                            " > " + definedSize);
                }
                ByteBuffer byteBuffer = null;
                ByteBuffer[] byteBuffers = null;
                if (buffer.nioBufferCount() == 1) {
                    byteBuffer = buffer.nioBuffer();
                } else {
                    byteBuffers = buffer.nioBuffers();
                }
                int written = 0;
                if (file == null) {
                    file = tempFile();
                }
                if (fileChannel == null) {
                    FileOutputStream outputStream = new FileOutputStream(file);
                    fileChannel = outputStream.getChannel();
                }
                int i = 0, len = byteBuffers != null ? byteBuffers.length : 0;
                ByteBuffer buf;
                while (written < localsize) {
                    if (byteBuffer != null) {
                        written += fileChannel.write(byteBuffer);
                    } else if (byteBuffers != null) {
                        written += fileChannel.write(byteBuffers);
                    }
                }
                size += localsize;
                buffer.readerIndex(buffer.readerIndex() + written);
            } finally {
                // Release the buffer as it was retained before and we not need a reference to it at all
                // See https://github.com/netty/netty/issues/1516
                buffer.release();
            }
        }
        if (last) {
            if (file == null) {
                file = tempFile();
            }
            if (fileChannel == null) {
                FileOutputStream outputStream = new FileOutputStream(file);
                fileChannel = outputStream.getChannel();
            }
            //fileChannel.force(false); //因为数据是临时存入ssd,所以不需要强制刷入磁盘
            fileChannel.close();
            fileChannel = null;
            setCompleted();
        } else {
            if (buffer == null) {
                throw new NullPointerException("buffer");
            }
        }
    }   
### Netty-all 和 Netty-buffer 的差异及其应用场景 #### 1. Netty-all 组件概述 `netty-all` 是一个聚合模块,包含了整个 Netty 框架所需的所有功能组件。该模块旨在提供一种简便的方式引入完整的 Netty 功能集到项目中,使得开发者无需单独管理多个依赖项。<groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>x.x.x.Final</version> </dependency> ``` #### 2. Netty-buffer 特定作用域分析 相比之下,`netty-buffer` 则专注于处理字节缓冲区的操作。它提供了高效的内存管理和数据传输机制,支持堆外内存(Direct Memory),这有助于减少垃圾回收压力并提高性能表现,在高并发场景下尤为重要。此外还实现了多种优化策略来增强读写效率,比如复合缓冲区 CompositeByteBuf 能够有效降低复制成本[^1]。 ```java // 创建一个新的 ByteBuf 实例 ByteBuf buffer = Unpooled.buffer(1024); try { // 向 Buffer 中写入一些数据 String message = "Hello, world!"; byte[] bytes = message.getBytes(CharsetUtil.UTF_8); buffer.writeBytes(bytes); // 将 Buffer 数据转换成字符串形式输出 System.out.println(buffer.toString(CharsetUtil.UTF_8)); } finally { // 使用完毕后释放资源 buffer.release(); } ``` #### 3. 应用场景对比 当应用程序只需要特定部分的功能时,可以选择只导入 `netty-buffer` 或其他单个子模块以减小程序体积;而如果需要全面运用 Netty 提供的各种网络通信能力,则推荐采用 `netty-all` 来简化构建配置过程。另外值得注意的是,虽然两者都可以用于开发基于异步I/O模式的服务端程序,但在实际部署过程中应当依据具体的业务需求和技术栈选型做出合理决策[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值