方式一:
/***
* 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
@deprecated
public static byte[] readbytes2(inputstream in) throws ioexception {
byte[] temp = new byte[1024];
byte[] result = new byte[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
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];
int lengthone = a[i].length;
if (lengthone == 0) {
continue;
}
// 拷贝数组
system.arraycopy(a[i], 0, result, index, lengthone);
index = index + lengthone;
方式三:
* 指定字符编码,无损地读取文本文件.推荐!
* : 输入流,会关闭
* @param charset
* : 字符编码
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();
return sbuffer;
方式四:
* 先读取出来字节数组,然后在包装成为字符串;效率不高,因为有拷贝操作(system.arraycopy)
public static string getfullcontent2(inputstream in, string charset)
//并不是要读取的字节的长度
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) {
ins.close();
} finally {
方式六:
* write inputstream into file according to specified length.
* @param file
* @param length2
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
public static byte[] readbytesfrominputstream(bufferedinputstream br_right,
int length2) throws ioexception {
bytes = new byte[length2];
long index =0;//start from zero
while ((readsize = br_right.read(bytes,(int)index,(int)length_tmp)) != -1) {
index=index+readsize;
return bytes;