mirror of
https://github.com/correl/mage.git
synced 2024-12-25 11:11:16 +00:00
Net traffic compressing. Enabled by default. Can be disabled by -Dnocompress on server.
This commit is contained in:
parent
05759b2966
commit
b04f843f40
2 changed files with 69 additions and 0 deletions
15
Mage.Common/src/mage/remote/traffic/ZippedObject.java
Normal file
15
Mage.Common/src/mage/remote/traffic/ZippedObject.java
Normal file
|
@ -0,0 +1,15 @@
|
||||||
|
package mage.remote.traffic;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Base interface for class wrapping non compressed objects.
|
||||||
|
* Provides methods for compressing that should be used before sending it over internet and decompressing to get actual
|
||||||
|
* data.
|
||||||
|
*
|
||||||
|
* @author ayrat
|
||||||
|
*/
|
||||||
|
public interface ZippedObject<T> {
|
||||||
|
|
||||||
|
void zip(T object);
|
||||||
|
|
||||||
|
T unzip();
|
||||||
|
}
|
54
Mage.Common/src/mage/remote/traffic/ZippedObjectImpl.java
Normal file
54
Mage.Common/src/mage/remote/traffic/ZippedObjectImpl.java
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
package mage.remote.traffic;
|
||||||
|
|
||||||
|
import java.io.ByteArrayInputStream;
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.zip.GZIPInputStream;
|
||||||
|
import java.util.zip.GZIPOutputStream;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Implementation for compressing and decompressing objects using {@link GZIPInputStream} and {@link GZIPOutputStream}.
|
||||||
|
* Can be used to send any {@link Object} over internet to reduce traffic usage.
|
||||||
|
*
|
||||||
|
* @author ayrat
|
||||||
|
*/
|
||||||
|
public class ZippedObjectImpl<T> implements ZippedObject<T>, Serializable {
|
||||||
|
|
||||||
|
private byte[] data;
|
||||||
|
|
||||||
|
public ZippedObjectImpl(T object) {
|
||||||
|
zip(object);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void zip(T object) {
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||||
|
GZIPOutputStream gz = new GZIPOutputStream(bos);
|
||||||
|
ObjectOutputStream oos = new ObjectOutputStream(gz);
|
||||||
|
oos.writeObject(object);
|
||||||
|
oos.close();
|
||||||
|
data = bos.toByteArray();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public T unzip() {
|
||||||
|
try {
|
||||||
|
ByteArrayInputStream bis = new ByteArrayInputStream(data);
|
||||||
|
GZIPInputStream gz = new GZIPInputStream(bis);
|
||||||
|
ObjectInputStream ois = new ObjectInputStream(gz);
|
||||||
|
Object o = ois.readObject();
|
||||||
|
return (T)o;
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final long serialVersionUID = 1L;
|
||||||
|
}
|
Loading…
Reference in a new issue