Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I've read this link about the GetByteArrayElements:
FAQ: How do I share raw data with native code?
http://developer.android.com/training/articles/perf-jni.html
It said that GetByteArrayElements will return a actual pointers to the raw data in the Dalvik heap. So I can manipulate the raw source in C++ and speed up the process, am I right?
And so, ReleaseByteArrayElements won't copy the data also? Or since GetByteArrayElements return a pointer and I don't even need to release it after manipulation of data just like using GetDirectBufferAddress for FloatBuffer?
If it doesn't have to copy any data from Java to C++, is that possible to pass in and manipulate float array via GetByteArrayElements? Plz answer at NDK: Passing Jfloat array from Java to C++ via GetByteArrayElements?
Get<Primitive>ArrayElements may or may not copy the data as it sees fit. The isCopy output parameter will tell you whether it has been copied. If data is not copied, then you have obtained a pointer to the data directly in the Dalvik heap. Read more here.
You always need to call the corresponding Release<Primitive>ArrayElements, regardless of whether a copy was made. Copying data back to the VM array isn't the only cleanup that might need to be done, although (according to the JNI documentation already linked) it is feasible that changes can be seen on the Java side before Release... has been called (iff data has not been copied).
I don't believe the VM is going to allow you to make the conversions that would be necessary to do what you are thinking. As I see it, either way you go, you will need to convert a byte array to a float or a float to a byte array in Java, which you cannot accomplish by type casting. The data is going to be copied at some point.
Edit:
What you are wanting to do is possible using ByteBuffer.allocateDirect, ByteBuffer.asFloatBuffer, and GetDirectBufferAddress. In Java, you can use the FloatBuffer to interpret data as floats, and the array will be available directly in native code using GetDirectBufferAddress. I posted an answer to your other question as further explanation.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am need to write an obfuscation mechanism and think that the cracker can just read desired address in memory and access my object. All that I have so far come up with for the role of protection against this is the frequent "shuffling" of addresses in the heap. As far as I know, in garbage collection, the collector can do this.
My question is: is it possible in Java 8 to implement such a "shuffle" or at least change the address of a particular object, if so, how?
I am need to write an obfuscation mechanism and think that the cracker can just read desired address in memory and access my object.
You are probably trying to solve a problem that doesn't really exist. For a non-trivial Java program, it is extremely unlikely that a specific object will have the same memory address across multiple runs of your application. So checking the content of fixed memory addresses to find objects would not offer any "leverage" for the hacker. And ... it follows that moving objects won't improve security.
Having said that, the Java language and the Java Virtual Machine provide no way to move objects in memory. The closest you will get is to create a new object ... which will have a different address to the old one ... copy its state, and then update any variables containing references to it.
Finally ...
If the hackers (or customers who you don't trust) control the hardware or operating system on which your application is running, then there is nothing you can do to physically prevent them from hacking your code ... if they have enough time, skill and motivation.
Anything you can conceivably do can be subverted. Trying to come up with clever ways to make hacking more difficult is ultimately futile.
Java itself does not provide you with any API to manipulate the address of an object instance on the heap.
The JVM as such knows how to manipulate object addresses, and there are several ways to access this features, but not from the Java programming language, nor from any other JVM language I am aware of.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Currently I am working on transfer image from C++ to Java.
The destination location is allocate by Java,
the source location is the image generated by C++, so.
I have a
uint8_t* pixelPtr
, I want to move the content of this to a
__uint8_t* data
without copy.
I have 1920*1080*3 bytes in total, so I want to move rather than copy to be fast in computation, I am wondering is there any trick way to do so?
Thank you in advance!
Let's recap:
The source is a buffer allocated in C++ by an image generation function.
The destination is a buffer allocated in Java by some other code somewhere.
You want to transfer data between the two buffers.
As long as those two buffers are distinct, there is no "trick" to avoid this. "Moving" in this context would mean swapping the pointers around, but that does nothing to the underlying buffers. You will just have to copy the data.
Explore solutions such as generating the data in the destination buffer in the first place, or making use of appropriate functionality exposed by the C++ image generation function (or the Java code). Unfortunately we can't speculate on the possible existence or form of such solutions, from here.
The standard way is, you should modify your C++ code so it creates the data not wherever it wants, but in the given place. That is, if you have code like this
uint8_t* GenerateImage(...parameters...)
{
uint8_t* output = ... allocate ...
return output;
}
you should change it to receive the destination as a parameter
void GenerateImage(...parameters..., __uint8_t* destination)
{
... fill the destination ...
}
The latter is better C++ design anyway - this way you don't need to make a separate DestroyImage function - the memory is managed entirely by Java.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to pass a structure from java to C code and return a structure from C to Java code using JNI. I haven't find something useful regarding structures and JNI on net.
Can someone please provide a simple code snippet for the same?
This is a very broad area and you need to do more research online ( it's ell covered out there ). However, briefly ...
If you simply want to keep a blob of data and pass it between java methods without changing it you can store it in a Java byte array.
You could also malloc() the struct on the C side and then pass Java a pointer ( stored ideally in a byte array for reasons I won;t go into ). Again, Java can't mess with the data, but can at least pass a reference to it around.
If you actually need a data structure on both sides that you can access by fields and change, then you need to create wrapper functions that convert between the two ( C struct and Java class ). One way to automate this is to use SWIG , which is a code generator that can generate JNI wrapper code for you from C include files.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have several matrices in Java that I would like to transfer to Python as efficiently as possible, without requiring anything but standard libraries on both the Java and Python sides.
Currently I serialize them to file using the writeDouble function to write the entries out one by one, and writeInt to write the dimensions of the matrices. Now I would like to read these matrices back into Python. I can get the integers using struct.unpack, but Java's serialization of doubles does not correspond to an algorithm that struct.unpack can implement.
How can I decode a Java double in the binary format that writeDouble uses? I have trouble even finding a specification for the encoding that writeDouble uses.
You're overengineering it; DataOutputStream.writeDouble() and related methods are for manually serializing a Java Object, so it can be re-read as a Java Object. If all you need is to transfer data, you can simply write them out as text (or bytes), then read them back in. Common formats are CSV, JSON, XML, and ProtoBuf.
If all you're doing is trying to transfer a list of doubles, you can even just write them out one per line, and read them right back in with Python.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am working on a project where I will have a binary file. The file is split into multiple sections, each of which represents a list of primitive values. I need a solution where I can have a collection of objects, each of which represents a section of the file. These collections are then all held within a "file" object that represents the file as a whole.
Each collections object will need to provide sequential access to each value in the represented section of the file. What method would provide the fastest data retrieval without loading all the data into memory first?
Also it would be nice if two separate collections of the same "file" object could be accessed by two separate Threads, but this is not as important.
A good approach is to divide the solution into layers, here: one for the file i/o, mapping bytes to Java shorts and ints, another one for the abstraction of the file sections and the entire file.
java.nio's MappedByteBuffer provides a good interface between the "byte array" of a random access file and what you need for getting the Java typed data from that.
As Kayaman has mentioned, FileChannel.map() returns a MappedByteBuffer and you can navigate easily on that with its methods.
The implemention should make use of the OS feature for mapping memory pages to file pages, actually accessing on the file only what you really access in memory. (I've used this recently with Java 8 and Linux, and it performed well on files exceeding even the capacity of a single MappedByteBuffer.)