Ventrix’s Code Folds

j2se

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

Leave a Comment : more...

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.

Leave a Comment :, more...

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);

Comments Off more...

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);
}
}
Leave a Comment more...

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/

(ΥΓ, δεν έχω ευθύνη για οτιδήποτε μπορεί να κάνετε με αυτό το εργαλείο, χρησιμοποιείστε το με δική σας ευθύνη)

Σχολιάστε ελεύθερα εδώ…

4 Comments : more...

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"); 
Leave a Comment : more...

Java Substring using strings v2

by ventrix on Oct.14, 2008, under j2ee, j2me, j2se, java

Updated version 2: Added the includeStartString boolean variable.

public static String returnSubsting(String text, String startString, String endString, Boolean includeStartString) {
        int start = 0;
        int end = 0;
        start = text.indexOf(startString);
        if (start >= 0) {
            if (!includeStartString)
            {
                start+=startString.length();
            }
            end = text.indexOf(endString, start);
            if (end >= 0) {
                return text.substring(start, end);
            } else {
                return null;
            }
        } else {
            return null;
        }
    }

It’s a kind of self documented, so I will only give an example:

String temp=”123ventrix321″;
System.out.println(returnSubsting(temp,”ven”,”321″,true));
will print “ventrix”

System.out.println(returnSubsting(temp,”ven”,”321″,false));
will print “trix”

Leave a Comment : more...

Java String array to String

by ventrix on Oct.13, 2008, under j2ee, j2me, j2se, java

This is a method I find very useful. It’s usage is pretty obvious… It takes a array of Strings and a seperator and returns a string with the string items separated.

private static String arrayToString(String[] items, String seperator) {
        if ((items == null) || (items.length == 0)) {
            return "";
        } else {
            StringBuffer buffer = new StringBuffer(items[0]);
            for (int i = 1; i < items.length; i++) {
                buffer.append(seperator);
                buffer.append(items[i]);
            }
            return buffer.toString();
        }
    }

Example:

String[] browsers = {"firefox", "internet explorer", "konqueror", "safari"};
String output = Class.arrayToString(browsers,", ");
System.out.println(output);

Prints: firefox, internet explorer, konqueror, safari

Leave a Comment : more...

Java Prime number

by ventrix on Oct.07, 2008, under j2se, java

This is one approach to check if the given number is a prime number. It can take a very big number as an argument. It uses no threads and has no comments. Think it as version 0.01.

/*
 *      Prime.java
 *
 *      Copyright 2008 Ventrix 
 *
 *      http://ventrix.nsdc.gr/code_folds/
 *
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */

import java.math.BigInteger;

public class Prime {

    private static long start;
    private static long end;

    public static void main(String[] argv) {
        boolean isprimen;
        //http://java.sun.com/j2se/1.4.2/docs/api/java/math/BigInteger.html#isProbablePrime(int)
        //http://primes.utm.edu/lists/small/small.html
        //for i in `seq 1 1000`; do java Prime $i >> primes; done
        //cat primes | grep "is a"
        start = System.currentTimeMillis();
        try {
            BigInteger bigNumber = new BigInteger(argv[0]);
            if (bigNumber.compareTo(new BigInteger("2147483647")) == 1) {
                if (bigNumber.compareTo(new BigInteger("9223372036854775807")) == -1) {
                    Long longNumber = new Long(argv[0]);
                    bigNumber = null;
                    isprimen = isNorPrime(longNumber);
                } else {
                    isprimen = isBigPrime(bigNumber);
                }
            } else {
                bigNumber = null;
                Integer intNumber = new Integer(argv[0]);
                isprimen = isPrime(intNumber);
            }

            if (isprimen) {
                System.out.println(argv[0] + " is a prime!");
            } else {
                System.out.println(argv[0] + " is NOT a prime!");
            }
            end = System.currentTimeMillis();
            System.out.println("Completed in +" + (end - start) + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static boolean isBigPrime(BigInteger number) {
        System.out.println("You gave me a BIG number");
        BigInteger[] remain;
        remain = number.divideAndRemainder(new BigInteger(new Integer("2").toString()));
        if (remain[1].compareTo(new BigInteger("0")) == 0) {
            return false;
        }
        remain = number.divideAndRemainder(new BigInteger(new Integer("3").toString()));
        if (remain[1].compareTo(new BigInteger("0")) == 0) {
            return false;
        }
        int y = 2;
        int x = (int) Math.sqrt(number.doubleValue());
        for (int i = 5; i <= x; i += y, y = 6 - y) {
            remain = number.divideAndRemainder(new BigInteger(new Integer(i).toString()));
            if (remain[1].compareTo(new BigInteger("0")) == 0) {
                return false;
            }
        }
        return true;
    }

    private static boolean isNorPrime(Long number) {
        System.out.println("You gave me a normal number");
        if (number % 2 == 0) {
            return false;
        }
        if (number % 3 == 0) {
            return false;
        }
        int y = 2;
        int x = (int) Math.sqrt(number);
        for (int i = 5; i <= x; i += y, y = 6 - y) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }

    private static boolean isPrime(Integer number) {
        System.out.println("You gave me a small number");
        if (number < 2) {
            return false;
        }
        if (number == 2) {
            return true;
        }
        if (number % 2 == 0) {
            return false;
        }
        if (number == 3) {
            return true;
        }
        if (number % 3 == 0) {
            return false;
        }
        int y = 2;
        int x = (int) Math.sqrt(number);
        for (int i = 5; i <= x; i += y, y = 6 - y) {
            if (number % i == 0) {
                return false;
            }
        }
        return true;
    }
}//class
1 Comment : more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...

Archives

All entries, chronologically...