ByteBuffer buf = ByteBuffer.allocate(1000);
Converting file formats tends to be a solved problem for most problem domains. For example:
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.)
When the number of bytes is truly unknown, you can do a few things:
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.