Take the 2-minute tour ×

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) {
            if (bos != null) {
share|improve this question
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 8 '11 at 7:03

3 Answers 3

up vote 5 down vote accepted

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();
share|improve this answer
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. –  Dataknife May 4 '11 at 16:35
@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 4 '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 4 '11 at 17:19
There's a servlets tag. Oh, it's BalusC with a. –  BalusC May 4 '11 at 17:48
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... [guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/… (Guava Documentation) –  Dataknife May 4 '11 at 18:44
 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);
share|improve this answer

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.

share|improve this answer

Your Answer


