Java Inflater 抛出 DataFormatException "invalid code length set"

Java Inflater throws a DataFormatException "invalid code length set"

我正在使用 inflater 解压缩 SAMLRequest。由于这个值是用 GZIP 压缩的,我设法将 "true" 传递给 inflater 构造函数,以提供与这种格式的兼容性。但是,膨胀线抛出 DataFormatException。

    private String decodeMessage(String SAMLContent) {
        try {
            //URLDecode, Base64 and inflate data

            //URLDecode
            SAMLContent = URLDecoder.decode(SAMLContent, "UTF-8");

            //Base64 decoding
            byte[] decode = Base64.getDecoder().decode(SAMLContent);
            SAMLContent = new String(decode, "UTF-8");
            //SAMLContent = new String(Base64.getDecoder().decode(SAMLContent), "UTF-8");

            //Inflating data
            try {
                byte[] inflated = new byte[(10 * SAMLContent.getBytes("UTF-8").length)];
                Inflater i = new Inflater(true);
                i.setInput(SAMLContent.getBytes("UTF-8"), 0, SAMLContent.getBytes("UTF-8").length);

                //The following line throws DFException
                int finalSize = i.inflate(inflated);

                SAMLContent = new String(SAMLContent.getBytes("UTF-8"), 0, finalSize, "UTF-8");
                i.end();

            } catch (DataFormatException ex) {
                JOptionPane.showMessageDialog(null, "DFE: " + ex.getMessage());  //Returns "invalid code length set"
            }

        } catch (UnsupportedEncodingException ex) {
            JOptionPane.showMessageDialog(null, "UEE: " + ex.getMessage());
        }

        return SAMLContent;
    }

第 20 行出现异常。我要复制的工作流程是

  1. 正在复制请求的值(This one for instance)
  2. 使用this URL decoder to decode it(左下方的文本框)
  3. 在此Base64decoder + inflater粘贴第二步的结果,以得到原文XML,如最后一页第三个文本框所示。

是字节数组和字符串多次转换的问题,可能是某处信息丢失了。这是我正在使用的工作代码。

private String decodeHeader(String SAMLContent) {
    try {
        SAMLContent = URLDecoder.decode(SAMLContent, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        JOptionPane.showMessageDialog(null, "UEException: " + ex.getMessage());
    }

    byte[] deflatedBytes = Base64.getDecoder().decode(SAMLContent);
    byte[] inflatedBytes = new byte[100*deflatedBytes.length];
    Inflater compressor = new Inflater(true);

    compressor.setInput(deflatedBytes, 0, deflatedBytes.length);

    try {
        int size = compressor.inflate(inflatedBytes);
    } catch (DataFormatException ex) {
        JOptionPane.showMessageDialog(null, "DFException: " + ex.getMessage());
    }

    try {
        return new String(inflatedBytes, "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        JOptionPane.showMessageDialog(null, "UEException: " + ex.getMessage());
    }
    JOptionPane.showConfirmDialog(null, "This shouln't be printed");
    return null;
}