📄 util.java
字号:
package jodd.util;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
/**
* General and System utilities.
*/
public final class Util {
/**
* Compare two objects just like "equals" would. Unlike Object.equals,
* this method allows any of the 2 objects to be null.
*
* @param obj1
* @param obj2
*
* @return true if equal, otherwise false
*/
public static boolean equals(Object obj1, Object obj2) {
if (obj1 == null && obj2 == null) {
return true;
} else if (obj1 != null) {
return obj1.equals(obj2);
} else {
return obj2.equals(obj1);
}
}
/**
* Create object copy using serialization mechanism.
*
* @param obj
*
* @return cloned object
* @exception Exception
*/
public static Object cloneViaSerialization(Serializable obj) throws Exception {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(bytes);
out.writeObject(obj);
out.close();
ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes.toByteArray()));
Object objCopy = in.readObject();
in.close();
return objCopy;
}
/**
* Loads external or internal class. Class may exists in current virtual
* mashine, or it may be defined externally, as part of a jar.
*
* @param jarPath path to the jar file, or null
* @param type class type
*
* @return founded class, otherwise null
* @exception ClassNotFoundException
*/
public static Class loadClass(String jarPath, String type) throws ClassNotFoundException {
Class clazz = null;
try {
if (jarPath == null) {
clazz = Class.forName(type);
} else {
jarPath = "jar:file:/" + jarPath + "!/";
URL[] url = {new URL(jarPath)};
URLClassLoader ucl = new URLClassLoader(url);
clazz = ucl.loadClass(type);
}
} catch (MalformedURLException muex) {
throw new ClassNotFoundException("bad jar path: " + jarPath, muex);
}
return clazz;
}
/**
* Reads from input and writes read data to the output, until the stream end.
*
* @param in
* @param out
* @param bufSizeHint
*
* @throws IOException
*/
public static void copyPipe(InputStream in, OutputStream out, int bufSizeHint) throws IOException {
int read = -1;
byte[] buf = new byte[bufSizeHint];
while ((read = in.read(buf, 0, bufSizeHint)) >= 0) {
out.write(buf, 0, read);
}
out.flush();
}
/**
* Returns current stack trace not without this method in the trace. Since an
* exception is thrown internally, this is a slow method.
*
* @return array of stack trace elements
*/
public static StackTraceElement[] getStackTrace() {
try {
throw new Exception();
} catch (Exception ex) {
StackTraceElement[] ste = ex.getStackTrace();
if (ste.length > 1) {
StackTraceElement[] result = new StackTraceElement[ste.length - 1];
System.arraycopy(ste, 1, result, 0, ste.length - 1);
return result;
} else {
return ste;
}
}
}
/**
* Returns exception stack trace as a String.
*
* @param ex exception
*
* @return string of the exception
*/
public static String exceptionToString(Exception ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
return sw.getBuffer().toString();
}
// ---------------------------------------------------------------- synchronization
/**
* Waits for a object for synchronization purposes.
*
* @param obj object to wait for
*/
public static void wait(Object obj) {
synchronized (obj) {
try {
obj.wait();
} catch (InterruptedException inex) {
}
}
}
/**
* Waits for a object or a timeout for synchronization purposes.
*
* @param obj object to wait for
* @param timeout the maximum time to wait in milliseconds
*/
public static void wait(Object obj, long timeout) {
synchronized (obj) {
try {
obj.wait(timeout);
} catch (InterruptedException inex) {
}
}
}
/**
* Notifies an object for synchronization purposes.
*
* @param obj object to notify
*/
public static void notify(Object obj){
synchronized (obj) {
obj.notify();
}
}
/**
* Notifies an object for synchronization purposes.
*
* @param obj object to notify
*/
public static void notifyAll(Object obj){
synchronized (obj) {
obj.notifyAll();
}
}
// ---------------------------------------------------------------- serialization
/**
* Serializes an Object to bytes array.
*
* @param obj Object to serialize
*
* @return byte array of the serialized object
* @exception IOException
*/
public static byte[] objectToByteArray(Object obj) throws IOException {
byte[] octets = null;
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
ObjectOutputStream p = new ObjectOutputStream(ostream);
p.writeObject (obj);
p.flush();
octets = ostream.toByteArray();
ostream.close();
p.close();
return octets;
}
/**
* Deserializes an object from byte array
*
* @param octets byte array of serialized object
*
* @return deserialized object
* @exception IOException
* @exception ClassNotFoundException
*/
public static Object byteArrayToObject(byte[] octets) throws IOException, ClassNotFoundException {
Object retObj = null;
ByteArrayInputStream istream = new ByteArrayInputStream(octets);
ObjectInputStream i = new ObjectInputStream(istream);
retObj = i.readObject();
i.close();
istream.close();
return retObj;
}
// ---------------------------------------------------------------- memory
/**
* Returns amount of total memory.
*
* @return total memory in bytes
*/
public long getTotalMemory() {
return Runtime.getRuntime().totalMemory();
}
/**
* Returns amount of free memory.
*
* @return free memory in bytes
*/
public long getFreeMemory() {
return Runtime.getRuntime().freeMemory();
}
/**
* Returns amount of used memory.
*
* @return used memory in bytes
*/
public long getUsedMemory() {
return Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -