H2BinaryDatabase: bytes get lost
While debugging an issue with binary transfer I found that the H2BinaryDatabase loses data.
What the code does:
- create 3 MB of random bytes
- create a UUID
- call
db.addBinary(UUID, byte[]) - call
db.getBinaryInfo(UUID) - print original length and length reported by
getBinaryInfo
Code:
package sila_java.examples.metadata_and_binary_transfer;
import sila_java.library.server_base.binary_transfer.BinaryInfo;
import sila_java.library.server_base.binary_transfer.database.BinaryDatabase;
import sila_java.library.server_base.binary_transfer.database.impl.H2BinaryDatabase;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.Random;
import java.util.UUID;
public class BinaryDBFail {
public static void main(String[] args) throws Exception {
try(BinaryDatabase db = new H2BinaryDatabase(UUID.randomUUID())) {
// random bytes
int numBytes = 3 * 1024 * 1024;
System.out.println("Number of bytes to create: " + numBytes);
byte[] bytes = new byte[numBytes];
new Random().nextBytes(bytes);
System.out.println("Length of byte array: " + numBytes);
// write bytes to database
UUID binaryTransferUUID = UUID.randomUUID();
try (InputStream byteStream = new ByteArrayInputStream(bytes)) {
System.out.println("Number of bytes in the input stream: " + byteStream.available());
db.addBinary(binaryTransferUUID, byteStream);
}
// get bytes from database
BinaryInfo info = db.getBinaryInfo(binaryTransferUUID);
// print number of input and output bytes
System.out.println("Number of bytes in the database: " + info.getLength());
}
}
}
Output:
Number of bytes to create: 3145728
Length of byte array: 3145728
Number of bytes in the input stream: 3145728
Number of bytes in the database: 2978439