private static void bindInputStream(@NotNull PreparedStatement ps, int index, @NotNull InputStream stream) throws SQLException {
// We check whether the InputStream is actually InputStreamWithSize, for two reasons:
// 1) the database/driver can optimize the call better if it knows the size in advance
// 2) calls without size were introduced in JDBC4 and not all drivers support them
if (stream instanceof InputStreamWithSize) {
InputStreamWithSize streamWithSize = (InputStreamWithSize) stream;
long size = streamWithSize.getSize();
// The overload which takes 'long' as parameter was introduced in JDBC4 and is not
// universally supported so we'll call the 'int' overload if possible.
if (size <= Integer.MAX_VALUE)
ps.setBinaryStream(index, streamWithSize, (int) size);