Just factor out your buffer size calc in bytesFromByteBuffer to the getEstimatedSize(Object value) method and use that to preallocate a buffer in the method above. For this method, the caller is expected to have already sized the byte[] correctly, but you can have a check for this in bytesFromByteBuffer to be safe.