簡體   English   中英

如何調整BufferedInputStream read()?

[英]how to tune BufferedInputStream read()?

我正在從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圖像數據。 那么,有什么辦法可以減少我的時間呢?

鑒於blob已經具有緩沖區的概念,實際上有可能實際上完全通過使用BufferedInputStream 損害了性能-它可能會進行較小的read()調用,進行了比必要的更多的網絡調用。

嘗試完全擺脫BufferedInputStream ,僅直接從Blob的二進制流中讀取即可。 這只是一個想法,但值得一試。 哦,您不必每次編寫時都刷新輸出流。

(順便說一句,您應該b在finally塊中關閉流-否則,如果發生任何異常,您將泄漏句柄。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM