Title: Message
The performance bottleneck is the lack of a buffer to download/write the file and the flush method invoked too often.
You're writing a byte, then flushing, which sends a TCP/IP packet down the wire. Since TCP/IP�packets have a 64 byte header and 512 bytes for data, you're wasting 511 bytes in each packet. Some app servers also have caches available, so this interferes with that as well.
 
Try this code:
 
try {
   
    bif = new BufferedInputStream(new FileInputStream("E:/abcd/download/xyz/"+fname));   
    stream = res.getOutputStream();
    byte [] buf = new byte[8192]; // 8KB buffer
    int data;
    while(( data = "bif.read(buf,0,8192))" != -1) {      
    stream.write(buf, 0, data);
    // stream.flush();    
    }
    bif.close();
   stream.close();   
   return res;
   } catch(Exception e) {
    System.out.println("The Exception is :"+e);
   }   
   finally {
   if (stream != null)
   stream.close();
   return res;
   } 
   }//end of method
 
Now, you'll be reading in 8KB chunks, and writing in 8KB chunks as well. I commented out the flush() method invocation, but you'll probably want to try with and without it and see which performs better. Also feel free to tweak the size of the buffer, always in 512 bytes increments and remember it should be at least 512 for optimum performance.
 
HTH,
 
Juan Pablo Lorandi
Chief Software Architect
Code Foundry Ltd.

Barberstown, Straffan, Co. Kildare, Ireland.
Tel: +353-1-6012050  Fax: +353-1-6012051
Mobile: +353-86-2157900
www.codefoundry.com
 
Disclaimer:
 
Opinions expressed are entirely personal and bear no relevance to opinions held by my employer.
Code Foundry Ltd.'s opinion is that I should get back to work.
-----Original Message-----
From: A mailing list for Enterprise JavaBeans development [mailto:[EMAIL PROTECTED]] On Behalf Of Anshul Dutta, Noida
Sent: Thursday, November 14, 2002 7:29 AM
To: [EMAIL PROTECTED]
Subject: Re: Performance improvement of Download Code

One bottleneck to performance is read operation from the hard disk.
I would suggest you to read the data from the file in the init method, put it in an instance variable and read it from there for all the requests.
-----Original Message-----
From: Dmitri Colebatch [mailto:[EMAIL PROTECTED]]
Sent: Thursday, November 14, 2002 11:06 AM
To: [EMAIL PROTECTED]
Subject: Re: Performance improvement of Download Code

I have no idea what the performance implications of this are, but if all you're doing is downloading the file, I wouldn't bother with BufferedInputStream.... why not just just FileInputStream as is....
 
alternatively, depending on your app server, you could make put the "download" directory under WEB-INF/downloads or something, and just redirect to that....  but some app servers dont like this despite its legitimacy being clarified in the 2.3 spec.
 
hth
dim
----- Original Message -----
From: ssmtech
Sent: Thursday, November 14, 2002 4:30 PM
Subject: Performance improvement of Download Code

Hello Sir,
             I am working on J2EE application Server and on EJB and trying to Download a "Text File" from our application server..
             I am using the Following code for downloading,but the thing is that,no doubt it downloads the Text file properly,it takes
             a LONG time to do so....
 
  <code>
  public HttpServletResponse doDownloadFile(HttpServletRequest req,HttpServletResponse res)
  throws ServletException,IOException {  
  
   ServletOutputStream stream = null;  
   res.setContentType("application/binary");
  
   String fname1 = req.getParameter("BusinessId");
   String fname2 = req.getParameter("AccountUserId");
   String fname =fname1+"-"+fname2+".txt";
   res.setHeader("Content-Disposition","inline; filename=\""+fname+"\";");
 
   BufferedInputStream bif = null;  
   try {
   
    bif = new BufferedInputStream(new FileInputStream("E:/abcd/download/xyz/"+fname));   
    stream = res.getOutputStream();
    int data;
    while(( data = "bif.read())" != -1) {      
    stream.write(data);
    stream.flush();    
    }
    bif.close();
   stream.close();   
   return res;
   } catch(Exception e) {
    System.out.println("The Exception is :"+e);
   }   
   finally {
   if (stream != null)
   stream.close();
   return res;
   } 
   }//end of method
 
</Code>
 
    Can anybody tell me how do i improve the Performance of the Code,or something is wrong with the code...
    Any suggestions or code snippets will be greatly appreciated...
 
Thanks a million is advance.
Regards
Sam
 
            

Reply via email to