📄 streamutil.java
字号:
package com.pegasus.framework.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import com.pegasus.framework.exception.NestedRuntimeException;
public class StreamUtil {
private static final String DEFAULT_ENCODING = "UTF-8";
private static final int DEFAULT_STREAM_SIZE = 4096;
public static String readStream(InputStream inputStream) throws IOException {
return readStream(inputStream, DEFAULT_ENCODING);
}
public static String readStream(InputStream inputStream, String encoding) {
ByteArrayOutputStream outputStream = null;
String result = "";
try {
outputStream = new ByteArrayOutputStream(DEFAULT_STREAM_SIZE);
copyStream(inputStream, outputStream);
result = outputStream.toString(encoding);
outputStream.close();
} catch (Exception e) {
throw new NestedRuntimeException("readStream",e);
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
throw new NestedRuntimeException("readStream",e);
}
}
}
return result;
}
public static void copyStream(InputStream inputStream, OutputStream outputStream) throws IOException {
byte data[] = new byte[DEFAULT_STREAM_SIZE];
int len;
while ((len = inputStream.read(data)) != -1) {
outputStream.write(data, 0, len);
outputStream.flush();
}
}
public static void copyStream(Reader reader, Writer writer) throws IOException {
char data[] = new char[DEFAULT_STREAM_SIZE];
int len;
while ((len = reader.read(data)) != -1) {
writer.write(data, 0, len);
writer.flush();
}
}
public static void writeStream(byte data[], OutputStream outputStream) throws IOException {
outputStream.write(data);
outputStream.flush();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -