Skip to content

FILE_READ from classpath not working because of 0 byte file length #446

@ghost

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions