How to compress non-file-based data (Copy)

zhaozj2021-02-16  45

December 7, 2002

Q: This week, I answered two questions that use Java for data compression.

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

The second question is: I read Todd Sundsted "Compressed your data" with great enthusiasm, thus improving your web application performance, but I am a bit disappointing after reading. When I read the title of the article I am very happy. I think I finally found the solution to solve the problem.

In our company, we try to improve the performance of the RMI application of an organization data. The server is mostly processed and optimized. We spent a year and a half to improve performance, but now it seems that bottleneck is data On the transfer. In any time of the day, we are likely to transfer thousands of data between customers and servers.

One possible solution, I suggest that we can compress this data before returning data to the client, which is already very clear in Todd's article. However, the example in the article is a compressed file, not What we need - is compressed.

In the implementation in RMI, we first get data from the database, then put the data into a list, then return this list to the client, and then insert them into JTABLE. I want to return the data to the customer, First compress the data in the list, then decompress the client, and finally insert the data into the table.

Is this idea possible?

A: I recently received some questions about Todd's article. Many readers look very confused to the examples in the article. Because the examples in the article are compressed as cores.

First answer the first question, when you use zipinputstream and zipoutputStream do not force you to use files. The only thing to note is that you must convert the data into the form of byte arrays.

The second problem is more difficult. In the network, communication is required to communicate in the RMI mode. In order to make RMI data compression before transmitting data, you must create a new socket that compresses data. Then, When you have created a socket, you have to tell RMI to use this socket.

The following is a brief steps in creating a socket in the form of a RMI:

1: Select or create a new socket. (You can see "Create a typical socket").

2: Create a server-side socket.

3: Create a RMICLIENTSOCKETFACTORY

4: Create a RMISERVERSOCKETFACTORY

5: Create a remote object that inherits UnicastRemoteObject to use new factories.

According to this rough idea, we will see how each step is realized.

Step 1: Create zipsocket

We recreate such a socket because of ZIP compression.

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 outputReam 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 oException {

Super (port);

}

Public socket acception () throws oException {

Socket Socket = new zipsocket ();

IMPLACCEPT (SOCKET);

Return Socket;

}

}

Step 3: Create ZipClientSocketFactory

The creation of the client's Factory must follow the following form:

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 UnicastRemoteObject to use new factories.

Public class yourrmiObject extends unicastremoteObject {

Public YourRemoteObject (int port) {

Super (port, new zipclientsocketfactory (), new zipserversocketFactory ());

}

// The remaining is your own procedure implementation

}

Now your communication data is compressed.

About the Author:

Tony Sintes is a separate consultant, and the founder of First Class Consulting, Inc. is mainly committed to tailoring and training for different enterprise systems. Amateur time, Tony is a positive free writer At the same time, it is also the << 21 day learning-oriented object-oriented programming >> Author (SAMS, 2001; ISBN: 0672321092). Resources:

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 Translator: I think this article is very practical, and tells us a feasible solution to data compression when network communication. Measures. So translating it, I hope everyone can learn this method. This article is translated and simpler. But because of the limited level, if you are not right, please refer to you. Bootcool@263.net.

转载请注明原文地址:https://www.9cbs.com/read-27658.html

New Post(0)