用InputStream包装ByteBuffer [英] Wrapping a ByteBuffer with an InputStream

查看:444
本文介绍了用InputStream包装ByteBuffer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个采用InputStream并从中读取数据的方法.我也想将此方法与ByteBuffer一起使用.有没有包装ByteBuffer的方法,以便可以将其作为流进行访问?

I have a method that takes an InputStream and reads data from it. I would like to use this method with a ByteBuffer also. Is there a way to wrap a ByteBuffer so it can be accessed as a stream?

推荐答案

JDK中什么也没有,但是有很多实现,对于ByteBufferInputStream来说,它是google的.基本上,它们包装一个或多个ByteBuffer并跟踪其中的索引,该索引记录已读取的数量.像这样的出现了很多,但显然是越野车,请参见 @Mike Houston的改进版本答案).

Nothing in the JDK, but there are lots of implementations out there, google for ByteBufferInputStream. Basically they wrap one or more ByteBuffers and keep track of an index into them that records how much has already been read. Something like this comes up a lot, but apparently is buggy, see @Mike Houston's answer for an improved version).

这篇关于用InputStream包装ByteBuffer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆