val inputStream: InputStream = // 输入流对象
val outputStream = ByteArrayOutputStream()
val bufferedInputStream = BufferedInputStream(inputStream)
val buffer = ByteArray(1024) // 缓冲区大小
var length: Int
while (bufferedInputStream.read(buffer).also { length = it } != -1) {
outputStream.write(buffer, 0, length)
}
val byteArray = outputStream.toByteArray()
bufferedInputStream.close()
outputStream.close()
上面是正常从InputStream读取数据指定一个1024可能不太符合某些业务情况
如何准确指定长度捏?
val buffer = ByteArray(inputStream.available())
每次写IO的时候都忘记,写个笔记防止忘记