Skip to content

Commit

Permalink
Java bindings: add byte[] org.gdal.gdal.GetMemFileBuffer(String filen…
Browse files Browse the repository at this point in the history
…ame)

Fixes OSGeo#11192
  • Loading branch information
rouault committed Nov 6, 2024
1 parent c578e9d commit 3b98913
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
14 changes: 14 additions & 0 deletions swig/include/java/gdal_java.i
Original file line number Diff line number Diff line change
Expand Up @@ -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*;
39 changes: 39 additions & 0 deletions swig/include/java/typemaps_java.i
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
***************************************************/
Expand Down
24 changes: 20 additions & 4 deletions swig/java/apps/GDALTestIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -148,7 +149,9 @@ public static void main(String[] args) throws InterruptedException
gdal.AllRegister();

testInt64();


testGetMemFileBuffer();

int nbIters = 50;

method = METHOD_JAVA_ARRAYS;
Expand Down Expand Up @@ -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;
Expand All @@ -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}");
}
}
}
16 changes: 16 additions & 0 deletions swig/java/javadoc.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,22 @@ 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 to the memory buffer underlying a
* virtual "in memory" file.
*
* @param fileName filename (should begin with "/vsimem/")
*
* @return file content
*
* @since 3.10.1
*/
public class gdal:public static byte[] GetMemFileBuffer(String fileName)


/**
* Delete a file.
* <p>
Expand Down

0 comments on commit 3b98913

Please sign in to comment.