【问题标题】:Need to improve performance in file unzipping from DB需要提高从数据库解压文件的性能
【发布时间】:2015-01-03 23:02:25
【问题描述】:

我正在从 db 获取压缩 blob,并以下列方式使用该 blob,

例如:-

byte[] inputBlob = blobfile.getBytes(1, (int) blobfile.length());

得到 blob 后,我得到 zippedStream 并将其传递给另一个 Class 方法(解压缩)。

例如:-

ByteArrayOutputStream zippedStream = null;
InputStream byteInputStream = null;
IParser parser = null;
byte[] buffer = null;
try {
        zippedStream = new ByteArrayOutputStream();
        byteInputStream = new ByteArrayInputStream(blob);
        blob = null;
        int bytes_read;
        buffer = new byte[byteInputStream.available()];

        while ((bytes_read = byteInputStream.read(buffer)) > 0) {
            zippedStream.write(buffer, 0, bytes_read);
        }
        buffer = null;
        byteInputStream.close();
        byteInputStream = null;

    }catch(Exception e){
        e.printStackTrace();
    }

解压方法: 例如:-

byte[] buffer = new byte[1024];
try {
    InputStream decodedInput = new ByteArrayInputStream(zippedStream.toByteArray());
    zippedStream.close();
    zippedStream = null;
    GZIPInputStream unzippedStream = new GZIPInputStream(decodedInput);
    decodedInput.close();
    decodedInput = null;
    int bytes_read;
    unzippedOutputstream = new ByteArrayOutputStream();
    while ((bytes_read = unzippedStream.read(buffer)) > 0) {
        unzippedOutputstream.write(buffer, 0, bytes_read);
    }
    buffer = null;
    unzippedStream.close();
    unzippedStream = null;
} catch (Exception ex) {
    logger.setException(ex);
    logger.error("unzipper", generateMsg("Exception occurred"));
}

使用这种方式,我的应用程序卡住了一段时间,性能非常糟糕。 是否有任何优化方法来获取 zippedstream 文件并轻松解压缩?

【问题讨论】:

  • 伙计,你的格式……没有人应该考虑阅读这篇文章。
  • 原谅。但是,请查看内容并帮助我。

标签: java string performance inputstream bufferedinputstream


【解决方案1】:

真的需要所有这些缓冲吗?你能用 IParser 解析 Stream 吗?

 InputStream zippedStream = ...
 IParser parser = ...
 parser.parse(new GZIPInputStream(zippedStream));

这将读取压缩数据,并在执行过程中解压缩,效率更高。

【讨论】:

  • 你能给出一个避免上述方法性能的想法吗?
  • 我不明白你的意思。解析器可以接受输入流吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-08
  • 2022-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-01
相关资源
最近更新 更多