栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

将Java Deflater / Inflater与自定义词典一起使用会导致IllegalArgumentException

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

将Java Deflater / Inflater与自定义词典一起使用会导致IllegalArgumentException

实际上,我在提出问题时就想出了这一点,但我认为无论如何我应该提出问题,以便其他人可以从我的奋斗中受益。

事实证明,您必须在设置输入之后但 设置字典 之前
调用inflate()一次。返回的值将为0,然后对needsDictionary()的调用将返回true。之后,您可以设置字典并再次调用充气。

修改后的代码如下:

import java.util.zip.Deflater;import java.util.zip.Inflater;public class DeflateWithDictionary {    public static void main(String[] args) throws Exception {        String inputString = "blahblahblahblahblah??";        byte[] input = inputString.getBytes("UTF-8");        byte[] dict = "blah".getBytes("UTF-8");        // Compress the bytes        byte[] output = new byte[100];        Deflater compresser = new Deflater();        compresser.setInput(input);        compresser.setDictionary(dict);        compresser.finish();        int compressedDataLength = compresser.deflate(output);        // Decompress the bytes        Inflater decompresser = new Inflater();        decompresser.setInput(output, 0, compressedDataLength);        byte[] result = new byte[100];        decompresser.inflate(result);        decompresser.setDictionary(dict);        int resultLength = decompresser.inflate(result);        decompresser.end();        // Depre the bytes into a String        String outputString = new String(result, 0, resultLength, "UTF-8");        System.out.println("Decompressed String: " + outputString);    }}

从API设计的角度来看,这似乎非常直觉且笨拙,因此,如果有更好的选择,请告诉我。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/595721.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号