Android将InputStream转换为String和byte[]
16lz
2021-01-25
为什要将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; }
更多相关文章
- 为什么要学习 Markdown?究竟有什么用?
- Android下AlarmManager完整调用流程
- Android(安卓)Audio代码分析20 - queryEffects函数
- Android――带文字阴影效果的TextView
- [Android] Smali基本语法
- Rokon引擎主要类介绍
- Android(安卓)Activity返回结果startActivityForResult函数
- untiy实现手机摇一摇震动效果
- NDK开发之Android(安卓)Studio生成.h头文件的方法