我需要将异常记录到数据库中。数据库 API 声明我可以将值作为 ByteBuffer 或 byte[] 数组传递。那么哪个更有效率呢?
private final static byte[] getThrowableStackTraceBytes(Throwable throwable) {
StringWriter throwableStackTraceStringWriter = new StringWriter();
throwable.printStackTrace(new PrintWriter(throwableStackTraceStringWriter));
return throwableStackTraceStringWriter.toString().getBytes();
}
对比
private final static ByteBuffer getThrowableStackTraceByteBuffer(Throwable throwable) {
ByteArrayOutputStream throwableStackTraceByteArrayOutputStream = new ByteArrayOutputStream();
throwable.printStackTrace(new PrintStream(throwableStackTraceByteArrayOutputStream));
ByteBuffer throwableByteBuffer = ByteBuffer.wrap(throwableStackTraceByteArrayOutputStream.toByteArray());
return throwableByteBuffer;
}
我认为如果我使用 ByteBuffer,整体操作会更有效率,尤其是在它被传递到数据库方法之后进行处理时。我对吗?
(具体来说,我需要将异常记录到 Hypertable 中,它使用 Thrift Java API。)