放气 - 膨胀错误.导致“不正确的头检查"错误 [英] Deflate - Inflate errors. Causing "incorrect header check" errors

查看:33
本文介绍了放气 - 膨胀错误.导致“不正确的头检查"错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正致力于通过 HTTP-REDIRECT 绑定机制实现 SAMLSLO.使用 deflate-inflate 工具会给我一个 DataFormatException 和不正确的标头检查.

I am working on implementing a SAMLSLO through HTTP-REDIRECT binding mechanism. Using deflate-inflate tools gives me a DataFormatException with incorrect header check.

我将其作为独立尝试.虽然我在这里没有得到 DataFormatException,但我观察到整个消息没有被返回.

I tried this as a stand-alone. Though I did not get DataFormatException here I observed the whole message is not being returned.

    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".

Surprisingly I get the result as [22] bytes, the original is [23] bytes and the 'g' is missing after inflating.

我在这里做错了什么吗?

Am I doing something fundamentally wrong here?

推荐答案

Java 的 String 是一个 CharacterSequence(一个字符是 2 个字节).使用 new String(byte[]) 可能无法正确地将您的 byte[] 转换为字符串表示.至少你应该指定一个字符编码 new String(byte[], "UTF-8") 以防止无效的字符转换.

Java's String is a CharacterSequence (a character is 2 bytes). Using new String(byte[]) may not correctly convert your byte[] to a String representation. At least you should specify a character encoding new String(byte[], "UTF-8") to prevent invalid character conversions.

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

Here's an example of compressing and decompressing:

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

这篇关于放气 - 膨胀错误.导致“不正确的头检查"错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆