diff --git a/swig/include/java/gdal_java.i b/swig/include/java/gdal_java.i index c3ed14adfcbd..65e1919370e5 100644 --- a/swig/include/java/gdal_java.i +++ b/swig/include/java/gdal_java.i @@ -1432,3 +1432,17 @@ import org.gdal.gdalconst.gdalconstConstants; %include callback.i %include typemaps_java.i + + +// Also defined in python/gdal_python.i and csharp/gdal_csharp.i + +%rename (GetMemFileBuffer) wrapper_VSIGetMemFileBuffer; + +%apply (GByte* outBytes) {GByte*}; +%inline %{ +GByte* wrapper_VSIGetMemFileBuffer(const char *utf8_path, vsi_l_offset *length) +{ + return VSIGetMemFileBuffer(utf8_path, length, 0); +} +%} +%clear GByte*; diff --git a/swig/include/java/typemaps_java.i b/swig/include/java/typemaps_java.i index 551cc42b1f4e..c4d3c259d897 100644 --- a/swig/include/java/typemaps_java.i +++ b/swig/include/java/typemaps_java.i @@ -403,6 +403,45 @@ SafeNewStringUTF8(JNIEnv *jenv, const char* pszInput) } +/*************************************************************** + * Typemaps for (const char *utf8_path, vsi_l_offset *length) + ***************************************************************/ + +%typemap(in) (const char *utf8_path, vsi_l_offset *length) (vsi_l_offset length) +{ + /* %typemap(in) (const char *utf8_path, vsi_l_offset *length) */ + if ($input) + { + $1 = (char *)jenv->GetStringUTFChars($input, 0); + } + else + { + SWIG_JavaException(jenv, SWIG_ValueError, "Received a NULL pointer."); return $null; + } + $2 = &length; +} + +%typemap(argout) (const char *utf8_path, vsi_l_offset *length) +{ + /* %typemap(argout) (const char *utf8_path, vsi_l_offset *length) */ + if ($input) + { + jenv->ReleaseStringUTFChars($input, (char*)$1); + } + $result = jenv->NewByteArray((jsize)length$argnum); + jenv->SetByteArrayRegion($result, (jsize)0, (jsize)length$argnum, (jbyte*)result); + // Do not free result, as it is owned by the /vsimem/ file +} + +%typemap(jni) (const char *utf8_path, vsi_l_offset *length) "jstring" +%typemap(jtype) (const char *utf8_path, vsi_l_offset *length) "String" +%typemap(jstype) (const char *utf8_path, vsi_l_offset *length) "String" +%typemap(javain) (const char *utf8_path, vsi_l_offset *length) "$javainput" +%typemap(javaout) (const char *utf8_path, vsi_l_offset *length) { + return $jnicall; + } + + /*************************************************** * Typemaps for (GByte* outBytes ) ***************************************************/ diff --git a/swig/java/apps/GDALTestIO.java b/swig/java/apps/GDALTestIO.java index 4af18b71f7f6..20d32bfb5b0a 100644 --- a/swig/java/apps/GDALTestIO.java +++ b/swig/java/apps/GDALTestIO.java @@ -23,6 +23,7 @@ import org.gdal.gdal.Dataset; import org.gdal.gdal.Driver; import org.gdal.gdalconst.gdalconst; +import org.gdal.gdal.*; public class GDALTestIO implements Runnable { @@ -148,7 +149,9 @@ public static void main(String[] args) throws InterruptedException gdal.AllRegister(); testInt64(); - + + testGetMemFileBuffer(); + int nbIters = 50; method = METHOD_JAVA_ARRAYS; @@ -186,12 +189,12 @@ public static void main(String[] args) throws InterruptedException System.out.println("Success !"); } - + private static void testInt64() { - + long[] data1; long[] data2; - + int xSz = 5; int ySz = 2; int nBands = 1; @@ -214,4 +217,17 @@ private static void testInt64() { throw new RuntimeException("int64 write and read values are not the same "+data1[i]+" "+data2[i]); } } + + private static void testGetMemFileBuffer() + { + gdal.FileFromMemBuffer("/vsimem/test", new byte[] {1, 2, 3}); + for(int iter = 0; iter < 2; iter++) + { + byte[] res = gdal.GetMemFileBuffer("/vsimem/test"); + if (res.length != 3 ) + throw new RuntimeException("res.length != 3"); + if (res[0] != 1 || res[1] != 2 || res[2] != 3) + throw new RuntimeException("res != {1, 2, 3}"); + } + } } diff --git a/swig/java/javadoc.java b/swig/java/javadoc.java index 37e57f6bd572..81e830bd051f 100644 --- a/swig/java/javadoc.java +++ b/swig/java/javadoc.java @@ -239,6 +239,25 @@ public class gdal:public static String EscapeString(byte[] byteArray, int scheme */ public class gdal:public static void FileFromMemBuffer(String fileName, byte[] byteArray) + +/** + * Fetch buffer underlying memory file. + * + * This function returns a byte[] array with a copy of the content of the memory + * buffer underlying a virtual "in memory" file. The original file is unmodified + * by this operation, and must be removed with Unlink(fileName) if necessary. + * + * @param fileName filename (should begin with "/vsimem/") + * + * @return file content + * + * @since 3.10.1 + * + * @see #Unlink(String fileName) + */ +public class gdal:public static byte[] GetMemFileBuffer(String fileName) + + /** * Delete a file. *