Files
Druid/src/main/java/com/alibaba/druid/util/IOUtils.java
T
2013-08-14 13:49:55 +08:00

250 lines
6.9 KiB
Java

/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.lang.management.ManagementFactory;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class IOUtils {
public final static int DEFAULT_BUFFER_SIZE = 1024 * 4;
public static String read(InputStream in) {
InputStreamReader reader;
try {
reader = new InputStreamReader(in, "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new IllegalStateException(e.getMessage(), e);
}
return read(reader);
}
public static String readFromResource(String resource) throws IOException {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (in == null) {
in = IOUtils.class.getResourceAsStream(resource);
}
if (in == null) {
return null;
}
String text = IOUtils.read(in);
return text;
} finally {
JdbcUtils.close(in);
}
}
public static byte[] readByteArrayFromResource(String resource) throws IOException {
InputStream in = null;
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (in == null) {
return null;
}
return readByteArray(in);
} finally {
JdbcUtils.close(in);
}
}
public static byte[] readByteArray(InputStream input) throws IOException {
ByteArrayOutputStream output = new ByteArrayOutputStream();
copy(input, output);
return output.toByteArray();
}
public static long copy(InputStream input, OutputStream output) throws IOException {
final int EOF = -1;
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
long count = 0;
int n = 0;
while (EOF != (n = input.read(buffer))) {
output.write(buffer, 0, n);
count += n;
}
return count;
}
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();
}
public static Boolean getBoolean(Properties properties, String key) {
String property = properties.getProperty(key);
if ("true".equals(property)) {
return Boolean.TRUE;
} else if ("false".equals(property)) {
return Boolean.FALSE;
}
return null;
}
public static Integer getInteger(Properties properties, String key) {
String property = properties.getProperty(key);
if (property == null) {
return null;
}
try {
return Integer.parseInt(property);
} catch (NumberFormatException ex) {
// skip
}
return null;
}
public static Long getLong(Properties properties, String key) {
String property = properties.getProperty(key);
if (property == null) {
return null;
}
try {
return Long.parseLong(property);
} catch (NumberFormatException ex) {
// skip
}
return null;
}
public static Class<?> loadClass(String className) {
Class<?> clazz = null;
if (className == null) {
return null;
}
ClassLoader ctxClassLoader = Thread.currentThread().getContextClassLoader();
if (ctxClassLoader != null) {
try {
clazz = ctxClassLoader.loadClass(className);
} catch (ClassNotFoundException e) {
// skip
}
}
if (clazz != null) {
return clazz;
}
try {
return Class.forName(className);
} catch (ClassNotFoundException e) {
return null;
}
}
private static int pid;
public final static int getPID() {
if (pid == 0) {
String name = ManagementFactory.getRuntimeMXBean().getName();
String[] items = name.split("@");
pid = Integer.parseInt(items[0]);
}
return pid;
}
private static Date startTime;
public final static Date getStartTime() {
if (startTime == null) {
startTime = new Date(ManagementFactory.getRuntimeMXBean().getStartTime());
}
return startTime;
}
}