mirror of
https://github.com/magefree/mage.git
synced 2025-12-25 04:52:07 -08:00
Merge branch 'master' of https://github.com/magefree/mage
This commit is contained in:
commit
03355aee37
36 changed files with 454 additions and 285 deletions
|
|
@ -50,36 +50,44 @@ public class Copier<T> {
|
|||
|
||||
public T copy(T obj) {
|
||||
T copy = null;
|
||||
|
||||
FastByteArrayOutputStream fbos = null;
|
||||
ObjectOutputStream out = null;
|
||||
ObjectInputStream in = null;
|
||||
try {
|
||||
FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
|
||||
ObjectOutputStream out= new ObjectOutputStream(fbos);
|
||||
fbos = new FastByteArrayOutputStream();
|
||||
out = new ObjectOutputStream(fbos);
|
||||
|
||||
// Write the object out to a byte array
|
||||
out.writeObject(obj);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
// Retrieve an input stream from the byte array and read
|
||||
// a copy of the object back in.
|
||||
ObjectInputStream in = new CopierObjectInputStream(loader, fbos.getInputStream());
|
||||
in = new CopierObjectInputStream(loader, fbos.getInputStream());
|
||||
copy = (T) in.readObject();
|
||||
}
|
||||
catch(IOException | ClassNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
StreamUtils.closeQuietly(fbos);
|
||||
StreamUtils.closeQuietly(out);
|
||||
StreamUtils.closeQuietly(in);
|
||||
}
|
||||
return copy;
|
||||
|
||||
}
|
||||
|
||||
public byte[] copyCompressed(T obj) {
|
||||
FastByteArrayOutputStream fbos = null;
|
||||
ObjectOutputStream out = null;
|
||||
try {
|
||||
FastByteArrayOutputStream fbos = new FastByteArrayOutputStream();
|
||||
ObjectOutputStream out= new ObjectOutputStream(new GZIPOutputStream(fbos));
|
||||
fbos = new FastByteArrayOutputStream();
|
||||
out = new ObjectOutputStream(new GZIPOutputStream(fbos));
|
||||
|
||||
// Write the object out to a byte array
|
||||
out.writeObject(obj);
|
||||
out.flush();
|
||||
out.close();
|
||||
|
||||
byte[] copy = new byte[fbos.getSize()];
|
||||
System.arraycopy(fbos.getByteArray(), 0, copy, 0, fbos.getSize());
|
||||
|
|
@ -87,6 +95,9 @@ public class Copier<T> {
|
|||
}
|
||||
catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
StreamUtils.closeQuietly(fbos);
|
||||
StreamUtils.closeQuietly(out);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
|||
30
Mage/src/main/java/mage/util/StreamUtils.java
Normal file
30
Mage/src/main/java/mage/util/StreamUtils.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
package mage.util;
|
||||
|
||||
import java.io.Closeable;
|
||||
|
||||
public final class StreamUtils {
|
||||
|
||||
/***
|
||||
* Quietly closes the closable, ignoring nulls and exceptions
|
||||
* @param c - the closable to be closed
|
||||
*/
|
||||
public static void closeQuietly(Closeable c) {
|
||||
if (c != null) {
|
||||
try {
|
||||
c.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void closeQuietly(AutoCloseable ac) {
|
||||
if (ac != null) {
|
||||
try {
|
||||
ac.close();
|
||||
}
|
||||
catch (Exception e) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue