some example to send big file from server to client?

Frederic Bregier fredbregier at free.fr
Thu May 21 14:48:04 EDT 2009


Hi,

I think that you can even improve it by taking care of write ability like
"discard" example shows. Indeed, in your code, if the network link is slow,
you can have the side effect in the client to fill the write queue so much
that there will be an OOME.

A simple implementation idea could be as follow (from your code example):

                FileInputStream fis = new FileInputStream(f);
                int len;
                byte[] bs = new byte[1024];
                while ((len = fis.read(bs)) > 0) {
                    ChannelBuffer buffer = ChannelBuffers.copiedBuffer(bs);
                    // directly create and copy the ChannelBuffer from the
byte array
                    while (! channel.isWritable()) {
                         Thread.sleep(10); // wait a while (maybe more than
10ms)
                         // until channel is writable again
                    }
                    channel.write(buffer);
                }
                fis.close();

Of course, if you have a client Handler, then you can directly code this in
the handler such that you can even prevent your code of this active wait
loop (see Discard example to see how).

HTH too,
Frederic


tantom wrote:
> 
> hi
> 
> thanks,it's ok. below it's my test code,i post it hope to help next who
> encount same with me:)
> 
>  File f = new File("g:\\test.msi");
>                 String params = "GetFile|test.msi";
>                 byte[] bsParams = params.getBytes("UTF-8");
>                 ChannelBuffer response = ChannelBuffers.buffer(8 +
> bsParams.length);
>                
> response.writeBytes(CTypeConvert.int2byte(bsParams.length));
>                 response.writeBytes(CTypeConvert.int2byte((int)
> f.length()));
>                 response.writeBytes(bsParams);
> 
>                 //write the header info first
>                 channel.write(response);
> 
>                 
>                 FileInputStream fis = new FileInputStream(f);
>                 int len;
>                 byte[] bs = new byte[1024];
>                 while ((len = fis.read(bs)) > 0) {
>                     ChannelBuffer buffer = ChannelBuffers.buffer(len);
>                     buffer.writeBytes(bs);
>                     channel.write(buffer);
>                 }
>                 fis.close();
> 


-----
Hardware/Software Architect
-- 
View this message in context: http://n2.nabble.com/some-example-to-send-big-file-from-server-to-client--tp2952981p2953620.html
Sent from the Netty User Group mailing list archive at Nabble.com.




More information about the netty-users mailing list