sepiroth sepiroth - 5 months ago 17
Java Question

How to initialize a ByteBuffer if you don't know how many bytes to allocate beforehand?

Is this:

ByteBuffer buf = ByteBuffer.allocate(1000);


...the only way to initialize a
ByteBuffer
?

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

Edit: More details:

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.

Answer

Depends.

Library

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

  • Batik can transcode between various image formats (including TIFF).
  • Apache POI can convert between office spreadsheet formats.
  • PegDown 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.)


Known Quantities

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

Unknown Quantities

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

  • Make a guess.
  • Analyze example data sets to buffer; use the average length.

Example

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.

Optimization

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.

Comments