mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.Reader;
|
||||
import java.io.StringWriter;
|
||||
import java.text.SimpleDateFormat;
|
||||
|
||||
public class IOUtils {
|
||||
|
||||
public final static int DEFAULT_BUFFER_SIZE = 1024 * 4;
|
||||
|
||||
public static String read(Reader reader) {
|
||||
try {
|
||||
|
||||
StringWriter writer = new StringWriter();
|
||||
|
||||
char[] buffer = new char[DEFAULT_BUFFER_SIZE];
|
||||
int n = 0;
|
||||
while (-1 != (n = reader.read(buffer))) {
|
||||
writer.write(buffer, 0, n);
|
||||
}
|
||||
|
||||
return writer.toString();
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("read error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static String read(Reader reader, int length) {
|
||||
try {
|
||||
char[] buffer = new char[length];
|
||||
|
||||
int offset = 0;
|
||||
int rest = length;
|
||||
int len;
|
||||
while ((len = reader.read(buffer, offset, rest)) != -1) {
|
||||
rest -= len;
|
||||
offset += len;
|
||||
|
||||
if (rest == 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return new String(buffer, 0, length - rest);
|
||||
} catch (IOException ex) {
|
||||
throw new IllegalStateException("read error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static String toString(java.util.Date date) {
|
||||
if (date == null) {
|
||||
return null;
|
||||
}
|
||||
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
||||
return format.format(date);
|
||||
}
|
||||
|
||||
public static String getStackTrace(Throwable ex) {
|
||||
StringWriter buf = new StringWriter();
|
||||
ex.printStackTrace(new PrintWriter(buf));
|
||||
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
public static String toString(StackTraceElement[] stackTrace) {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
for (StackTraceElement item : stackTrace) {
|
||||
buf.append(item.toString());
|
||||
buf.append("\n");
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user