使用动态缓冲区?爪哇

发布于 2024-10-19 16:03:57 字数 441 浏览 8 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

洋洋洒洒 2024-10-26 16:03:57

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...

完美的未来在梦里 2024-10-26 16:03:57

只需读取所需的字节数即可。不要使用read(buffer),而是使用read(buffer,0,size)。如果还有更多字节,就丢弃它们吧,反正JPG就坏了。

Just read the required number of bytes. Do not use read(buffer), but use read(buffer,0,size). If there are more bytes, just discard them, the JPG is broken anyway.

小梨窩很甜 2024-10-26 16:03:57

编辑:

分配一个 byte[] 比从文件或文件中读取要快得多
套接字,我会很惊讶它会有很大的不同,除非你
拥有一个微秒就要花钱的系统。

读取 64 KB 的文件大约需要 10 毫秒(除非
文件在内存中)

分配 64 KB byte[] 所需的时间约为 0.001 ms,
可能更快。


您可以使用 apache IO 的 IOBuffer,但是扩展成本非常昂贵。

您还可以使用 ByteBuffer,position() 会告诉您读取了多少数据。

如果您不知道缓冲区有多大,并且您有 64 位 JVM,则可以创建一个大的直接缓冲区。这只会在使用时分配内存(按页)。结果是您可以分配 1 GB,但如果您只需要 4 KB,则可能只使用 4 KB。直接缓冲区不支持 array() 但是,您必须使用其他方法从 ByteBuffer 中读取数据。

另一种解决方案是使用 AtomicReference被调用的方法可以根据需要增加大小,但如果它足够大,它将重用以前的缓冲区。

EDIT:

Allocating a byte[] is so much faster than reading from a file or a
socket, I would be surprised it will make much difference, unless you
have a system where micro-seconds cost money.

The time it takes to read a file of 64 KB is about 10 ms (unless the
file is in memory)

The time it takes to allocate a 64 KB byte[] is about 0.001 ms,
possibly faster.


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.

顾忌 2024-10-26 16:03:57

在高级 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 an InputStream 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文