惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Security Archives - TechRepublic
MongoDB | Blog
MongoDB | Blog
量子位
博客园 - 叶小钗
罗磊的独立博客
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
MyScale Blog
MyScale Blog
GbyAI
GbyAI
Help Net Security
Help Net Security
Y
Y Combinator Blog
Engineering at Meta
Engineering at Meta
Hacker News - Newest:
Hacker News - Newest: "LLM"
Latest news
Latest news
H
Hacker News: Front Page
Blog — PlanetScale
Blog — PlanetScale
雷峰网
雷峰网
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
Scott Helme
Scott Helme
S
Schneier on Security
博客园 - 司徒正美
Hugging Face - Blog
Hugging Face - Blog
S
Security @ Cisco Blogs
Recorded Future
Recorded Future
S
Securelist
博客园 - Franky
Application and Cybersecurity Blog
Application and Cybersecurity Blog
A
About on SuperTechFans
N
News and Events Feed by Topic
AI
AI
T
Tenable Blog
N
News | PayPal Newsroom
C
Cybersecurity and Infrastructure Security Agency CISA
V
V2EX - 技术
T
Threat Research - Cisco Blogs
Cisco Talos Blog
Cisco Talos Blog
L
LINUX DO - 热门话题
N
Netflix TechBlog - Medium
S
SegmentFault 最新的问题
T
The Blog of Author Tim Ferriss
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google Online Security Blog
Google Online Security Blog
S
Security Affairs
Webroot Blog
Webroot Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园 - 三生石上(FineUI控件)
C
Comments on: Blog
G
GRAHAM CLULEY

博客园 - window07

Hibernate缓存机制 Facebook 的 Scaling Out 经验 软件架构设计 mysql的FLUSH句法(清除缓存) 优化MySQL数据库性能 MySQL系统变量interactive_timeout 与 wait_timeout INSERT … ON DUPLICATE KEY UPDATE Howto change runtime variables without restart MySQL Server MySQL 5.1新特性之事件调度器(Event Scheduler) GROUP_CONCAT()妙用 MYSQL命令行导出XML格式数据 - window07 - 博客园 Mysql参数配置优化说明 MySQL 备份和恢复 error: Failed dependencies: MySQLconflicts flash的socket连接安全策略文件 缓存系统MemCached的Java客户端优化历程 使用java.util.zip对字符串进行压缩和解压缩 反序列化时出现“base-64 字符数组的无效长度”错误提示的解决 关于GC垃圾回收
Java和flash通信中数据的zlib压缩与解压缩
window07 · 2009-08-22 · via 博客园 - window07

由于as3的bytearray支持compress和uncompress。所以我们可以在Java端将数据压缩,然后在flash端读取再解压缩,这样数据在传输过程中又会小很多。
下面就介绍使用方法,基于前篇文章的范例:

服务端:
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Output;
import flex.messaging.log.Log;
import flex.messaging.messages.Message;

public class Test {

/**
* @param args
*/
public static void main(String[] args)
{
SerializationContext serializationContext=new SerializationContext();
Amf3Output amfOut = new Amf3Output(serializationContext);
    ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    DataOutputStream dataOutStream = new DataOutputStream(outStream);
    amfOut.setOutputStream(dataOutStream);
    
    HashMap<String, Object> map=new HashMap<String, Object>();
    Double[] arr=new Double[10000];
    for(int index=0;index<10000;index++)
    {
     arr[index]=Math.random();
    }
    map.put("arr", arr);
    map.put("name", "weni");
    map.put("age", "27");
    try
    {
amfOut.writeObject(map);
dataOutStream.flush();
} catch (IOException e)
{
e.printStackTrace();
}

    byte[] messageBytes = outStream.toByteArray();
    
   
    
    try 
    { 
      FileOutputStream  os; 
      OutputStreamWriter  ow; 
      BufferedWriter  out; 
      os  =  new  FileOutputStream("D://test.txt"); 
      ow  =  new  OutputStreamWriter(os); 
      out  =  new  BufferedWriter(ow); 
      os.write(messageBytes);
      os.flush();
      os.close();
      messageBytes=compressBytes(messageBytes); //将数据进行压缩
      System.out.println("OK");
}catch(Exception  e) 
{
System.out.println("error  :"  +  e);                      
}
}

private static int cachesize = 1024;
private static Inflater decompresser = new Inflater();
private static Deflater compresser = new Deflater();
public static byte[] compressBytes(byte input[])
{
compresser.reset();
compresser.setInput(input);
compresser.finish();
byte output[] = new byte[0];
ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
try
{
byte[] buf = new byte[cachesize];
int got;
while (!compresser.finished())
{
got = compresser.deflate(buf);
o.write(buf, 0, got);
}
output = o.toByteArray();
} finally
{
try
{
o.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
return output;
}
public static byte[] decompressBytes(byte input[])
{
byte output[] = new byte[0];
decompresser.reset();
decompresser.setInput(input);
ByteArrayOutputStream o = new ByteArrayOutputStream(input.length);
try
{
byte[] buf = new byte[cachesize];

int got;
while (!decompresser.finished())
{
got = decompresser.inflate(buf);
o.write(buf, 0, got);
}
output = o.toByteArray();
}catch(Exception e)
{
e.printStackTrace();
}finally
{
try
{
o.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
return output;
}
}

客户端:
public class AMF3Test extends Sprite
{
private var loader:URLLoader;
public function AMF3Test()
{
loader=new URLLoader();
loader.load(new URLRequest("D://test5.txt"));
loader.addEventListener(Event.COMPLETE,onComplete);
loader.dataFormat=URLLoaderDataFormat.BINARY;
}

private function onComplete(evt:Event):void
{
var start:Number=getTimer();
var byte:ByteArray=loader.data as ByteArray;
byte.uncompress() //将数据进行解压缩
var obj:Object=byte.readObject();
var end:Number=getTimer();
trace("耗时:"+(end-start)+"毫秒")
trace(obj.name,obj.age,obj.arr.length)
}
}