放气 - 膨胀错误。导致“错误的标题检查”错误

问题描述 投票:0回答:1

我正在通过HTTP-REDIRECT绑定机制实现SAMLSLO。使用deflate-inflate工具会给我一个带有错误标头检查的DataFormatException。

我试着把它作为独立的。虽然我没有在这里得到DataFormatException,但我发现没有返回整个消息。

    import java.io.UnsupportedEncodingException;
    import java.util.logging.Level;
    import java.util.zip.DataFormatException;
    import java.util.zip.Deflater;
    import java.util.zip.Inflater;


    public class InflateDeflate {
    public static void main(String[] args) {
    String source = "This is the SAML String";
            String outcome=null;
    byte[] bytesource = null;
    try {
        bytesource = source.getBytes("UTF-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    int byteLength = bytesource.length;
    Deflater compresser = new Deflater();
    compresser.setInput(bytesource);
    compresser.finish();

    byte[] output = new byte[byteLength];
    int compressedDataLength = compresser.deflate(output);
    outcome = new String(output);
    String trimmedoutcome = outcome.trim();
    //String trimmedoutcome = outcome;  // behaves the same way as trimmed;
            // Now try to inflate it
    Inflater decompresser = new Inflater();
    decompresser.setInput(trimmedoutcome.getBytes());
    byte[] result = new byte[4096];
    int resultLength = 0;
    try {
        resultLength = decompresser.inflate(result);
    } catch (DataFormatException e) {
        e.printStackTrace();
    }
    decompresser.end();
    System.out.println("result length ["+resultLength+"]");
    String outputString = null;
    outputString = new String(result, 0, resultLength);
    String returndoc = outputString;
    System.out.println(returndoc);
    }

    }

令人惊讶的是,我得到的结果是[22]字节,原始的是[23]字节,并且在膨胀后缺少'g'。

我在这里做了一些根本错误的事吗?

zip inflate deflate
1个回答
0
投票

Java的String是一个CharacterSequence(一个字符是2个字节)。使用新的String(byte [])可能无法正确地将byte []转换为String表示形式。至少你应该指定一个编码新String(byte [],“UTF-8”)的字符来防止无效的字符转换。

以下是压缩和解压缩的示例:

import java.util.zip.Deflater;
import java.util.zip.InflaterInputStream;
...

byte[] sourceData; // bytes to compress (reuse byte[] for compressed data)
String filename; // where to write
{
    // compress the data
    Deflater deflater = new Deflater(Deflater.DEFAULT_COMPRESSION);
    deflater.setInput(sourceData);
    deflater.finish();
    int compressedSize = deflater.deflate(data, 0, sourceData.length, Deflater.FULL_FLUSH);

    // write the data   
    OutputStream stream = new FileOutputStream(filename);
    stream.write(data, 0, compressedSize);
    stream.close();
}

{
    byte[] uncompressedData = new byte[1024]; // where to store the data
    // read the data
    InputStream stream = new InflaterInputStream(new FileInputStream(filename)); 
    // read data - note: may not read fully (or evenly), read from stream until len==0
    int len, offset = 0;
    while ((len = stream.read(uncompressedData , offset, uncompressedData .length-offset))>0) {
        offset += len;
    }           
    stream.close();
}
© www.soinside.com 2019 - 2024. All rights reserved.