Tag: InputStream

错误“不能为null”在Kotlin中

有一个.zip文件中有多个文件,我正在尝试获取。 试图解压缩文件提供了一个java.lang.IllegalStateException:zis.nextEntry不能为null。 如何正确地做到这一点? @Throws(IOException::class) fun unzip(zipFile: File, targetDirectory: File) { val zis = ZipInputStream( BufferedInputStream(FileInputStream(zipFile))) try { var ze: ZipEntry var count: Int val buffer = ByteArray(8192) ze = zis.nextEntry while (ze != null) { val file = File(targetDirectory, ze.name) val dir = if (ze.isDirectory) file else file.parentFile if (!dir.isDirectory && !dir.mkdirs()) throw FileNotFoundException(“Failed to […]

如何写while循环与在kotlin中的responseInputStream.read – (while((i = responseInputStream.read(byteContainer))

如何使用while循环与kotlin android中的responseInputStream.read 这里添加了responseInputStream read while循环.kt val responseInputStream = conn.inputStream val responseStringBuffer = StringBuffer() val byteContainer = ByteArray(1024) var i: Int while ((i = responseInputStream.read(byteContainer)) != -1) { responseStringBuffer.append(String(byteContainer, 0, i)) } Log.w(“TAG”, “res :” + responseStringBuffer.toString())

Android Kotlin打开资产文件

我想打开资产文件。 在java代码工作之前,但当我更改代码kotlin它不起作用。 Java代码的工作 InputStream streamIN = new BufferedInputStream(context.getAssets().open(Database.ASSET)); OutputStream streamOU = new BufferedOutputStream(new FileOutputStream(LOCATION)); byte[] buffer = new byte[1024]; int length; while ((length = streamIN.read(buffer)) > 0) { streamOU.write(buffer, 0, length); } streamIN.close(); streamOU.flush(); streamOU.close(); 我将代码更改为Kotlin,但它不起作用 var length: Int val buffer = ByteArray(1024) BufferedOutputStream(FileOutputStream(LOCATION)).use { out -> { BufferedInputStream(context.assets.open(Database.ASSET)).use { length = it.read(buffer) if (length […]

逐渐填充InputStream并用SpringMVC返回

考虑我在数据库中有大量的csv字符串,我需要通过他的请求传递给客户端。 一个天真的方式来做到这一点: @RequestMapping(value = "/{user}/ejournal", method = RequestMethod.GET, produces = "text/csv") public ResponseEntity<ByteArrayResource> getEJournal(@PathVariable Long userId) { final byte[] documentBody = EJournal .findByUser(userId) .stream() .collect(Collectors.joining("\n")) .getBytes(); return new ResponseEntity( new ByteArrayResource(documentBody), HttpStatus.OK); } 从客户端直接(kotlin代码示例): val inputStream = eJournalService.getJournal(userId) inputStream.reader().forEachLine { line -> … } 但是我不得不把所有的数据加载到内存中,然后传递给流,显然效率不高。 所以我需要以某种方式缓冲它使用自定义阅读器分页读取数据和发送缓冲区到客户端。 我想实现我自己的InputStream但InputStream#read()返回int而不是String ,这有点复杂。 任何最佳实践来做到这一点? 我试图搜索,但只有例子传递一个图片使用已经在内存中的流,而不是缓冲分页后批次发送顺序数据库查询。 提前致谢。

错误“不能为null”在Kotlin中

有一个.zip文件中有多个文件,我正在尝试获取。 试图解压缩文件提供了一个java.lang.IllegalStateException:zis.nextEntry不能为null。 如何正确地做到这一点? @Throws(IOException::class) fun unzip(zipFile: File, targetDirectory: File) { val zis = ZipInputStream( BufferedInputStream(FileInputStream(zipFile))) try { var ze: ZipEntry var count: Int val buffer = ByteArray(8192) ze = zis.nextEntry while (ze != null) { val file = File(targetDirectory, ze.name) val dir = if (ze.isDirectory) file else file.parentFile if (!dir.isDirectory && !dir.mkdirs()) throw FileNotFoundException("Failed to […]

如何写while循环与在kotlin中的responseInputStream.read – (while((i = responseInputStream.read(byteContainer))

如何使用while循环与kotlin android中的responseInputStream.read 这里添加了responseInputStream read while循环.kt val responseInputStream = conn.inputStream val responseStringBuffer = StringBuffer() val byteContainer = ByteArray(1024) var i: Int while ((i = responseInputStream.read(byteContainer)) != -1) { responseStringBuffer.append(String(byteContainer, 0, i)) } Log.w("TAG", "res :" + responseStringBuffer.toString())

在Kotlin中,如何将InputStream的全部内容读入字符串?

我最近看到了将InputStream全部内容读入Kotlin中的字符串的代码,比如: // input is of type InputStream val baos = ByteArrayOutputStream() input.use { it.copyTo(baos) } val inputAsString = baos.toString() 并且: val reader = BufferedReader(InputStreamReader(input)) try { val results = StringBuilder() while (true) { val line = reader.readLine() if (line == null) break results.append(line) } val inputAsString = results.toString() } finally { reader.close() } 而且,即使这样看起来更平滑,因为它会自动关闭InputStream : […]