Java Inflater создает исключение DataFormatException «недопустимая длина кода» - PullRequest
0 голосов
/ 26 марта 2020

Я использую инфлятор для распаковки запроса SAML. Поскольку это значение сжато с помощью 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. Копирование значения запроса ( Это, например, )
  2. Используйте этот декодер URL для его декодирования (нижнее левое текстовое поле)
  3. Вставьте результат второго шаг в этом Base64decoder + inflater , чтобы получить оригинал XML, как показано в третьем текстовом поле последней страницы.

1 Ответ

0 голосов
/ 31 марта 2020

Это была проблема нескольких преобразований между байтовым массивом и строкой, которая, вероятно, вызывала потерю информации где-то. Это рабочий код, который я использую.

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