2014-01-08 31 views
0

我使用netty 3.6,我想写一个文件数据到任何地区的通道,也加密
我写一个文件数据FileRegion在一个通道上,没有任何问题,工作得很好,不吃我的RAM,但我想读块的块为encypted(由RC4)(块大小为512)befor WIRTE到信道,我使用此代码:
如何通过读取块大块写入文件数据并在netty中写入通道?

if (e.getChannel().isWritable()) { 

    FileInputStream fin = new FileInputStream(file); 

    byte[] data = new byte[512]; 

     for (int i = 512; i < file.length(); i += 512) { 

         fin.read(data); 
         index_range += 512; 
         e.getChannel().write(ChannelBuffers.wrappedBuffer(RC4.encrypte(data))); 

        } 

        int remain_len=(int)(file.length() - index_range); 
        if(remain_len>0){ 
        data = new byte[remain_len]; 
        fin.read(data); 
        e.getChannel().write(ChannelBuffers.wrappedBuffer(RC4.encrypte(data))); 
        } 
     fin.close(); 

    } 


但我得到在螺纹异常“池-5-螺纹-1“java.lang.OutOfMemoryError:Java堆空间,我也用RandomAccessFile class但问题是一样的,我该如何解决这个问题

回答

2

使用ChunkedWriteHandler和ChunkedNioFile。然后你可以拦截生成的ChannelBuffer并且即时进行加密。

+0

感谢@normanm,我搜索看到任何例子来做到这一点,但我找不到任何,你能告诉我你的解释在示例代码请 –

+0

https://github.com/netty/netty/blob/3 /src/main/java/org/jboss/netty/example/http/file/HttpStaticFileServerHandler.java#L172 –

+0

非常感谢netty master –

0

@normanm,您在服务器端使用ChunkedFile的示例,我可以在发布请求时使用ChunkedFile吗?