views:

139

answers:

1

I am reading a BLOB column from a Oracle database, then writing it to a file as follows:

    public static int execute(String filename, BLOB blob)
  {

    int success = 1;

    try
    {
      File              blobFile   = new File(filename);
      FileOutputStream  outStream  = new FileOutputStream(blobFile);
      BufferedInputStream inStream   = new BufferedInputStream(blob.getBinaryStream());

      int     length  = -1;
      int     size    = blob.getBufferSize();
      byte[]  buffer  = new byte[size];

      while ((length = inStream.read(buffer)) != -1)
      {
        outStream.write(buffer, 0, length);
        outStream.flush();
      }

      inStream.close();
      outStream.close();
    }
    catch (Exception e)
    {
      e.printStackTrace();
      System.out.println("ERROR(img_exportBlob) Unable to export:"+filename);
      success = 0;
    }
}

The file size is around 3MB and it takes 40-50s to read the buffer. Its actually a 3D image data. So, is there any way by which I can reduce this time?

+2  A: 

Given that the blob already has the concept of a buffer, it's possible that you're actually harming performance by using the BufferedInputStream at all - it may be making smaller read() calls, making more network calls than necessary.

Try getting rid of the BufferedInputStream completely, just reading directly from the blob's binary stream. It's only a thought, but worth a try. Oh, and you don't need to flush the output stream every time you write.

(As an aside, you should b closing streams in finally blocks - otherwise you'll leak handles if anything throws an exception.)

Jon Skeet