如何从 ByteBuffer 中获取连续的切片?

发布于 2024-11-18 05:28:15 字数 449 浏览 3 评论 0 原文

我有一个 ByteBuffer ,其中包含大文件(100 MB):

    java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
    writeChannel.write(byteBuffer);
    writeChannel.closeFinally();

我一次只能合法地将 1 MB 写入 writeChannel

如何对 ByteBuffer 的内容进行切片并一次仅将 1 MB 切片写入 writeChannel 中?

I have a ByteBuffer that contains a large file (100 MB):

    java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
    writeChannel.write(byteBuffer);
    writeChannel.closeFinally();

I can only legally write 1 MB to the writeChannel at a time.

How do I slice up the contents of the ByteBuffer and write only a 1 MB slice at a time into the writeChannel?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

深海里的那抹蓝 2024-11-25 05:28:15

您可以使用 ByteBuffer#slice()< /code> 获取基本 ByteBuffer 实例的重复视图,然后移动位置以公开内容的滑动窗口。或者,如果您不需要将基本缓冲区暴露给任何其他使用者,则可以对它执行相同的操作。

您可以通过 单参数 Buffer#position(int) 方法,并通过 Buffer#limit(int)< /a>.只要您注意不要将视图推到底层缓冲区的限制之外,您就可以执行以下操作:

final ByteBuffer view = base.slice();
for (int start = base.position(), end = base.limit(), stride = 1000000;
     start != end;
     start = view.limit())
  consume(view.position(start)
              .limit(start + Math.min(end - start, stride)));

我没有测试它,但它看起来是正确的。可以重写以避免位置的初始设置,这在这里并不是绝对必要的,但它会导致一些重复或第一次的更尴尬的特殊情况处理。

我这样做是为了保留基本的 for 循环 结构。

You can use ByteBuffer#slice() to get a duplicate view of your base ByteBuffer instance, then bump the position along to expose a sliding window of content. Alternately, you can just do the same to your base buffer if you don't need to expose it to any other consumers.

You can change the starting position of your view of the content via the single-argument Buffer#position(int) method, and change the end position of your view via Buffer#limit(int). So long as you're mindful not to push the view beyond the limit of the underlying buffer, you can do the following:

final ByteBuffer view = base.slice();
for (int start = base.position(), end = base.limit(), stride = 1000000;
     start != end;
     start = view.limit())
  consume(view.position(start)
              .limit(start + Math.min(end - start, stride)));

I didn't test it, but it looks correct. It's possible to rewrite to avoid the initial setting of the position, which isn't strictly necessary here, but it incurs either some repetition or more awkward special case treatment of the first time through.

I left it this way to preserve the basic for loop structure.

忆伤 2024-11-25 05:28:15

据我所知, writeChannel 上的 write() (我认为它的名称是 SocketChannel 类型)将“尝试将最多 r 个字节写入通道,其中 r 是缓冲区中剩余的字节数,即是,dst.remaining(),此时调用此方法”。 (根据 这个

ByteBuffer.remaining() 表示此方法将“返回当前位置和限制之间的元素数量”。

所以我的猜测不是你不能写整个 ByteBuffer,而是你的代码应该在 ByteBuffer 对象上调用 Flip() ,这样它就变成了:

java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
byteBuffer.flip();
writeChannel.write(byteBuffer);
writeChannel.closeFinally();

正如 Buffer.flip(): "在一系列通道读取或put 操作,调用此准备一系列通道写入或相对获取操作的方法”

As far as I know, write() on the writeChannel (which for the name I supposed is of type SocketChannel) will "attempt to write up to r bytes to the channel, where r is the number of bytes remaining in the buffer, that is, dst.remaining(), at the moment this method is invoked". (according to this)

The description of ByteBuffer.remaining() says that this method will "Return the number of elements between the current position and the limit."

So my guessing is not that you can't write the entire ByteBuffer, but that your code should call flip() on the ByteBuffer object so it become:

java.nio.ByteBuffer byteBuffer = ByteBuffer.wrap(multipartFile.getBytes());
byteBuffer.flip();
writeChannel.write(byteBuffer);
writeChannel.closeFinally();

As its said in Buffer.flip(): "After a sequence of channel-read or put operations, invoke this method to prepare for a sequence of channel-write or relative get operations"

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