Use Java to compress data transmitted over the network

Source: Internet
Author: User

 

Compressed Data
How to compress data that is not file-based

Q: I have answered two questions about using Java for data compression.

The first question is: how can I compress data that is not in the file.

The second problem is: I read "compressing your data to improve the performance of your network applications" with great enthusiasm, but I was a little disappointed after reading it. I am very happy when I read the article title. I thought I finally found a solution to the problem.

In our company, we try to improve the performance of RMI applications that organize data. the server performs most of the processing and optimization. we spent a year and a half to improve performance, but now it seems that the bottleneck lies in data transmission. at any time of the day, we may transmit thousands of data between the customer and the server.

One possible solution is to compress the data before returning it to the client. This is already clear in the document of Todd. however, the example in this article is to compress the file, not what we need-to compress the data.

In RMI implementation, we first obtain data from the database, then put the data into a list, then return the list to the client, and finally insert them into the JTable. when I want to return the data to the customer, first compress the data in the list, decompress the data on the client, and insert the data into the table.

Is this idea feasible?

A: Recently I have received some questions about Todd. Many readers seem confused about the examples in this article because the examples in this article are based on File compression.

First, answer the first question. When ZipInputStream and ZipOutputStream are not mandatory, you must use files. The only thing you must note is that you must convert the data into byte arrays.

The second problem is tricky. in the network, some adjustments are required for RMI communication. to Compress RMI before transmitting data, you must create a new socket that can compress the data. then, after you create a socket, you have to tell RMI to use this socket.

The procedure for creating a socket in the RMI format is as follows:

1: select or create a new socket. (See "Create a typical socket" in SUNS ").

2: Create a socket on the server.

3: Create an RMIClientSocketFactory

4: Create an RMIServerSocketFactory

5: Create a remote object that inherits UnicastRemoteObjec and use the new factories.

Based on this general idea, we can see how each step is implemented.

Step 1: Create ZipSocket

To perform Zip compression, we re-create such a socket.

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

}

}





Step 2: Create 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;

}

}





Step 3: Create ZipClientSocketFactory

The client's factory must be created in the following format:



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;

}

}



Step 4: Create 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;

}

}



Step 5: Create a remote object that inherits UnicastRemoteObjec and use the new factories.

Public class YourRMIObject extends UnicastRemoteObject {



Public YourRemoteObject (int port ){

Super (port, new ZipClientSocketFactory (), new ZipServerSocketFactory ());

}



// The rest is your own program implementation

}



Now your communication data is compressed.





About Author:

Tony Sintes is an independent consultant and First Class Consulting, Inc. founder. this consulting company is dedicated to providing customized and training for different enterprise systems. in his spare time, Tony is an active freelance writer and author of <21-day Object-Oriented Programming> published by Sams (Sams, 2001; ISBN: 0672321092 ).



Resource:

To download the source code that accompanies this article, go:

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 Microsystem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.