[英]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 行引发异常。我尝试复制的工作流程是
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.