I am completing an Android program to classify the genre of a song on a mobile phone. The classification code is implemented in a native shared library (not written by me). Unfortunately, it only classifies correctly the first time, any times after then it just returns the previous result. I was thinking this was to do with the fact that some of the variables in the code are declared static and that they are not being instantiated properly between classifications.
I need to know how I can free the memory associated with a native library after each classification so that all the variables are instantiated correctly in the next classification.
I currently call the native library via a class, can I 'free' the class (like in C/C++) explicitly without waiting for the garbage collector?
#qrtt1 was right, searched through the code for a reset function and I found one... thanks!
Related
Currently I am using file.delete() but it is showing a security risk for this as files deleted like this can be recovered by different means. So please provide me a correct way to delete a file. The security risk depicted here is provided by a testing tool called Quixxi and it checks for any vulnerability in app.
The reason a "deleted" file is recoverable is because a delete operation simply unlinks the file in the filesystem, so the directory no longer considers that file part of it. The contents on disk (or whatever storage) still exist on that device.
If you want to guarantee the contents can never be recovered, you have to overwrite the contents first. There are no built-in functions to do this - you'd have to find a library or write the code yourself. Typically you'd write something like all 0s over the file (make sure to flush to media), write all 1s, write a pattern of 01 repeating, 10 repeating, something like that. After you've written with garbage patterns to media (flush) a few times, then you issue the delete.
Not possible in JRE, unfortunately. The JVM is not designed for that, and you need OS-dependent utilities.
The answer by user1676075 contains a mistake. Let's go by steps.
As pointed out already, Java's File.delete method only unlinks the file leaving its contents on disk. It actually invokes the underlying OS APIs to perform this unlink operation.
The problem occurs when you want to overwrite contents in Java.
Java can open a file for overwrite, but will leverage OS utils to do so. And the OS will likely:
Unlink the allocated space on disk
Link the file to a new free area of disk
The result is that you are now writing tons of zeroes... somewhere else!!!
And even if you managed to write zeroes on the same sectors used by the original file, Gutmann method exists for a reason. Gutmann utilities require root/Administrator (Super User) permissions and direct DMA access to precisely control where the writes have to occur.
And with SSDs, things changes. Actually, it might get easier! At this point, I should provide source for SSDs having a CLEAR instructions to replace a sector with zeroes and that privacy-savy disk controllers do that. But maybe pretend you have read nothing.
This will be a sufficient answer for now, because we have demonstrated that there is no out-of-the-box and straightforward way to securely clear a file in Java.
What Java allows, and is called Java Native Interfaces (please also see Java Native Access), is to call native code from Java. So, you got your Gutmann tool in C++ ready? Are you running root? You can write code to invoke Gutmann-ish erasure from Java, but that's a whole other point.
Never tried, but surely feasible
I am currently trying to have Logstash work on Solaris with the File Input method. But I am encountering some bugs (see LOGSTASH-665). After digging a lot, it appears that native support for File.stat on my system (SunOS 5.10, JDK 1.6.0_21, 32 bit) is totally deficient, so I am looking for a way to properly handle it.
Specifically I want to access the inode information. Based on the metadata I can gather about the file (like its path and whatever is available on solaris), I want to calculate a number which is unique for that file, and which changes when the file is replaced by another file which has the same name. At first I thought about simply using a hash of the file path and used this function as a replacement, but indeed, when the file is rolled over the number does not change, so I need to also access the ctime information...
..Or make a system call to get the ls -li result to get the real inode number by another way.
Problem is that I never used ruby before, I am more used to java, so I am struggling to find a solution. Every suggestion will be appreciated.
The best solution I know of is to wrap the native call using JNI or JNA.
There do appear to be a couple of projects that have done this, although I haven't used either of them. See this question: Is there a Java library of Unix functions?
I see alot of post concerning the Android Jni but I still do not have a clear idea how to edit it. I have a Phonegap app that is having JNI Errors when loading a certain page.
From what I have read during research, I need to release the local storage. How do I do this, I am wondering.
All I see to do it but no real clear path on what to do.
If you were going to either release localRef strings or create a globalRef string What would you do?
Thanks ahead.
So you have already created 512 local references and you need to create more. You either have to release some of the ones you've already created, via DeleteLocalRef(), if that makes sense in your code, or else request more local slots, via EnsureLocalCapacity() or PushLocalFrame(). I prefer the latter in conjunction with PopLocalFrame(): many coding patterns fit into it, maybe yours.
We were designing an android app for a competition. I was trying to improvise on the features by trying to have a temporary cache on the app. I was going to use HashMap for this purpose. However, I was wondering if there is any special way of creating dynamic hashMap. If yes, please let me know as I am a java noob.(Google did not help much) As of now, i am using the normal declaration for hashMap.(I'm sure that's not right though).
JVM takes care of memory once an application as been terminated. Memory will automatically be free'd up after you exit the program.
Now, if you want to dump the hash map to a file, that's a different story, since HashMap implements Serializable, you can dump the object bytes to a file before the app exits, and load it back in when it starts back up.
Decent write up on Serialization:
http://java.sun.com/developer/technicalArticles/Programming/serialization/
I would like to write a program which can monitor activity of my keyboard. In more details, the Java program should "see" which key is pressed/released and when. All this information should be stored in a given file.
First of all I would like to know if it's possible to do it with Java. I know that it's possible if I type in a text field generated by Java program. But is it possible for Java to monitor the keyboard activity if I type, let say, in a text field of a browser or, for example, in word (or open office) document?
These events are directed to the window which has the focus, from all events on the desktop you can only get the mouse position.
import java.awt.MouseInfo;
public class Mouse {
public static void main(String[] args) {
while ( true ) {
System.out.println( MouseInfo.getPointerInfo().getLocation() );
}
}
}
For capturing sysem wide (what you need for word) you need to include a native lib
Code example for windows: Native keyboard mouse hook
Forget JNI and JNA.
There is now a new library for native mouse&keyboard access that works exactly the same as MouseXxxListener and KeyListener. In other words, it's a familiar interface so you don't have to learn anything new (except for how you link an external library).
https://code.google.com/p/jnativehook/
Forget JNI
JNA is the solution (is based on JNI but it's way easier although most people don't use it thinking it's as difficult as JNI, but it's not)
Check https://github.com/twall/jna/
and specifically in the middle of the page where it says: Platform / Platform Documentation
If you download the platform.jar (and check the sourcecode) you'll find a wrapper around
User32.dll where you can find the win32 function
GetAsyncKeyState(int vKey)
mapped to a corresponding java function
(see h**ps://jna.dev.java.net/javadoc/platform/com/sun/jna/platform/win32/User32.html)
There u can check the state of a key (pressed or released)
If you need a global key listener have a look at my other reply to:
h**p://stackoverflow.com/questions/696170/java-keylistener-without-window-being-open/4394398
Thanks to Stackoverflow i can't post more than one working link in my replies :(
Cheers
It's likely possible to write a Java-based key logger using some native libraries, although be aware that such a program is likely more noticeable than one with a different technology, since the Java VM will need to be running for it to work. Keep that in mind if you're trying to be clandestine.
Also, if you just need such a program for use, and don't have to develop it yourself, there are many hardware and software keylogging systems already out there that you could use instead.
I think that native functions can do it.
It's something like you can connect c++ with Java.
http://en.wikipedia.org/wiki/Java_Native_Interface
What you are requesting is not possible using the Java API. In order to do system-wide key logging you need to register with win32 (or other OS native) hooks. Specifically, this will be done using native libraries and interfacing with the JNI.
There are some code snippets over at http://forums.sun.com/thread.jspa?messageID=3808163#3808163. It's a good example of how to get started with JNI and registering a Java callback to a win32 hook.