java 中如何读取输入流呢?
方式一:
/***
* Has been tested ok
*
* @param in
* @return
* @throws IOException
*/
public static byte[] readBytes3(InputStream in) throws IOException {
BufferedInputStream bufin = new BufferedInputStream(in);
int buffSize = 1024;
ByteArrayOutputStream out = new ByteArrayOutputStream(buffSize);
// System.out.println("Available bytes:" + in.available());
byte[] temp = new byte[buffSize];
int size = 0;
while ((size = bufin.read(temp)) != -1) {
out.write(temp, 0, size);
}
bufin.close();
in.close();
byte[] content = out.toByteArray();
out.close();
return content;
}
方式二:
/***
* get byte[] from <code>InputStream</code> Low efficiency
*
* @param in
* @return
* @throws IOException
*/
@Deprecated
public static byte[] readBytes2(InputStream in) throws IOException {
byte[] temp = new byte[1024];
byte[] result = new byte[0];
int size = 0;
while ((size = in.read(temp)) != -1) {
byte[] readBytes = new byte[size];
System.arraycopy(temp, 0, readBytes, 0, size);
result = mergeArray(result, readBytes);
}
return result;
}
/***
* 合并字节数组
*
* @param a
* @return
*/
public static byte[] mergeArray(byte[]... a) {
// 合并完之后数组的总长度
int index = 0;
int sum = 0;
for (int i = 0; i < a.length; i++) {
sum = sum + a[i].length;
}
byte[] result = new byte[sum];
for (int i = 0; i < a.length; i++) {
int lengthOne = a[i].length;
if (lengthOne == 0) {
continue;
}
// 拷贝数组
System.arraycopy(a[i], 0, result, index, lengthOne);
index = index + lengthOne;
}
return result;
}
方式三:
/***
* 指定字符编码,无损地读取文本文件.推荐!
*
* @param in
* : 输入流,会关闭
* @param charset
* : 字符编码
* @return
* @throws IOException
*/
public static StringBuffer getFullContent3(InputStream in, String charset)
throws IOException {
StringBuffer sbuffer = new StringBuffer();
InputStreamReader inReader;
// 设置字符编码
if (ValueWidget.isNullOrEmpty(charset)) {
charset = SystemHWUtil.CURR_ENCODING;
}
inReader = new InputStreamReader(in, charset);
char[] ch = new char[1024];
int readCount = 0;
while ((readCount = inReader.read(ch)) != -1) {
sbuffer.append(ch, 0, readCount);
}
inReader.close();
in.close();
return sbuffer;
}
方式四:
/***
* 先读取出来字节数组,然后在包装成为字符串;效率不高,因为有拷贝操作(System.arraycopy)
*
* @param in
* @param charset
* @return
* @throws IOException
*/
public static String getFullContent2(InputStream in, String charset)
throws IOException {
//并不是要读取的字节的长度
int step = BUFFSIZE_1024;
BufferedInputStream bis = new BufferedInputStream(in);
// Data's byte array
byte[] receData = new byte[step];
// data length read from the stream
int readLength = 0;
// data Array offset
int offset = 0;
// Data array length
int byteLength = step;
while ((readLength = bis.read(receData, offset, byteLength - offset)) != -1) {
// Calculate the current length of the data
offset += readLength;
// Determine whether you need to copy data , when the remaining
// space is less than step / 2, copy the data
if (byteLength - offset <= step / 2) {
byte[] tempData = new byte[receData.length + step];
System.arraycopy(receData, 0, tempData, 0, offset);
receData = tempData;
byteLength = receData.length;
}
}
return new String(receData, 0, offset, charset);
}
方式五:
/***
* write inputstream into outputStream ,haven't close stream.
*
* @param ins
* @param outs
*/
public static void writeIn2Output(InputStream ins, OutputStream outs,
boolean isCloseOut, boolean isCloseInput) {
try {
int resultInt = -1;
byte[] bytes = null;
bytes = new byte[4096];
try {
while ((resultInt = ins.read(bytes)) != -1) {
outs.write(bytes, 0, resultInt);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (isCloseOut) {
try {
outs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (isCloseInput) {
try {
ins.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} finally {
}
}
方式六:
/***
* write inputstream into file according to specified length.
*
* @param file
* @param ins
* @param length2
* @throws IOException
*/
public static void writeInputStream2File(File file, InputStream ins,
long length2, boolean isShutOutputStream) throws IOException {
String parentDir = SystemHWUtil.getParentDir(file.getAbsolutePath());
File fatherFile = new File(parentDir);
if (!fatherFile.exists()) {
fatherFile.mkdirs();
}
FileOutputStream outs = new FileOutputStream(file);
int readSize;
byte[] bytes = null;
bytes = new byte[(int) length2];
long length_tmp = length2;
while ((readSize = ins.read(bytes)) != -1) {
length_tmp -= readSize;
outs.write(bytes, 0, readSize);
if (length_tmp == 0) {
break;
}
if (length_tmp < 4096) {
bytes = new byte[(int) length_tmp];
}
}
if (isShutOutputStream) {
outs.close();
}
}
方式七:
/***
* 从输入流获取字节数组
* @param br_right
* @param length2
* @return
* @throws IOException
*/
public static byte[] readBytesFromInputStream(BufferedInputStream br_right,
int length2) throws IOException {
int readSize;
byte[] bytes = null;
bytes = new byte[length2];
long length_tmp = length2;
long index =0;//start from zero
while ((readSize = br_right.read(bytes,(int)index,(int)length_tmp)) != -1) {
length_tmp -= readSize;
if (length_tmp == 0) {
break;
}
index=index+readSize;
}
return bytes;
}