12月7日, 2002 
 Q: 本周,我回答了两个使用Java进行数据压缩的问题. 
第一个问题是: 我怎样才能压缩那些不在文件中的数据. 
第二个问题是: 我以极大的热情阅读了Todd Sundsted的"压缩你的数据,从而提高你的网络应用程序的性能",但是读完后我却有点失望.当我读到文章标题时我很高兴.我想我总算找到了解决问题的办法了. 
    在我们的公司,我们试图提高一个组织数据的RMI应用程序的性能.服务器端进行了绝大部分的处理和优化.我们花了一年半的时间去提高性能,但是现在看来瓶颈在于数据的传输上.在一天的任何时间内,我们都有可能在客户和服务器之间传送成千上万的数据. 
      一种可能的解决办法,我建议我们能够在把数据返回给客户端时先压缩这些数据,这在Todd的文章中已经说得很清楚了.但是,文章中的例子却是压缩文件,而不是我们所需要的----对数据进行压缩. 
    在RMI中的实现中,我们先从数据库取得数据,再把数据放入一个列表中,接着把这个列表返回给客户端,最后再把它们插入JTable中.我想在把数据返回给客户时,首先把列表中的数据压缩,然后在客户端解压缩,最后把数据插入到表格中. 
这样的想法可行吗? 
A:最近我收到了一些关于Todd的文章的疑问.很多读者看起来对文章中的举例很疑惑.因为文章中的例子是以文件压缩为核心的. 
   首先回答第一个问题,当你使用ZipInputStream 和 ZipOutputStream 并没有强制你必须使用文件.唯一要注意的是你必须把数据转换为字节数组的形式. 
  第二个问题比较棘手.在网络中,以RMI方式通信就需要作一些调整了.为了在传送数据之前就让RMI进行数据压缩,你必须创建一个能够压缩数据的新的套接字.然后,当你创建了一个套接字后,你得告诉RMI使用这一套接字. 
  以下是创建一个RMI形式的套接字的简要步骤: 
  1:选择或者创建一个新的套接字.(可以参看SUN'S的"创建一个典型的套接字"). 
  2:创建一个服务器端的套接字. 
  3:创建一个RMIClientSocketFactory 
  4:创建一个RMIServerSocketFactory 
  5:创建一个继承了UnicastRemoteObjec的远程对象,从而使用新的factories. 
  根据这一大致的想法,我们来看每一步如何具体的实现. 
 步骤1: 创建ZipSocket  
由于要进行Zip压缩,我们重新创建这样的套接字 
mport java.io.InputStream; 
import java.io.OutputStream; 
import java.util.zip.ZipInputStream; 
import java.util.zip.ZipOutputStream; 
import java.net.Socket; 
public class ZipSocket extends Socket { 
  
    private InputStream in; 
    private OutputStream out; 
     
    public ZipSocket() { super(); } 
  
    public ZipSocket(String host, int port)  
        throws IOException { 
            super(host, port); 
    } 
     
    public InputStream getInputStream()  
        throws IOException { 
            if (in == null) { 
                in = new ZipInputStream(super.getInputStream()); 
            } 
       return in; 
    } 
  
    public OutputStream getOutputStream()  
        throws IOException { 
            if (out == null) { 
                out = new ZipOutputStream(super.getOutputStream()); 
            } 
            return out; 
    } 
     
    public synchronized void close() throws IOException { 
        OutputStream o = getOutputStream(); 
        o.flush(); 
        super.close(); 
    } 
} 
  
  
步骤2: 创建ZipServerSocket  
  
import java.net.ServerSocket; 
import java.net.Socket; 
import java.io.IOException; 
  
public class ZipServerSocket extends ServerSocket 
{ 
  public ZipServerSocket(int port) throws IOException {     
    super(port); 
  } 
   
  public Socket accept() throws IOException {  
    Socket socket = new ZipSocket(); 
    implAccept(socket); 
    return socket; 
  } 
} 
  
  
步骤3:创建ZipClientSocketFactory  
客户端的factory的创建必须遵循以下的形式:  
  
import java.io.IOException;  
import java.io.Serializable;  
import java.net.Socket;  
import java.rmi.server.RMIClientSocketFactory; 
  
public class ZipClientSocketFactory  
    implements RMIClientSocketFactory, Serializable {  
    public Socket createSocket(String host, int port)  
        throws IOException {  
            ZipSocket socket = new ZipSocket(host, port);  
            return socket;  
    }  
} 
  
步骤4:创建ZipServerSocketFactory  
  
import java.io.IOException;  
import java.io.Serializable;  
import java.net.ServerSocket;  
import java.rmi.server.RMIServerSocketFactory;  
   
public class ZipServerSocketFactory  
    implements RMIServerSocketFactory, Serializable {  
  
    public ServerSocket createServerSocket(int port)  
        throws IOException {  
            ZipServerSocket server = new ZipServerSocket(port);  
            return server;  
    }  
} 
  
步骤5: 创建一个继承了UnicastRemoteObjec的远程对象,从而使用新的factories.  
public class YourRMIObject extends UnicastRemoteObject { 
  
public YourRemoteObject( int port ) { 
super( port, new ZipClientSocketFactory(), new ZipServerSocketFactory() ); 
} 
  
// 剩下的是你自己的程序实现 
  
} 
  
现在你的通信数据得到了压缩. 
  
  
关于作者: 
Tony Sintes 是一个独立咨询人,同时也是First Class Consulting, Inc. 的创始人.这一咨询公司主要致力与对各个不同的企业系统进行量身定制和培训 . 业余时间,Tony 是一个积极的自由作家,同时也是Sams出版的<<21天学通面向对象编程>>的作者 (Sams, 2001; ISBN: 0672321092).  
  
资源: 
To download the source code that accompanies this article, go to:  
http://www.javaworld.com/javaworld/javaqa/2001-12/ziprmi/01-qa-1207-ziprmi.zip  
  
"Zip Your Data and Improve the Performance of Your Network-Based Applications," Todd Sundsted (JavaWorld, November 1998):  
http://www.javaworld.com/javaworld/jw-11-1998/jw-11-howto.html  
  
"Creating a Custom RMI Socket Factory," (Sun Microsystems, 1999):  
http://java.sun.com/products/jdk/1.2/docs/guide/rmi/rmisocketfactory.doc.html  
  
"Creating a Custom Socket Type," (Sun Microsystems, 1999):  
http://java.sun.com/products/jdk/1.2/docs/guide/rmi/sockettype.doc.html  
  
Be sure to check out Tony's suggested reading list at:  
http://www.firstclassconsulting.net/reading.html  
  
For more RMI stories, visit the RMI / RMI-IIOP section of JavaWorld's Topical Index:  
http://www.javaworld.com/channel_content/jw-rmi-index.shtml  
  
Want more? See the Java Q&A Index for the full Q&A catalog:  
http://www.javaworld.com/columns/jw-qna-index.shtml  
  
For over 100 insightful Java tips from some of the best minds in the business, visit JavaWorld's Java Tips Index:  
http://www.javaworld.com/columns/jw-tips-index.shtml  
  
Learn the basics of client-side Java in our Java Beginner discussion. Core topics include the Java language, the Java Virtual Machine, APIs, and development tools:  
http://forums.idg.net/webx?50@@.ee6b804  
  
Sign up for JavaWorld's free Applied Java newsletter:  
http://www.idg.net/jw-subscribe  
  
You'll find a wealth of IT-related articles from our sister publications at IDG.net  
译者:我觉得这篇文章很有实用性,并且告诉了我们一种在网络通信时进行数据压缩的一个可行的解决办法.所以把它翻译出来,希望大家都能学到这一方法.这篇文章翻译起来比较简单.但是由于水平有限,如果不对的地方恳请大家指正. [email protected].  
 
  |