您的位置:首页 > 移动开发 > Android开发

Android将InputStream转换为String和byte[]

2011-07-07 09:31 513 查看
为什要将InputStream转换为String?因为要实现加密功能,加密函数的输入都是String。
public static String inputStream2String (InputStream in) throws IOException {

StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
int n;
while ((n = in.read(b))!= -1){
out.append(new String(b,0,n));
}
Log.i("String的长度",new Integer(out.length()).toString());
return out.toString();
} 通过各种getInputStream,就可以将HttpUrlconnection、输入文本流等等转换为String,当然还可以转化为byte[]
public static byte[] InputStreamToByte(InputStream is) throws IOException {
ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
int ch;
while ((ch = is.read()) != -1) {
bytestream.write(ch);
}
byte imgdata[] = bytestream.toByteArray();
bytestream.close();
return imgdata;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息