-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
Using Version 1.4.193 I tried to apply the following script, which doesn't work:
INSERT INTO BINARY_DATA (id, bytes) VALUES (1, FILE_READ('classpath:myfile.xml'));
Whereas the following, with absolute path, works:
INSERT INTO BINARY_DATA (id, bytes) VALUES (1, FILE_READ('/tmp/myfile.xml'));
I went on to debug the code, and came to realize that the way the variable "fileLength" is calculated, is wrong for classpath resources. It returns 0 and hence the blob is created with 0 bytes.
long fileLength = FileUtils.size(fileName);
The input stream is retrieved correctly, but the 0 byte fileLength creates a valid 0 byte blob.
in class Function.java, line 1608
https://github.com/h2database/h2database/blob/version-1.4.193/h2/src/main/org/h2/expression/Function.java#L1608
case FILE_READ: {
session.getUser().checkAdmin();
String fileName = v0.getString();
boolean blob = args.length == 1;
try {
long fileLength = FileUtils.size(fileName);
InputStream in = new AutoCloseInputStream(
FileUtils.newInputStream(fileName));
if (blob) {
result = database.getLobStorage().createBlob(in, fileLength);
} else {
Reader reader;
if (v1 == ValueNull.INSTANCE) {
reader = new InputStreamReader(in);
} else {
reader = new InputStreamReader(in, v1.getString());
}
result = database.getLobStorage().createClob(reader, fileLength);
}
session.addTemporaryLob(result);
} catch (IOException e) {
throw DbException.convertIOException(e, fileName);
}
break;
}It basically boils down to the FileUtils.size using the FilePathDisk.size method which returns 0 for a "classpath:myfile" fileName:
https://github.com/h2database/h2database/blob/master/h2/src/main/org/h2/store/fs/FilePathDisk.java#L47