3

This is a piece of code to output a PDF file to browser, could it be faster?
This is implemented in a Java servlet.

private ByteArrayOutputStream getByteArrayOutputStreamFromFile(File file) throws Exception {
        BufferedInputStream bis = null;
        ByteArrayOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(file));
            bos = new ByteArrayOutputStream();
            byte[] byteContent = new byte[1024 * 1024];
            int len = 0;
            while ((len = bis.read(byteContent)) != -1) {
                bos.write(byteContent, 0, len);
            }
            return bos;
        } catch (Exception ex) {
            throw ex;
        } finally {
            if (bis != null) {
                bis.close();
            }
            if (bos != null) {
                bos.close();
            }
        }
    }
informatik01
  • 16,038
  • 10
  • 74
  • 104
lamwaiman1988
  • 3,729
  • 15
  • 55
  • 87
  • 4
    Why do you write to a `ByteArrayOutputStream` instead of directly to the outputStream of the response? Also 1M of buffer is probably not going to improve performance over a network. – Joachim Sauer Apr 08 '11 at 07:03

3 Answers3

5
 response.setContentType("application/pdf");
 ServletContext ctx = getServletContext();
 InputStream is = ctx.getResourceAsStream("/erp.pdf");

 int read =0;
 byte[] bytes = new byte[1024];
 OutputStream os = response.getOutputStream();
 while((read = is.read(bytes)) != -1)
 {
 os.write(bytes, 0, read);
 }
 os.flush();
 os.close();
Nirmal- thInk beYond
  • 11,847
  • 8
  • 35
  • 46
5

Using Google Guava you can summarize this in one line:

import com.google.common.io.Files;

private OutputStream getOutputStream(File file) throws IOException {
    return Files.newOutputStreamSupplier(file).getOutput();
}
yves amsellem
  • 7,106
  • 5
  • 44
  • 68
  • This is probably the best of the answers I've read so far, as it encapsulates buffering the input stream read in well tested code. – Steven Fines May 04 '11 at 16:35
  • 2
    @Dataknife: please note that the code in the question is very misleading. The OP is basically asking for the fastest way to write a PDF file to the servlet response, not for the fastest way to get an `OutputStream` of a PDF file. – BalusC May 04 '11 at 16:49
  • @BelusC: I'm agree but even his code do not have any reference to a servlet or equivalent. So, let's assume this is all about getting a stream out of a file. Please, comment this if I'm wrong. – yves amsellem May 04 '11 at 17:19
  • There's a `servlets` tag. Oh, it's BalusC with a. – BalusC May 04 '11 at 17:48
  • 2
    To make this servlet related just do this instead: import com.google.common.io.Files; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException{ Files.copy(File, response.getOutputStream()); } The code has be elided for brevity, but the intent should be clear... [http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html] (Guava Documentation) – Steven Fines May 04 '11 at 18:44
  • Do you guarentted it is faster and is reliable? – lamwaiman1988 May 05 '11 at 03:44
  • Guava is high-quality open-source. in case of doubt, check by yourself how it works. – yves amsellem May 05 '11 at 08:51
0

A suggestion:

always look to libraries such as Apache Commons FileUtils. They provide simple and easy to use methods.

You can also leave out the BufferedOutputStream as you're already using a buffer. But that's not going to make a big difference. Try using the nio instead of the streams. This might make some difference.

Also look at this: How to download and save a file from Internet using Java? might help you some way.

Community
  • 1
  • 1
Harry Joy
  • 58,650
  • 30
  • 162
  • 207