Android将InputStream转换为String和byte[]
Android将InputStream转换为String和byte[]
为什要将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;
- }
评论暂时关闭