diff --git a/src/jni/duckdb_java.cpp b/src/jni/duckdb_java.cpp index bd445885..8e6a620a 100644 --- a/src/jni/duckdb_java.cpp +++ b/src/jni/duckdb_java.cpp @@ -994,15 +994,20 @@ jobject ProcessVector(JNIEnv *env, Connection *conn_ref, Vector &vec, idx_t row_ break; } case LogicalTypeId::BLOB: - varlen_data = env->NewObjectArray(row_count, J_ByteBuffer, nullptr); + varlen_data = env->NewObjectArray(row_count, J_ByteArray, nullptr); for (idx_t row_idx = 0; row_idx < row_count; row_idx++) { if (FlatVector::IsNull(vec, row_idx)) { continue; } auto &d_str = ((string_t *)FlatVector::GetData(vec))[row_idx]; - auto j_obj = env->NewDirectByteBuffer((void *)d_str.GetData(), d_str.GetSize()); - env->SetObjectArrayElement(varlen_data, row_idx, j_obj); + + auto j_arr = env->NewByteArray(d_str.GetSize()); + auto j_arr_el = env->GetByteArrayElements(j_arr, nullptr); + memcpy((void *)j_arr_el, (void *)d_str.GetData(), d_str.GetSize()); + env->ReleaseByteArrayElements(j_arr, j_arr_el, 0); + + env->SetObjectArrayElement(varlen_data, row_idx, j_arr); } break; case LogicalTypeId::UUID: diff --git a/src/main/java/org/duckdb/DuckDBVector.java b/src/main/java/org/duckdb/DuckDBVector.java index fe62174c..d6d3c240 100644 --- a/src/main/java/org/duckdb/DuckDBVector.java +++ b/src/main/java/org/duckdb/DuckDBVector.java @@ -278,7 +278,7 @@ Blob getBlob(int idx) throws SQLException { return null; } if (isType(DuckDBColumnType.BLOB)) { - return new DuckDBResultSet.DuckDBBlobResult((ByteBuffer) varlen_data[idx]); + return new DuckDBResultSet.DuckDBBlobResult(ByteBuffer.wrap((byte[]) varlen_data[idx])); } throw new SQLFeatureNotSupportedException("getBlob"); @@ -290,11 +290,7 @@ byte[] getBytes(int idx) throws SQLException { } if (isType(DuckDBColumnType.BLOB)) { - ByteBuffer bb = (ByteBuffer) varlen_data[idx]; - bb.position(0); - byte[] bytes = new byte[bb.remaining()]; - bb.get(bytes); - return bytes; + return (byte[]) varlen_data[idx]; } throw new SQLFeatureNotSupportedException("getBytes"); diff --git a/src/test/java/org/duckdb/TestDuckDBJDBC.java b/src/test/java/org/duckdb/TestDuckDBJDBC.java index 3e3fc6f3..08be7914 100644 --- a/src/test/java/org/duckdb/TestDuckDBJDBC.java +++ b/src/test/java/org/duckdb/TestDuckDBJDBC.java @@ -4604,6 +4604,20 @@ public static void test_metadata_get_index_info() throws Exception { } } + public static void test_blob_after_rs_next() throws Exception { + try (Connection conn = DriverManager.getConnection(JDBC_URL)) { + try (Statement stmt = conn.createStatement()) { + try (ResultSet rs = stmt.executeQuery("SELECT 'AAAA'::BLOB;")) { + Blob blob = null; + while (rs.next()) { + blob = rs.getBlob(1); + } + assertEquals(blob_to_string(blob), "AAAA"); + } + } + } + } + public static void main(String[] args) throws Exception { System.exit(runTests(args, TestDuckDBJDBC.class, TestExtensionTypes.class)); }