压缩用Kryo提供的DeflateSerializer类,然后写入到CipherOutputStream。
Kryo kryo=new Kryo();kryo.setReferences(false);kryo.setRegistrationRequired(true);String w_str1="繁體中文,English,12345,12345,123451234512345,12345,12345,12345,12345,12345,12345,12345"; //GZipEncryptor gz=null;try {gz = new GZipEncryptor(); } catch (NoSuchAlgorithmException e) {// TODO Auto-generated catch blocke.printStackTrace();}kryo.register(String.class, new DeflateSerializer(new StringSerializer()));ByteArrayOutputStream bos=new ByteArrayOutputStream();CipherOutputStream cos=gz.newCipherOutputStream(bos);Output output = new Output(cos,1);kryo.writeClassAndObject(output, w_str1);try {output.flush();cos.close();bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();} byte[] out =bos.toByteArray();
逆向还原:
<span style="white-space:pre"> </span>Kryo kryo1=new Kryo();kryo1.setRegistrationRequired(true);kryo1.setReferences(false);kryo1.register(String.class, new DeflateSerializer(new StringSerializer()));CipherInputStream cis=gz.newCipherInputStream(new ByteArrayInputStream(out));Input input=new Input(cis); String w_str2=(String)kryo1.readClassAndObject(input);System.out.println(w_str2);