Thanks for help, I'll try this and report back. Igor
On Wed, Oct 24, 2012 at 1:37 AM, Jonathan Pryor <j...@xamarin.com> wrote: > On Oct 23, 2012, at 4:32 AM, Igor Russkih <iruss...@gmail.com> wrote: > > Assume I have my java class, and my cs class. What I want is to transfer > binary data between these two with max performance. > > > > Main direction is cs -> java for now. > > > > What is the best way? > > > > 1) JNI CopyArray - how is that implemented internally? SetIntArrayRegion? > > Yes, JNIEnv.CopyArray is implemented atop SetIntArrayRegion() and > SetIntArrayRegion(), plus some type overhead. > > > 2) DirectByteBuffer seems to be a better alternative, but on mono side, > the only way is Marshal.ReadInt32/WriteInt32? Is that fast? > > This should be, as DirectByteBuffer was designed for exposure of "native" > memory into Java processes. The only issue is that said "native" memory > can't move, so it can't be backed by a C# byte[], but it can be backed by > an "unmanaged" (non-moving) block of memory. > > As for reading the buffer contents from C#, there's Marshal.Read*() and > Marshal.Write*(), as you note, but there is also unsafe C# code and > pointers: > > void Foo(Java.Nio.Buffer buffer) > { > if (!buffer.IsDirect) > throw new ArgumentException (...); > IntPtr address = buffer.GetDirectBufferAddress(); > unsafe { > int* p = (int*) address; > *p = 42; > } > } > > To allocate said `buffer`, you can use either > ByteBuffer.AllocateDirect(int) (and Java allocates the block of memory), or > use JNIEnv.NewDirectByteBuffer() (and you allocate the memory): > > IntPtr directByteBuffer = > JNIEnv.NewDirectByteBuffer(Marshal.AllocHGlobal (4096), 4096L); > Buffer buffer = > Java.Lang.Object.GetObject<Buffer>(directByteBuffer, > JniHandleOwnership.TransferLocalRef); > Foo (buffer); > > - Jon > > _______________________________________________ > Monodroid mailing list > Monodroid@lists.ximian.com > > UNSUBSCRIBE INFORMATION: > http://lists.ximian.com/mailman/listinfo/monodroid >
_______________________________________________ Monodroid mailing list Monodroid@lists.ximian.com UNSUBSCRIBE INFORMATION: http://lists.ximian.com/mailman/listinfo/monodroid