如果你不知道预先分配了多少字节,如何初始化ByteBuffer? [英] How to initialize a ByteBuffer if you don't know how many bytes to allocate beforehand?

查看:254
本文介绍了如果你不知道预先分配了多少字节,如何初始化ByteBuffer?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是:

ByteBuffer buf = ByteBuffer.allocate(1000);

...初始化 ByteBuffer的唯一方法

如果我不知道需要分配多少字节怎么办?

What if I have no idea how many bytes I need to allocate..?

编辑:更多详细信息:

我正在将一种图像文件格式转换为TIFF文件。问题是起始文件格式可以是任何大小,但我需要将TIFF中的数据写入小端。所以我正在阅读我最终要打印到TIFF文件中的东西到ByteBuffer中,所以我可以将所有内容放在Little Endian中,然后我将它写入outfile。我想因为我知道IFD有多长,标题是,我可以计算每个图像平面中有多少字节,我可以在整个过程中使用多个ByteBuffers。

I'm converting one image file format to a TIFF file. The problem is the starting file format can be any size, but I need to write the data in the TIFF to little endian. So I'm reading the stuff I'm eventually going to print to the TIFF file into the ByteBuffer first so I can put everything in Little Endian, then I'm going to write it to the outfile. I guess since I know how long IFDs are, headers are, and I can probably figure out how many bytes in each image plane, I can just use multiple ByteBuffers during this whole process.

推荐答案

取决于。

转换文件格式趋势成为大多数问题域的解决问题。例如:

Converting file formats tends to be a solved problem for most problem domains. For example:


  • 蜡染可以在各种图像格式(包括TIFF)之间进行转码。

  • Apache POI 可以在办公室电子表格格式之间进行转换。

  • Flexmark 可以从Markdown生成HTML。

  • Batik can transcode between various image formats (including TIFF).
  • Apache POI can convert between office spreadsheet formats.
  • Flexmark can generate HTML from Markdown.

列表很长。第一个问题应该是,可以完成这项任务吗?如果考虑性能,那么优化现有软件包以满足您的需求可能比编写其他工具花费更多时间。 (作为奖励,其他人可以从集中工作中受益。)

The list is long. The first question should be, "What library can accomplish this task?" If performance is a consideration, your time is likely better spent optimising an existing package to meet your needs than writing yet another tool. (As a bonus, other people get to benefit from the centralised work.)


  • 读取文件?分配 file.size() bytes。

  • 复制字符串?分配 string.length() bytes。

  • 复制TCP数据包?例如,分配1500个字节。

  • Reading a file? Allocate file.size() bytes.
  • Copying a string? Allocate string.length() bytes.
  • Copying a TCP packet? Allocate 1500 bytes, for example.

当数字字节是真的未知,你可以做一些事情:

When the number of bytes is truly unknown, you can do a few things:


  • 猜猜。

  • 分析要缓冲的示例数据集;使用平均长度。

Java的 StringBuffer 使用初始缓冲区大小来保存16个字符。填充16个字符后,将分配一个新的较长的数组,然后复制原始的16个字符。如果 StringBuffer 的初始大小为1024个字符,那么重新分配不会像早期或经常那样发生。

Java's StringBuffer, unless otherwise instructed, uses an initial buffer size to hold 16 characters. Once the 16 characters are filled, a new, longer array is allocated, and then the original 16 characters copied. If the StringBuffer had an initial size of 1024 characters, then the reallocation would not happen as early or as often.

无论哪种方式,这可能是一个过早的优化。通常,当您想减少执行的内部内存重新分配的数量时,您将分配一定数量的字节。

Either way, this is probably a premature optimization. Typically you would allocate a set number of bytes when you want to reduce the number of internal memory reallocations that get executed.

这不太可能是应用程序的瓶颈。

It is unlikely that this will be the application's bottleneck.

这篇关于如果你不知道预先分配了多少字节,如何初始化ByteBuffer?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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