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.
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 3 years ago.
Improve this question
I need to identify java source code all type of statements and store them in a tree structure to make a control flow graph! what i cannot understand is how should i read a java source code so that my program may distinguish all different types of statements in java( if,for, classes, methods etc.)
Do i need to add the whole grammar of java language?
what i cannot understand is how should i read a java source code so that my program may distinguish all different types of statements in java( if,for, classes, methods etc.)
Read java source code (uncompiled)
file extension is .java and it's just a regular text so this should be a trivial task.
Now depends what you wanted to parse and to store.
The best way is to have all the grammar and check the file.
There are tools that are doing lexical analysis, also known as language recognition, and also generate for you AST (abstract syntax tree). Eg. JavaCC or ANTLR.
But maybe you want just a custom parse(partial).
So you can store the keywords in a data-structure (if, for) and parse the file accordingly.
(and with some patterns for instruction eg:if. More could made simple automates_DFA for each instruction or maybe regular expression)
Even here is a little bit work. Eg. Want if from instruction not if from a text. String s="if". Or/And are you sure that every time will parse a valid java file? )
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 6 years ago.
Improve this question
I have a C++ program and a Java program. On the C++ side, I have a vector having millions of entries. I need to transfer this data to my Java program. So far, I have tried the following:
Created a unix socket, converted the vector to a long string (Serialized) and send it through the unix socket
Created a thrift server/client model to transfer the data
Both approaches worked very well, but the performance I'm getting is quite low. I don't even see it using the full network bandwidth (in the case of thrift).
Also with the unix socket approach, Since I'm serializing it to String and then again converting this string back to a string array (received byte[] to String and split) on the Java side is very expensive operation.
What is the best way to transfer data faster from the C++ world to the Java world with lesser overhead on reconstructing/serializing the object?
If both problems are on the same machine, I would use a shared memory mapped file.
This way both programs can access the data at full memory speed without serialization/deserialization esp if the values are int, long or double values.
You can place the file on a tmpfs, or ram drive to avoid hitting a hard drive.
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 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.)
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 will get straight to question. I have facedetection done in c with .dll libraries using jni. I send stream array from java to c. I am successful on getting coordinates of the face (4 points), how do I transfer those coordinates from C back to java? These coordinates are constantly updated, and I need to use those 4points to draw a rectangle in java around the face. At the moment in c I can only print out the points. I tryed writing themto .txt the reading in java from it but there is such a huge delay,so I abandoned this attempt.
With JNI you can:
Access Java fields from C.
Call Java methods from C.
If performance matters to you, you might want to have a data structure for these 4 points, pass a reference to that data structure to C, and in C update the fields of that data structure. (data structure = class that primarily holds data but doesn't offer a lot of operations)