java
How to sort an ArrayList in Java
by ventrix on Sep.22, 2009, under android, java
Example:
Class Test {
public String name;
public String sirName;
}
ArrayList<Test> res = new ArrayList<Test>();
res.add(…);
res.add(…);
Comparator<Test> comperator = new Comparator<Test>() {
@Override
public int compare(Test object1, Test object2) {
return object1.name.compareToIgnoreCase(object2.name);
}
};
Collections.sort(res, comperator);
the list is now sorted by name.
![]()
Java socket timeout
by ventrix on Jan.30, 2009, under android, j2ee, j2me, j2se, java
private String socketIP="192.168.1.1"; private int socketPort=2424; private int timeout=5000; //5 seconds Socket socket1 = new Socket(); socket1.connect(new InetSocketAddress(this.socketIP, this.socketPort), timeout); //or else Socket socket1 = new Socket(); socket1.connect(new InetSocketAddress(this.socketIP, this.socketPort)); socket1.setSoTimeout(timeout);
http://java.sun.com/j2se/1.4.2/docs/api/java/net/Socket.html
Java float problems
by ventrix on Dec.23, 2008, under android, j2ee, j2se, java
If you want 0.6917 - 0.6911 to be 0.0006 then:
http://www.velocityreviews.com/forums/showpost.php?s=bfd8bcba71b3b530733952c65e86772a&p=1604265&postcount=4
There are three solutions:
1. If you’ve got enough accuracy but just need the numbers to look
better when you display them, then you can simply use
java.text.DecimalFormat to format the answers to a more reasonable
length when they are displayed. Rounding will occur as appropriate in
the display layer, and the result should look just fine.
2. If you aren’t getting enough precision, then you could look into the
double data type instead of float. Double has not only an immensely
larger range, but also approximately twice the precision in digits.
3. If you need exact math in decimal (often required for financial
calculations, for example) then the class java.math.BigDecimal is what
you’re looking for. BigDecimal lets you store values of any arbitrary
precision; it stores them in decimal, so you need not worry about
rounding that results from binary conversion, and you can customize the
rounding mode on operations that still need it, like division… or even
disabling rounding entirely so that the runtime will throw an exception
to let you know when a calculation won’t have an exact result.
Closing a stream
by ventrix on Dec.10, 2008, under android, j2ee, j2me, j2se, java
/**
* Closes the specified stream.
*
* @param stream The stream to close.
*/
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e("IO", "Could not close stream", e);
}
}
Use it like: closeStream(in);
Copy the content of the input stream into the output stream
by ventrix on Dec.10, 2008, under android, j2ee, j2me, j2se, java
Copy the content of the input stream into the output stream
/**
* Copy the content of the input stream into the output stream, using a temporary
* byte array buffer whose size is defined by {@link #IO_BUFFER_SIZE}.
*
* @param in The input stream to copy from.
* @param out The output stream to copy to.
*
* @throws IOException If any error occurs during the copy.
*/
private static final int IO_BUFFER_SIZE = 4 * 1024;
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
Java Form Cracker Testing call
by ventrix on Nov.26, 2008, under j2se, java
Έφτιαξα μια εφαρμογή σε Java (brute forcer) που κάνει crack web forms.
Είναι αποτέλεσμα δουλειάς 3 ημερών και για αυτό χρειάζεται περαιτέρω testing.
Ο πηγαίος κώδικας θα δημοσιευτεί σύντομα.
Κατεβάστε το, δοκιμάστε το (θα βρείτε οδηγίες για να το δοκιμάσετε στον δικό μου server ελεύθερα) και στείλτε μου σχόλια!
Website: http://ventrix.nsdc.gr/projects/form_cracker/
(ΥΓ, δεν έχω ευθύνη για οτιδήποτε μπορεί να κάνετε με αυτό το εργαλείο, χρησιμοποιείστε το με δική σας ευθύνη)
Σχολιάστε ελεύθερα εδώ…
Creating random numbers in j2me and android within specific space
by ventrix on Nov.23, 2008, under Uncategorized, android, j2me, java
Creating random numbers in j2me within specific space
import java.util.Random;
/**
* Random Number Generator within specific space
* @param min is the minimum number to be produced
* @param max is the maximum number to be produced
* @return the random number
* @author ventrix
*/
public final class RandomCreator{
public static int getRandomInt(int min,int max)
{
try {
//Give the currentTimeMillis some time for the seed
Thread.sleep(2);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
Random randomizer = new Random(System.currentTimeMillis());
return randomizer.nextInt(max-min+1)+min;
}
}
Use it as: System.out.println(RandomCreator.getRandomInt(5,10));
JSR180 SIP API for J2ME javadoc documentation
by ventrix on Oct.26, 2008, under j2me, java
Ok, I have found the javadoc on the internet, but I want to have it at my own server.
So, here it is: JSR180 SIP API
Jain-Sip javadoc Documentation
by ventrix on Oct.25, 2008, under j2ee, java
I’m currently working with Jain-Sip and the java doc site is down. So I uploaded it here.
You can download it for offline use here
Java execution time
by ventrix on Oct.16, 2008, under j2ee, j2me, j2se, java
A very simple way to see how much time needed to run your application.
//variables
private long start;
private long end;
//Put this before the main code
start = System.currentTimeMillis();
//Main code here
//Put this at the end of the code
end = System.currentTimeMillis();
System.out.println("Completed in +"+(end-start)+"ms");