简体   繁体   English

Java Inflater 抛出 DataFormatException “无效的代码长度设置”

[英]Java Inflater throws a DataFormatException "invalid code length set"

I am using the inflater to decompress a SAMLRequest.我正在使用充气器来解压缩 SAMLRequest。 Since this value is compressed with GZIP i managed to pass "true" to the inflater constructor, in order to provide compatibility with such format.因为这个值是用 GZIP 压缩的,所以我设法将“true”传递给 inflater 构造函数,以提供与这种格式的兼容性。 However, the inflating line throws a DataFormatException.但是,膨胀线会引发 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;
    }

The exception is raised at line 20. The workflow I'm trying to replicate is第 20 行引发异常。我尝试复制的工作流程是

  1. Copying the value of the request ( This one for instance )复制请求的值(例如这个
  2. Use this URL decoder to decode it (the bottom-left textbox)使用此 URL 解码器对其进行解码(左下角的文本框)
  3. Paste the result of the second step in this Base64decoder + inflater in order to get the original XML, as shown in the third textbox of the last page.将第二步的结果粘贴到这个Base64decoder + inflater中,以获得原始 XML,如最后一页的第三个文本框所示。

It was a problem of the several conversions between byte array and string, which was probably causing an information loss somewhere.这是字节数组和字符串之间多次转换的问题,这可能导致某处信息丢失。 This is the working code I am using.这是我正在使用的工作代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JDK 1.7 64位Inflater抛出无效的代码长度集 - JDK 1.7 64-bit Inflater throws Invalid Code Length set 如何在充气器代码上设置 onclick 侦听器 - How to set onclick listener on inflater code 生成无效java异常的ANTLR会抛出代码 - ANTLR generating invalid java exceptions throws code Android:java.io.IOException:java.util.zip.DataFormatException:从服务器获取数据时距离太远的无效距离 - Android: java.io.IOException: java.util.zip.DataFormatException: invalid distance too far back while fetching data from server Java 使用 ASM 修改字节码抛出 ClassFormatError: Invalid length XXX in LocalVariableTable - Java bytecode modification using ASM throws ClassFormatError: Invalid length XXX in LocalVariableTable DataFormatException:java.util.zip.InflaterInputStream中的标头检查不正确 - DataFormatException: incorrect header check in java.util.zip.InflaterInputStream Java.util.zip.DataFormatException:不正确的 header 检查 - Java.util.zip.DataFormatException: incorrect header check 使用Java中的Inflater解压缩gzip压缩数据 - Decompressing gzipped data with Inflater in Java 这是 Java 的 Inflater 中的错误还是什么? - Is this a bug in Java's Inflater or what? 如何在Java JCE / JCA中设置消息身份验证代码的长度 - How to set length of message authentication code in java jce/jca
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM