📄 compressionhandler.java
字号:
/**
* $Log: CompressionHandler.java,v $
* Revision 1.6 2003/02/04 22:14:05 willaxt
* sendObject() must also take serialized Objects, not only externalized!
*
* Revision 1.5 2003/02/03 13:02:37 mwulff
* removed method sendObject(Serializable)
*
* Revision 1.4 2003/02/02 17:52:02 mwulff
* the Out/InputStream in sendObject and receiveObject, is now closed in a finally
* clause
*
* Revision 1.3 2003/02/01 17:39:14 willaxt
* now externalizable
*
* Revision 1.2 2003/01/18 21:55:31 mwulff
* changed debug() method name to log()
*
* Revision 1.1 2003/01/16 13:12:11 mwulff
* initial version
*
* Revision 1.4 2002/12/20 18:51:57 mwulff
* no message
*
*/
package de.fhm.jkf.comm.clsv;
import java.io.ByteArrayOutputStream;
import java.io.Externalizable;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.zip.DataFormatException;
import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater;
import java.util.zip.InflaterInputStream;
/**
* @author marten wulff
* @version 1.0
*
* * <br><br><center><table border="1" width="80%"><hr>
* <strong><a href="http://jkf.sourceforge.net">The JKF Project</a></strong>
* <p>
* Copyright (C) 2002 by Marten Wulff
* <p>
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* <p>
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* <p>
* You should have received a copy of the <a href="http://www.gnu.org/copyleft/lesser.html">
* GNU Lesser General Public License</a> along with this library; if not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
* <hr></table></center>
*
*
* <p>
* <code>CompressionHandler</code> is used to send end receive compressed or
* uncompressed data on client side. The underlaying protocol that is
* used to transfer is wrapped in a <code>CompressionStreamWrapper</code> instance.
* For sending compressed data is used a <code>DeflaterOutputStream</code> and for
* receiving an <code>InflaterInputStream<code>.
* </p>
*
* @see de.fhm.jkf.comm.cl.ClientCompressionHandler
* @see de.fhm.jkf.comm.sv.ServerCompressionHandler
*/
public abstract class CompressionHandler {
// flag if running in compressed mode or not
protected boolean modeCompressed = false;
// the compress limit for the ClientCompressionHandler
protected int compressLimit = -1;
// the inital buffer size if compression is needed
protected int communicationBufferSize = -1;
// stream wrapper
protected CompressionStreamWrapper streamWrapper = null;
/**
* Constructor for CompressionHandler.
*
* @param compressLimit the amount of bytes which can
* be send uncompressed
* @param communicationBufferSize the initial buffer size if compression
* or decompression is used
*/
public CompressionHandler(int compressLimit, int communicationBufferSize) {
super();
this.compressLimit = compressLimit;
this.communicationBufferSize = communicationBufferSize;
}
/**
* Method for receiving the data send from the server.
*
* @return Object the received object
*
* @throws IOException if an I/O-Error occured while receiving data
* @throws ClassNotFoundException if no class information is available
* for the received object
* @throws DataFormatException if the stream is corrupted
*/
public Object receiveObject()
throws IOException, ClassNotFoundException, DataFormatException {
Object response = null;
InputStream in = null;
ObjectInputStream obIn = null;
try {
in = streamWrapper.getInputStream();
log("entering receiveObject()");
// check if we need to decode the response data
modeCompressed = streamWrapper.isModeCompressed();
if (modeCompressed) {
log("- mode is compressed");
obIn =
new ObjectInputStream(
new InflaterInputStream(in, new Inflater(true)));
} else {
log("- mode is uncompressed");
obIn = new ObjectInputStream(in);
}
response = obIn.readObject();
log("leaving receiveObject()");
} finally {
obIn.close();
}
return response;
}
/**
* Method for sending a serialized object.
* If the total data size of the requestObject exceeds
* the compressLimit, data will be send per DeflaterOutputStream.
*
* @param requestObject the object that should be send
*/
public void sendObject(Serializable requestObject) throws IOException {
ByteArrayOutputStream buffer = null;
ObjectOutputStream obOut = null;
Deflater def = null;
OutputStream out = null;
try {
log("entering sendObject()");
buffer = new ByteArrayOutputStream();
obOut = new ObjectOutputStream(buffer);
obOut.writeObject(requestObject);
obOut.flush();
obOut.close();
if (buffer.size() > compressLimit) {
log("sending in mode compressed");
this.streamWrapper.setModeCompressed(true);
def = new Deflater(Deflater.BEST_SPEED, true);
out =
new DeflaterOutputStream(
this.streamWrapper.getOutputStream(),
def,
communicationBufferSize);
} else {
log("sending in mode uncompressed");
this.streamWrapper.setModeCompressed(false);
out = this.streamWrapper.getOutputStream();
}
buffer.writeTo(out);
buffer.flush();
buffer.close();
out.flush();
// log some statistics
if (def != null) {
log(
"data ["
+ def.getTotalIn()
+ "] compressed data ["
+ def.getTotalOut()
+ "]");
} else {
log("data [" + buffer.size() + "]");
}
} finally {
out.close();
}
}
/**
* Method for sending a externalized object. If the total data size of the
* requestObject exceeds the compressLimit, data will be send per
* DeflaterOutputStream.
*
* @param requestObject the object that should be send
*/
public void sendObject(Externalizable requestObject) throws IOException {
ByteArrayOutputStream buffer = null;
ObjectOutputStream obOut = null;
Deflater def = null;
OutputStream out = null;
try {
log("entering sendObject()");
buffer = new ByteArrayOutputStream();
obOut = new ObjectOutputStream(buffer);
obOut.writeObject(requestObject);
obOut.flush();
obOut.close();
if (buffer.size() > compressLimit) {
log("sending in mode compressed");
this.streamWrapper.setModeCompressed(true);
def = new Deflater(Deflater.BEST_SPEED, true);
out =
new DeflaterOutputStream(
this.streamWrapper.getOutputStream(),
def,
communicationBufferSize);
} else {
log("sending in mode uncompressed");
this.streamWrapper.setModeCompressed(false);
out = this.streamWrapper.getOutputStream();
}
buffer.writeTo(out);
buffer.flush();
buffer.close();
out.flush();
// log some statistics
if (def != null) {
log(
"data ["
+ def.getTotalIn()
+ "] compressed data ["
+ def.getTotalOut()
+ "]");
} else {
log("data [" + buffer.size() + "]");
}
} finally {
out.close();
}
}
/**
* Sets the streamWrapper.
*
* @param streamWrapper the stream wrapper to set
*/
public void setStreamWrapper(CompressionStreamWrapper streamWrapper) {
this.streamWrapper = streamWrapper;
}
/**
* Method for logging. Must be implemented in sub classes, because
* client classes uses different logging mechanism than server
* classes.
*
* @see de.fhm.jkf.comm.cl.ClientCompressionHandler
* @see de.fhm.jkf.comm.sv.ServerCompressionHandler
*/
protected abstract void log(Object message);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -