java acm
java acm

Reputation: 1050

how can i write a file data by read chunk chunk and write to channel in netty?

i used netty 3.6 and i want to write a file data to a channel by any region and also with encryption
i write a file data with FileRegion on a channel and dont have any problem and worked as fine and dont eat my RAM but i want to read chunk chunk for encypted (by RC4)(chunk size is 512) befor wirte to channel , i used this code:

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();

     }


but i get Exception in thread "pool-5-thread-1" java.lang.OutOfMemoryError: Java heap space , i also used RandomAccessFile class but problem is as same , how can i resolved this problem

Upvotes: 0

Views: 935

Answers (2)

superzhu
superzhu

Reputation: 11

@normanm, The example you give used ChunkedFile in server side, can I use ChunkedFile when I posted a request?

Upvotes: 0

Norman Maurer
Norman Maurer

Reputation: 23557

Use ChunkedWriteHandler and ChunkedNioFile. Then you can intercept the produced ChannelBuffer and do the encryption on the fly.

Upvotes: 2

Related Questions