So may of you want to see the complete stack trace in logs. Not happy with Exception.getMessage(). So here is a way to convert the stack trace to string so now you can enjoy the printStackTrace functionality in your logs
/**
* Capture Stack Trace into a String
*/
public static String captureStackTrace(Throwable exception)
{
StringBuffer stackTrace = new StringBuffer();
stackTrace.append("Exception in thread \"");
stackTrace.append(Thread.currentThread());
stackTrace.append("\" ");
stackTrace.append(exception);
stackTrace.append("\n");
StackTraceElement elements[] = exception.getStackTrace();
for (int x = 0; x < elements.length; x++)
{
stackTrace.append(elements[x].toString());
stackTrace.append("\n");
}
return stackTrace.toString();
}
Showing posts with label java. Show all posts
Showing posts with label java. Show all posts
Monday, October 25, 2010
Thursday, October 21, 2010
Byte Array to Hex
Byte Array to Hex:
public static String byteArraytoHex(byte[] bytes)
{
StringBuffer buffer= new StringBuffer();
byte b;
for (int y = 0, x = 0; y < bytes.length; ++y, ++x)
{
b = ((byte) (bytes[y] >> 4));
b = (byte)( b<0?(16+b):b);
buffer.append(Integer.toString((b), 16).toUpperCase());
b = ((byte) (bytes[y] & 0xF));
b=(byte)( b<0?(16+b):b);
buffer.append(Integer.toString((b), 16).toUpperCase());
buffer.append(" ");
}
return buffer.toString();
}
Subscribe to:
Posts (Atom)