如何调整BufferedInputStream read()?

时间:2010-04-02 07:37:29

标签: java bufferedinputstream

我正在从Oracle数据库中读取BLOB列,然后将其写入文件,如下所示:

    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;
    }
}

文件大小约为3MB,读取缓冲区需要40-50秒。它实际上是一个3D图像数据。那么,有什么方法可以减少这个时间吗?

1 个答案:

答案 0 :(得分:3)

鉴于blob已经具有缓冲区的概念,您可能会使用BufferedInputStream实际上伤害性能 - 它可能会变小{{1调用,进行超出必要的网络调用。

尝试完全摆脱read(),直接从blob的二进制流中读取。这只是一个想法,但值得一试。哦,每次编写时都不需要刷新输出流。

(顺便说一下,你应该在finally块中关闭流 - 否则如果有什么抛出异常,你就会泄漏句柄。)