setBytes method always allocates a new byte array without any necessity. Please see:
public void setBytes(final long startpos, final byte[] bytes,
final int offset, final int length)
{
// ensure we have space
checkWritePos(startpos, startpos + length);
final int neededCapacity = (int) (size + length);
buffer.ensureCapacity(neededCapacity);
// copy the data
System.arraycopy(bytes, offset, buffer.getArray(), (int) startpos, length);
buffer.setSize(neededCapacity);
updateSize(startpos + length);
}
final int neededCapacity = (int) (size + length);
This is incorrect. The new capacity should depend on the starting position, something like this: startpos + length
This has became a problem in by code:
final byte[] newIFDBytes = oldIFDBytes.clone();
final DataHandle<?> newStream = getBytesHandle(newIFDBytes, littleEndian);
...some writing into newStream...
where
static BytesHandle getBytesHandle(byte[] data, boolean littleEndian) {
Objects.requireNonNull(data, "Null data");
final BytesHandle result = new BytesHandle(new BytesLocation(data));
result.setLittleEndian(littleEndian);
return result;
}
I was hoping that writing to newStream would affect my newIFDBytes array, since I always write within the array's range—I just modified some fields (IFD elements). Unfortunately, the first write to newStream results in a reallocation of the internal byte array. This is bad—after that, I need to read the byte array back from newStream, although this is obviously unnecessary.
setBytes method always allocates a new byte array without any necessity. Please see:
final int neededCapacity = (int) (size + length);
This is incorrect. The new capacity should depend on the starting position, something like this: startpos + length
This has became a problem in by code:
where
I was hoping that writing to newStream would affect my newIFDBytes array, since I always write within the array's range—I just modified some fields (IFD elements). Unfortunately, the first write to newStream results in a reallocation of the internal byte array. This is bad—after that, I need to read the byte array back from newStream, although this is obviously unnecessary.