使用动态缓冲区?爪哇
在 Java 中,我有一个方法
public int getNextFrame( byte[] buff )
可以从文件读取到缓冲区并返回读取的字节数。我正在读取 .MJPEG,它有一个 5 字节值,比如“07939”,后面是 jpeg 的那么多字节。
问题是 JPEG 字节大小可能会溢出缓冲区。我似乎无法找到一个简洁的分配解决方案。我的目标是不为每个图像创建新的缓冲区。我尝试了直接的 ByteBuffer,这样我就可以使用它的 array() 方法来直接访问底层缓冲区。 ByteBuffer
不会动态扩展。
我应该返回对参数的引用吗?例如:
public ByteBuffer getNextFrame( ByteBuffer ref )
如何找到读取的字节?谢谢。
In Java, I have a method
public int getNextFrame( byte[] buff )
that reads from a file into the buffer and returns the number of bytes read. I am reading from .MJPEG that has a 5byte value, say "07939", followed by that many bytes for the jpeg.
The problem is that the JPEG byte size could overflow the buffer. I cannot seem to find a neat solution for the allocation. My goal is to not create a new buffer for every image. I tried a direct ByteBuffer
so I could use its array()
method to get direct access to the underlying buffer. The ByteBuffer
does not expand dynamically.
Should I be returning a reference to the parameter? Like:
public ByteBuffer getNextFrame( ByteBuffer ref )
How do I find the bytes read? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
java.io.ByteArrayOutputStream
是字节数组的包装器,并根据需要对其进行放大。也许这是你可以使用的东西。编辑:
要重用,只需调用
java.io.ByteArrayOutputStream
is a wrapper around a byte-array and enlarges it as needed. Perhaps this is something you could use.Edit:
To reuse just call reset() and start over...
只需读取所需的字节数即可。不要使用
read(buffer)
,而是使用read(buffer,0,size)
。如果还有更多字节,就丢弃它们吧,反正JPG就坏了。Just read the required number of bytes. Do not use
read(buffer)
, but useread(buffer,0,size)
. If there are more bytes, just discard them, the JPG is broken anyway.编辑:
您可以使用 apache IO 的 IOBuffer,但是扩展成本非常昂贵。
您还可以使用 ByteBuffer,
position()
会告诉您读取了多少数据。如果您不知道缓冲区有多大,并且您有 64 位 JVM,则可以创建一个大的直接缓冲区。这只会在使用时分配内存(按页)。结果是您可以分配 1 GB,但如果您只需要 4 KB,则可能只使用 4 KB。直接缓冲区不支持
array()
但是,您必须使用其他方法从 ByteBuffer 中读取数据。另一种解决方案是使用 AtomicReference被调用的方法可以根据需要增加大小,但如果它足够大,它将重用以前的缓冲区。
EDIT:
You can use apache IO's IOBuffer, however this expands very expensively.
You can also use ByteBuffer, the
position()
will tell you how much data was read.If you don't know how big the buffer will be and you have a 64-bit JVM you can create a large direct buffer. This will only allocate memory (by page) when used. The upshot is that you can allocate a 1 GB but might only ever use 4 KB if that is all you need. Direct buffer doesn't support
array()
however, you would have to read from the ByteBuffer using its other methods.Another solution is to use an
AtomicReference<byte[]>
the called method can increase the size as required, but if its large enough it would reuse the previous buffer.在高级 API 中实现此目的的常用方法是让用户提供一个
OutputStream
并用您的数据填充它(可以是 ByteArrayOutputStream 或完全不同的东西),或者拥有一个>InputStream
作为返回值,用户可以读取该值来获取数据(这将从文件中动态加载正确的部分并在完成后停止)。The usual way of accomplishing this in a high-level API is either let the user provide an
OutputStream
and fill it with your data (which can be a ByteArrayOutputStream or something completely different), or have anInputStream
as return value, that the user can read to get the data (which will dynamically load the correct parts from the file and stop when finished).