【问题标题】:Why is my method not continuing after calling InflateObject.inflate(byte[] array)?为什么调用 InflateObject.inflate(byte[] array) 后我的方法没有继续?
【发布时间】:2014-08-20 11:50:01
【问题描述】:

我试图创建一个必须对文件内容进行解密的程序。

我的代码如下,问题是在我调用decompresser.inflate(result) 之后,此方法中不再执行任何操作。我是否必须通过另一个字段替换结果或错误在哪里?

//The string with the not yet decompressed content is converted into a byte array Content was earlier defined and initialised
byte[] notDeComPresCo = new byte[content.length()];
for (int i = 0; i < content.length(); i++) {
    notDeComPresCo[i] = content.substring(i, i + 1).getBytes()[0];
}
//Until here the programm works as I want, then the problem starts:

//A new inflater is contructed to decompress the byte array into an decompressed byte array
Inflater decompressor = new Inflater();
decompressor.setInput(notDeComPresCo, 0, notDeComPresCo.length);
byte[] result = new byte[content.length()];
try {
    System.out.println("This is printed");
    int decompressedDataLength = decompressor.inflate(result);
    System.out.println("This system.out.println - command is not printed"); 
    decompressor.end();
    String x = new String(result, 0, decompressedDataLength, "UTF-8");
} catch (DataFormatException e) {
    throw new InvalidGitObjectFileException("The content could not be decompressed.");
}

控制台中没有错误,该方法在到达System.out.println("This system.out.println - command is not printed")之前结束。

【问题讨论】:

  • 您等了多长时间? InvalidGitObjectFileException 被抛出了吗?
  • 是的,对不起,我错过了……但它被抛出在调用此方法的另一个方法中!
  • Decompressor 应该解压,Inflator 应该充气......不过这不是主题

标签: java compression inflate


【解决方案1】:

我有两个猜测:

  1. int decompressedDataLength = decompressor.inflate(result); 正在抛出某种被上层吞噬的运行时异常,这就是为什么你看不到任何合理的输出
  2. notDeComPresCo 的长度为 0

我的赌注是 1。添加额外的 catch 子句,例如

catch (Exception e) {System.out.println(e.getMessage());}

并检查是否没有抛出异常。

【讨论】:

  • 嗯,notDeComPresCo 的长度是 85,而且这个 Exception 也没有被抛出……这是否意味着问题是一种运行时异常?
  • catch (Exception e) 也会赶上RuntimeException
  • 调试这段代码时发生了什么?
  • 顺便说一句,你也可以e.printStacktrace();,或者e.fillInStackTrace(System.out);
  • 解压器正在填充缓冲区数组
猜你喜欢
  • 1970-01-01
  • 2015-08-01
  • 1970-01-01
  • 2018-09-13
  • 1970-01-01
  • 1970-01-01
  • 2017-12-07
  • 2019-07-03
  • 1970-01-01
相关资源
最近更新 更多